The nature of the binary search tree:
1,All nodes on the left subtreeThe value of is less than or equal to the value of its root node.
2,All nodes on the right subtreeThe value of is greater than or equal to the value of its root node.
Maximum heap nature:
1,All nodes in the two subtreesThe value of is not greater than the value of its root node.
1. Predecessor and Successor:
Performing on a binary treeMid-order traversal (not layer order), The order after traversal, the previous node of the current node is the predecessor node of this node; the next node of the current node is the successor node of this node.
1.1 For the binary search tree:
nodes x
predecessor node: less than the x.key
largest key node (15 precursor precursor precursor 4,7 6,17 6) is;
node x
successor nodes: greater than x.key
the smallest key node (6 The successor of 7, 7, the successor of 9, the successor of 13 15); the
successor pseudo code:
int TreeSuccessor(node x)
{
if(x.right != NULL) return Tree_minmum(x.right);
y = x.p;
while(y!=NULL && x ==y.right){
//若x为右节点,则后继为最低的祖先且其左孩子也是祖先(13的后继15)
x = y;
y = y.p;
}
return y; //若x为左节点,则返回父节点
}
2. Insertion and deletion:
2.1. Insertion:
The newly inserted node is always a leaf node, Therefore, the found suitable position is empty, and the parent node of the empty node is recorded for insertion.
Insert pseudo code:
void TreeInsert(Tree T, node z)
{
y = NULL;
x = T.root; //x用于找位置指导为空即找到,y为x的父节点
while(x != NULL){
y = x;
if(z.key<x.key){
x = x.left;
}else{
x = x.right;
}
}
z.p = y;
if(y == NULL){
// 树为空
T.root = z;
}
else if (z.key<y.key){
y.left = z;
}
else{
y.right = z;
}
}
13 is inserted below 15:
2.2. Delete: There
are three situations:
a) Simple situation: there is only one child or no children, thenReplace with another child or NULLz
b) Complicated situation: There are two children, divided into two situations, whether the right child is the successor or not.
Purpose: Replacement z
is its successor y
( z
minimum value greater than )
b1) The right child is his successor:y
Replace with successorz
The right child is the successor (greater than his minimum), indicating that the right child has no left child.
b2) The right child is not his successor:With a successor y
in place the right of the child y
, y
instead ofz
Fake code:
// v数代替u树的小函数
void Transplant(Tree T, node u,node v)
{
if(u.p = NULL) t.root = v;
else if(u = u.p.left) u.p.left = v;
else u.p.right = v;
if(v != NULL) v.p = u.p;
}
Delete node function:
**Reference: **Introduction to Algorithm: Section 12.2 Query Binary Search Tree and Section 12.3 Insert and Delete
to sum up:
1. The nature of the binary search tree: all nodes on the left subtree are smaller than the root node (the right subtree is greater than).
2. Predecessor and successor refer to the nodes after in-order traversal.
3. The successor (predecessor) of the binary search tree is the smallest (largest) node greater than (less than) the node.
4. The newly inserted node in the binary search tree is always a leaf node, starting from the root node and comparing to the empty leaf node. (It is not the same as inserting a node in the heap. The heap is placed at the end of the heap first, and then compared with the root node, it moves up if it is larger.
5. Delete from the binary search tree: if there is only one child or no child, replace the deleted node with another child or NULL z
; if there are two children, replace the deleted node with its successor.
6. The time of basic operations (insert, delete, predecessor, successor, maximum, minimum) on the binary search tree is proportional to the height of the tree, because the expected height is lg(n), therefore,Average running timeIs lg(n).
7. Red and black treesWorst running timeIs lg(n).