update dp
This commit is contained in:
+11
-3
@@ -1,12 +1,15 @@
|
||||
# 二叉查找树BST
|
||||
|
||||
二叉查找树(Binary search tree),也叫`有序二叉树(Ordered binary tree)`,`排序二叉树(Sorted binary tree)`。是指一个空树或者具有下列性质的二叉树:
|
||||
二叉查找树(Binary search tree),也叫`有序二叉树(Ordered binary tree)`,`排序二叉树(Sorted binary tree)`。
|
||||
|
||||
是指一个空树或者具有下列性质的二叉树:
|
||||
|
||||
1. 若任意节点的左子树不为空,则左子树上所有的节点值小于它的根节点值
|
||||
2. 若任意节点的右子树不为空,则右子树上所有节点的值均大于它的根节点的值
|
||||
3. 任意节点左右子树也为二叉查找树
|
||||
4. 没有键值(key)相等的节点
|
||||
|
||||
|
||||
有序的二叉查找树,中序遍历结果是递增的。
|
||||
|
||||
|
||||
@@ -23,12 +26,15 @@
|
||||
```
|
||||
|
||||
|
||||
### 删除节点
|
||||
|
||||
删除节点,需要重建排序树
|
||||
|
||||
1) 删除节点是叶子节点(分支为0),结构不破坏
|
||||
2)删除节点只有一个分支(分支为1),结构也不破坏
|
||||
3)删除节点有2个分支,此时删除节点
|
||||
3)删除节点有2个分支,此时删除节点 ; 需要重建树
|
||||
|
||||
|
||||
思路一: 选左子树的最大节点,或右子树最小节点替换
|
||||
|
||||
```
|
||||
@@ -55,6 +61,7 @@ int bisearch_tree_delete(BiSearchTree **tree,ElemType node){
|
||||
printf("树为空,或想要删除的节点不存在\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//该节点为叶子节点,直接删除
|
||||
if (!target->rChild && !target->lChild)
|
||||
{
|
||||
@@ -86,7 +93,8 @@ int bisearch_tree_delete(BiSearchTree **tree,ElemType node){
|
||||
target->rChild=target->rChild->rChild;
|
||||
|
||||
free(del);
|
||||
}
|
||||
}
|
||||
|
||||
else{ //左右子树均不空,p,t 2个指针一前以后,将左子树最大的节点(肯定是一个最右的节点)替换到删除的节点后,还需要处理左子树最大节点的左子树
|
||||
|
||||
BiSearchTree *p=target,*t=target->lChild;
|
||||
|
||||
Reference in New Issue
Block a user