DataStructure Tree Application
Zhejiang University
Binary Search Tree (BST)
Properties
- Left subTrees’s values are smaller than their roots
- Right subTrees’s values are larger than their roots
- Left and Right subTrees are all BSTs
API
- Position Find(ElementType X, BinTree BST)
- Position FindMin(BinTree BST)
- Position FindMax(BinTree BST)
- BinTree Insert(ElementType X, BinTree BST)
- BinTree Delete(ElementType X, BinTree BST)
Find
-
Recursion
-
Loop
1
2
3
4
5
6
7
8
9while(BST){
if(X > BST->Data)
BST = BST->Right;
else if(X < BST->Data)
BST = BST->Left;
else
return BST;
}
return NULL;
Insert
Very Similar to Find. However, we should store the location of the root!
1 | if(!BST){ |
Delete
Three Conditions
- Leaf
- Only one child
- Two Cildren
Two Children
The main idea is to convert this condition into the other 2 simpler ones above.
Methods
- Replace it with the smallest in the left and convert it into deleting the smallest one in left (Only one child)
- Replace it with the largest in the right and convert it into deleting the largest one in the right (Only one child)
Balanced Binary Tree
Balance Factor: BF(T) = h(left) - h(right)
For a Balanced Binary Tree (AVL tree), for every node in this tree the difference between any left and right subtree is no larger than 1.
What is the least number of nodes for a Balanced Binary Tree?
Fibonacci sequence!
Adjusting Balance Binary Tree
- Right right rotation
- Because the trouble node is on the right subtree of the right subtree of the detector, this is called RR insert.
- Left left rotation
- Left right rotation
- Right left rotation
Remark: When two detector-troublenode appear, we should handle the pair below the tree. This may also solve the upper problem.