SLIDE 1 BST remove
7 1 4 11 35 3 17 2 6 22 42 15
1 NULL remove(2) remove(4) remove(17) 15 or 22 1, 2, 3, 4, 6, 7, 11, 15, 17, 22, 35, 42 predecessor successor Inorder traversal T wo child remove:
min of right subtree
- Equivalent inorder predecessor
- r successor
SLIDE 2
BST remove
7 1 4 11 35 3 17 2 6 22 42 15
void remove(Node *& cRoot, const K & key){ Node *& target = find(cRoot, key); doRemoval(target); } 2 So we can remove root L R key Node to remove By reference By copy Get a reference to the pointer to the node to remove. Then we can update node that points to target.
SLIDE 3
BST remove
7 1 4 11 35 3 17 2 6 22 42 15
void doRemoval(Node *& cRoot){ if ((cRoot->left != NULL) && (cRoot->right != NULL)) ____________ChildRemove(cRoot); else ____________ChildRemove(cRoot); }
3 T wo ZeroOne 3, 7, 17,25 1, 2,4,6,11,15,22,42
SLIDE 4
BST remove
7 1 4 11 35 3 17 2 6 22 42 15
void zeroOneChildRemove(Node *& cRoot){ Node * temp = cRoot; if (cRoot->left == NULL) cRoot = cRoot->right; else: cRoot = cRoot->left; delete temp; }
4 1 What about remove(6)? Same code works since right is NULL So we replace the 4->6 with 4->NULL
SLIDE 5
BST remove
7 1 4 11 35 3 17 2 6 22 42 15
void twoChildRemove(Node *& cRoot){ Node *& rmc = rightMostChild(croot->left); cRoot->key = rmc->key; doRemoval(rmc); }
5 rmc 15 remove(17) 15 Overwrite key 17 w/ 15 Delete old node with key 15
SLIDE 6
BST remove
7 1 4 11 35 3 17 2 6 22 42 15
void rightMostChild(Node *& cRoot){ if (croot->right == NULL) return cRoot; else: rightMostChild(cRoot->right); }
6 return Node * & 14 rightMostChild(17->left) = 15
SLIDE 7 BST efficiency
7 1 4 11 35 3 17 2 6 22 42 15
Reminder: height(T) is:
- if T is empty
- 1+max{height(TL),heigh(TR)}
The runtime of BST algorithms depend on the height (h) of the tree. The analysis should be in terms
- f the number of nodes (n) the
tree contains. We need a relationship between h and n h ≥ f(n) h ≤ g(n)
7
height(6)=1+max{empty, empty} =1+-1 =0 lower bnd upper bnd
SLIDE 8 BST theory
What is the maximum number
- f nodes in a tree of height h?
What is the minimum height of a tree with n nodes? What is the minimum number
- f nodes in a tree of height h?
What is the maximum height of a tree of height h? Lower bound on h ≥ , upper bound on h ≤
8 N(h)=max nodes
N(h) = 2h+1 − 1 n < 2h+1 = ⇒ log2(n) < h + 1 = ⇒ log2(n) ≤ h h + 1 = ⇒ n ≥ h + 1 h ≤ n − 1 log2(n) n − 1
Upper bound n-1 is bad if we want to search the tree
SLIDE 9 BST shape
The height of a BST depends on the order in which data is inserted: 1,2,3,4,5,6,7 vs 4,2,3,6,7,1,5 How many different ways are there to insert n key into a tree? Average height, over all arrangements of n keys?
9
1 2 3 7 4 3 6 7 2 1 5
How many ways to order n things? n! log(n) Why? There are more ways to get a "bushy" tree than a "long" tree How many ways to get? 1 4,6,2,1,3,5,7 How many ways to get? 4,6,2,3,1,5,7 More than 1 etc
SLIDE 10
BST efficiency
avg case worst case sorted array sorted list find insert delete traverse
10 BST
Θ(n) Θ(n) Θ(n) Θ(n) Θ(n) Θ(n) Θ(n) Θ(log2(n)) Θ(log2(n)) Θ(log2(n)) Θ(log2(n)) Θ(n) Θ(n) Θ(n) Θ(n) Θ(n)
shift shift find find Binary search
SLIDE 11 Tree balance
x h=3 h=6 T T
L R
balance(x)=heigh(x.left) - heigh(x.right) If for all nodes x,
- balance(x) = 0 then perfectly balanced
- |balance(x)|≤1 then tree is “balanced” and height ≤ c log2 n
where c < 2
11 balance = 3 - 6 = -3 No Are BST s guaranteed to be balanced? What we want is a way so that insert/remove keep a tree balanced.
SLIDE 12 Tree rotations
7 4 11 35 3 17 42 22 19 32 12
1 2 3 1 4 Unbalanced 7 3 4 17 11 22 35 32 19 42
Only height of nodes on path to root change Tree remains a BST Where we are going:
- AVL trees are balanced and maintain balance after insertion/removal
- We will use rotations to balance the tree after insert/remove