Skip to content

Commit 7b950e2

Browse files
authored
Merge pull request #4 from LiuXingLong/tyler.liu-feature
HashMap + 二叉搜索树 Map 优化查找删除节点的右子树中的最小节点
2 parents 1951b57 + 0f2f43f commit 7b950e2

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

BST/bst/bst.depend

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
<iostream>
77
"bst.h"
88

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

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

BST/bst/bst.layout

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
<CodeBlocks_layout_file>
33
<FileVersion major="1" minor="0" />
44
<ActiveTarget name="Debug" />
5-
<File name="main.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
5+
<File name="include\bst.h" open="0" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
66
<Cursor>
7-
<Cursor1 position="3174" topLine="36" />
7+
<Cursor1 position="677" topLine="0" />
88
</Cursor>
99
</File>
10-
<File name="hash.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
10+
<File name="hash.cpp" open="0" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
1111
<Cursor>
12-
<Cursor1 position="6251" topLine="94" />
12+
<Cursor1 position="6422" topLine="160" />
1313
</Cursor>
1414
</File>
15-
<File name="include\bst.h" open="0" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
15+
<File name="main.cpp" open="0" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
1616
<Cursor>
17-
<Cursor1 position="633" topLine="0" />
17+
<Cursor1 position="935" topLine="27" />
1818
</Cursor>
1919
</File>
20-
<File name="src\bst.cpp" open="1" top="1" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
20+
<File name="src\bst.cpp" open="0" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
2121
<Cursor>
22-
<Cursor1 position="2613" topLine="72" />
22+
<Cursor1 position="2535" topLine="184" />
2323
</Cursor>
2424
</File>
2525
</CodeBlocks_layout_file>

BST/bst/include/bst.h

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class bst
2626
bool bst_set(string key, string value,unsigned int hash_key, BstNode *&node, BstNode *parent);
2727
bool bst_del(string key, unsigned int hash_key, BstNode *&node);
2828
unsigned int BKDRHash(string key);
29-
private:
30-
BstNode * get_min_node(BstNode *&node);
3129
};
3230

3331
#endif // BST_H

BST/bst/src/bst.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ unsigned int bst::BKDRHash(string key)
1111
}
1212
return (hash & 0x7FFFFFFF);
1313
}
14-
BstNode * bst::get_min_node(BstNode *&bst_p)
15-
{
16-
if(bst_p->lchild == nullptr){
17-
return bst_p;
18-
}else{
19-
return this->get_min_node(bst_p->lchild);
20-
}
21-
}
2214

2315
string bst::bst_get(string key, unsigned int hash_key,BstNode *bst_p)
2416
{
@@ -36,6 +28,12 @@ string bst::bst_get(string key, unsigned int hash_key,BstNode *bst_p)
3628
}
3729
if( key == bst_data->key ){
3830
return bst_data->value;
31+
/*
32+
if(bst_p->parent != nullptr){
33+
return bst_data->value + "|" + bst_p->parent->data->value;
34+
}else{
35+
return bst_data->value + "|root";
36+
}*/
3937
}else{
4038
return "null";
4139
}
@@ -98,7 +96,10 @@ bool bst::bst_del(string key, unsigned int hash_key, BstNode *&bst_p)
9896
/// 删除含两个子节点的 父节点
9997
BstNode *bst_h;
10098
bst_h = bst_p;
101-
bst_p = this->get_min_node(bst_p->rchild);
99+
bst_p = bst_p->rchild;
100+
while(bst_p->lchild != nullptr){
101+
bst_p = bst_p->lchild;
102+
}
102103
/// 处理删除节点,右子树中最小节点的孩子
103104
if(bst_p->rchild != nullptr){
104105
if(bst_p->parent->hash_key > bst_p->hash_key){

0 commit comments

Comments
 (0)