Skip to content

Commit 1951b57

Browse files
authored
Merge pull request #3 from LiuXingLong/tyler.liu-feature
Tyler.liu feature
2 parents e409775 + 66a74d0 commit 1951b57

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

BST/bst/bst.depend

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# depslib dependency file v1.0
2-
1544098639 source:c:\users\hnust_liuxinglong\desktop\tree\bst\bst\main.cpp
2+
1544106431 source:c:\users\hnust_liuxinglong\desktop\tree\bst\bst\main.cpp
33
<time.h>
44
<stdio.h>
55
<fstream>
66
<iostream>
77
"bst.h"
88

9-
1544077413 c:\users\hnust_liuxinglong\desktop\tree\bst\bst\include\bst.h
9+
1544100096 c:\users\hnust_liuxinglong\desktop\tree\bst\bst\include\bst.h
1010
<stdlib.h>
1111
<string>
1212
<iostream>
1313

14-
1544098605 source:c:\users\hnust_liuxinglong\desktop\tree\bst\bst\src\bst.cpp
14+
1544112403 source:c:\users\hnust_liuxinglong\desktop\tree\bst\bst\src\bst.cpp
1515
"bst.h"
1616

BST/bst/main.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@ int main()
2121
filename = "data\\set_" + std::to_string(i) + ".in";
2222
cout << filename << endl;
2323
infile.open(filename.c_str(),ios::in);
24-
while( !infile.eof() ){
25-
infile >> key;
26-
infile >> value;
27-
BstMap.bst_set(key,value,BstMap.BKDRHash(key),bst_p,parent);
24+
if(infile){
25+
while( !infile.eof() ){
26+
infile >> key;
27+
infile >> value;
28+
BstMap.bst_set(key,value,BstMap.BKDRHash(key),bst_p,parent);
29+
}
30+
infile.close();
31+
e_time = clock();
32+
cout<< "Data loading Time:" << (double)(e_time-s_time)/CLOCKS_PER_SEC << "s" << endl;
33+
}else{
34+
cout<< "Open Data: " + filename + " Error" << endl;
2835
}
29-
infile.close();
30-
e_time = clock();
31-
cout<< "Data loading Time:" << (double)(e_time-s_time)/CLOCKS_PER_SEC << "s" << endl;
3236
}
3337
/// 加载更新数据
3438
infile.open("data\\update.in",ios::in);

BST/bst/src/bst.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,37 @@ bool bst::bst_del(string key, unsigned int hash_key, BstNode *&bst_p)
9999
BstNode *bst_h;
100100
bst_h = bst_p;
101101
bst_p = this->get_min_node(bst_p->rchild);
102+
/// 处理删除节点,右子树中最小节点的孩子
102103
if(bst_p->rchild != nullptr){
103-
bst_p->parent->lchild = bst_p->rchild;
104+
if(bst_p->parent->hash_key > bst_p->hash_key){
105+
bst_p->parent->lchild = bst_p->rchild;
106+
}else if(bst_p->parent->hash_key < bst_p->hash_key){
107+
bst_p->parent->rchild = bst_p->rchild;
108+
}
104109
bst_p->rchild->parent = bst_p->parent;
105110
}else{
106-
bst_p->parent->lchild = nullptr;
111+
if(bst_p->parent->hash_key > bst_p->hash_key){
112+
bst_p->parent->lchild = nullptr;
113+
}else if(bst_p->parent->hash_key < bst_p->hash_key){
114+
bst_p->parent->rchild = nullptr;
115+
}
107116
}
117+
/// 处理删除节点,左右孩子
108118
bst_p->lchild = bst_h->lchild;
109119
bst_h->lchild->parent = bst_p;
110120
bst_p->rchild = bst_h->rchild;
111-
bst_h->rchild->parent = bst_p;
121+
if(bst_h->rchild != nullptr){
122+
bst_h->rchild->parent = bst_p;
123+
}
124+
/// 处理删除节点,父亲节点
112125
if(bst_h->parent == nullptr){
113126
bst_p->parent = nullptr;
114127
}else if(bst_h->parent->hash_key > bst_h->hash_key){
115128
bst_h->parent->lchild = bst_p;
116-
bst_p->parent = bst_h->parent->lchild;
129+
bst_p->parent = bst_h->parent;
117130
}else if(bst_h->parent->hash_key < bst_h->hash_key){
118131
bst_h->parent->rchild = bst_p;
119-
bst_p->parent = bst_h->parent->rchild;
132+
bst_p->parent = bst_h->parent;
120133
}
121134
delete bst_h;
122135
bst_h = nullptr;

0 commit comments

Comments
 (0)