Skip to content

Commit

Permalink
HashMap + 二叉搜索树 Map 删除bug修复
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler.liu committed Dec 6, 2018
1 parent 3aa10a6 commit 66a74d0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
4 changes: 2 additions & 2 deletions BST/bst/bst.depend
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# depslib dependency file v1.0
1544105912 source:c:\users\hnust_liuxinglong\desktop\tree\bst\bst\main.cpp
1544106431 source:c:\users\hnust_liuxinglong\desktop\tree\bst\bst\main.cpp
<time.h>
<stdio.h>
<fstream>
Expand All @@ -11,6 +11,6 @@
<string>
<iostream>

1544104271 source:c:\users\hnust_liuxinglong\desktop\tree\bst\bst\src\bst.cpp
1544112403 source:c:\users\hnust_liuxinglong\desktop\tree\bst\bst\src\bst.cpp
"bst.h"

23 changes: 18 additions & 5 deletions BST/bst/src/bst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,37 @@ bool bst::bst_del(string key, unsigned int hash_key, BstNode *&bst_p)
BstNode *bst_h;
bst_h = bst_p;
bst_p = this->get_min_node(bst_p->rchild);
/// 处理删除节点,右子树中最小节点的孩子
if(bst_p->rchild != nullptr){
bst_p->parent->lchild = bst_p->rchild;
if(bst_p->parent->hash_key > bst_p->hash_key){
bst_p->parent->lchild = bst_p->rchild;
}else if(bst_p->parent->hash_key < bst_p->hash_key){
bst_p->parent->rchild = bst_p->rchild;
}
bst_p->rchild->parent = bst_p->parent;
}else{
bst_p->parent->lchild = nullptr;
if(bst_p->parent->hash_key > bst_p->hash_key){
bst_p->parent->lchild = nullptr;
}else if(bst_p->parent->hash_key < bst_p->hash_key){
bst_p->parent->rchild = nullptr;
}
}
/// 处理删除节点,左右孩子
bst_p->lchild = bst_h->lchild;
bst_h->lchild->parent = bst_p;
bst_p->rchild = bst_h->rchild;
bst_h->rchild->parent = bst_p;
if(bst_h->rchild != nullptr){
bst_h->rchild->parent = bst_p;
}
/// 处理删除节点,父亲节点
if(bst_h->parent == nullptr){
bst_p->parent = nullptr;
}else if(bst_h->parent->hash_key > bst_h->hash_key){
bst_h->parent->lchild = bst_p;
bst_p->parent = bst_h->parent->lchild;
bst_p->parent = bst_h->parent;
}else if(bst_h->parent->hash_key < bst_h->hash_key){
bst_h->parent->rchild = bst_p;
bst_p->parent = bst_h->parent->rchild;
bst_p->parent = bst_h->parent;
}
delete bst_h;
bst_h = nullptr;
Expand Down

0 comments on commit 66a74d0

Please sign in to comment.