Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a...
Transcript of Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a...
![Page 1: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/1.jpg)
Binary Search Trees
Antonio Carzaniga
Faculty of InformaticsUniversità della Svizzera italiana
April 9, 2019
![Page 2: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/2.jpg)
Outline
Binary search trees
Randomized binary search trees
![Page 3: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/3.jpg)
Binary Search Tree
A binary search tree implements of a dynamic set
◮ over a totally ordered domain
![Page 4: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/4.jpg)
Binary Search Tree
A binary search tree implements of a dynamic set
◮ over a totally ordered domain
Interface
◮ TREE-INSERT(T, k) adds a key k to the dictionary D
◮ TREE-DELETE(T, k) removes key k from D
◮ TREE-SEARCH(T, x) tells whether D contains a key k
![Page 5: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/5.jpg)
Binary Search Tree
A binary search tree implements of a dynamic set
◮ over a totally ordered domain
Interface
◮ TREE-INSERT(T, k) adds a key k to the dictionary D
◮ TREE-DELETE(T, k) removes key k from D
◮ TREE-SEARCH(T, x) tells whether D contains a key k
◮ tree-walk: INORDER-TREE-WALK(T), etc.
![Page 6: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/6.jpg)
Binary Search Tree
A binary search tree implements of a dynamic set
◮ over a totally ordered domain
Interface
◮ TREE-INSERT(T, k) adds a key k to the dictionary D
◮ TREE-DELETE(T, k) removes key k from D
◮ TREE-SEARCH(T, x) tells whether D contains a key k
◮ tree-walk: INORDER-TREE-WALK(T), etc.
◮ TREE-MINIMUM(T) finds the smallest element in the tree
◮ TREE-MAXIMUM(T) finds the largest element in the tree
![Page 7: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/7.jpg)
Binary Search Tree
A binary search tree implements of a dynamic set
◮ over a totally ordered domain
Interface
◮ TREE-INSERT(T, k) adds a key k to the dictionary D
◮ TREE-DELETE(T, k) removes key k from D
◮ TREE-SEARCH(T, x) tells whether D contains a key k
◮ tree-walk: INORDER-TREE-WALK(T), etc.
◮ TREE-MINIMUM(T) finds the smallest element in the tree
◮ TREE-MAXIMUM(T) finds the largest element in the tree
◮ iteration: TREE-SUCCESSOR(x) and TREE-PREDECESSOR(x) find the successor andpredecessor, respectively, of an element x
![Page 8: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/8.jpg)
Binary Search Tree (2)
Implementation
◮ T represents the tree, which consists of a set of nodes
![Page 9: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/9.jpg)
Binary Search Tree (2)
Implementation
◮ T represents the tree, which consists of a set of nodes
◮ T . root is the root node of tree T
![Page 10: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/10.jpg)
Binary Search Tree (2)
Implementation
◮ T represents the tree, which consists of a set of nodes
◮ T . root is the root node of tree T
Node x
◮ x.parent is the parent of node x
◮ x.key is the key stored in node x
◮ x. left is the left child of node x
◮ x. right is the right child of node x
k k = x.keynode x
x.parent
x. left x.right
![Page 11: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/11.jpg)
Binary Search Tree (3)
![Page 12: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/12.jpg)
Binary Search Tree (3)
12
5 18
2 9 15 19
4 13 17
![Page 13: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/13.jpg)
Binary Search Tree (3)
12
5 18
2 9 15 19
4 13 17
≤ 12
![Page 14: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/14.jpg)
Binary Search Tree (3)
12
5 18
2 9 15 19
4 13 17
≤ 12 ≥ 12
![Page 15: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/15.jpg)
Binary Search Tree (3)
12
5 18
2 9 15 19
4 13 17
≤ 12 ≥ 12
Binary-search-tree property
◮ for all nodes x, y, and z
◮ y ∈ left-subtree(x) ⇒ y.key ≤ x.key
◮ z ∈ right-subtree(x) ⇒ z.key ≥ x.key
![Page 16: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/16.jpg)
In-Order Tree Walk
We want to go through the set of keys in order
12
5 18
2 9 15 19
4 13 17
![Page 17: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/17.jpg)
In-Order Tree Walk
We want to go through the set of keys in order
12
5 18
2 9 15 19
4 13 17
2 4 5 9 12 13 15 17 18 19
![Page 18: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/18.jpg)
In-Order Tree Walk (2)
A recursive algorithm
![Page 19: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/19.jpg)
In-Order Tree Walk (2)
A recursive algorithm
INORDER-TREE-WALK(x)
1 if x , NIL
2 INORDER-TREE-WALK(x. left)3 print x.key4 INORDER-TREE-WALK(x.right)
![Page 20: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/20.jpg)
In-Order Tree Walk (2)
A recursive algorithm
INORDER-TREE-WALK(x)
1 if x , NIL
2 INORDER-TREE-WALK(x. left)3 print x.key4 INORDER-TREE-WALK(x.right)
And then we need a “starter” procedure
INORDER-TREE-WALK-START(T)
1 INORDER-TREE-WALK(T .root)
![Page 21: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/21.jpg)
Pre-Order Tree Walk
![Page 22: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/22.jpg)
Pre-Order Tree Walk
PREORDER-TREE-WALK(x)
1 if x , NIL
2 print x.key3 PREORDER-TREE-WALK(x. left)4 PREORDER-TREE-WALK(x.right)
![Page 23: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/23.jpg)
Pre-Order Tree Walk
PREORDER-TREE-WALK(x)
1 if x , NIL
2 print x.key3 PREORDER-TREE-WALK(x. left)4 PREORDER-TREE-WALK(x.right)
12
5 18
2 9 15 19
4 13 17
![Page 24: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/24.jpg)
Pre-Order Tree Walk
PREORDER-TREE-WALK(x)
1 if x , NIL
2 print x.key3 PREORDER-TREE-WALK(x. left)4 PREORDER-TREE-WALK(x.right)
12
5 18
2 9 15 19
4 13 17
12 5 2 4 9 18 15 13 17 19
![Page 25: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/25.jpg)
Post-Order Tree Walk
![Page 26: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/26.jpg)
Post-Order Tree Walk
POSTORDER-TREE-WALK(x)
1 if x , NIL
2 POSTORDER-TREE-WALK(x. left)3 POSTORDER-TREE-WALK(x.right)4 print x.key
![Page 27: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/27.jpg)
Post-Order Tree Walk
POSTORDER-TREE-WALK(x)
1 if x , NIL
2 POSTORDER-TREE-WALK(x. left)3 POSTORDER-TREE-WALK(x.right)4 print x.key
12
5 18
2 9 15 19
4 13 17
![Page 28: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/28.jpg)
Post-Order Tree Walk
POSTORDER-TREE-WALK(x)
1 if x , NIL
2 POSTORDER-TREE-WALK(x. left)3 POSTORDER-TREE-WALK(x.right)4 print x.key
12
5 18
2 9 15 19
4 13 17
4 2 9 5 13 17 15 19 18 12
![Page 29: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/29.jpg)
Reverse-Order Tree Walk
![Page 30: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/30.jpg)
Reverse-Order Tree Walk
REVERSE-ORDER-TREE-WALK(x)
1 if x , NIL
2 REVERSE-ORDER-TREE-WALK(x.right)3 print x.key4 REVERSE-ORDER-TREE-WALK(x. left)
![Page 31: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/31.jpg)
Reverse-Order Tree Walk
REVERSE-ORDER-TREE-WALK(x)
1 if x , NIL
2 REVERSE-ORDER-TREE-WALK(x.right)3 print x.key4 REVERSE-ORDER-TREE-WALK(x. left)
12
5 18
2 9 15 19
4 13 17
![Page 32: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/32.jpg)
Reverse-Order Tree Walk
REVERSE-ORDER-TREE-WALK(x)
1 if x , NIL
2 REVERSE-ORDER-TREE-WALK(x.right)3 print x.key4 REVERSE-ORDER-TREE-WALK(x. left)
12
5 18
2 9 15 19
4 13 17
19 18 17 15 13 12 9 5 4 2
![Page 33: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/33.jpg)
Complexity of Tree Walks
![Page 34: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/34.jpg)
Complexity of Tree Walks
The general recurrence is
T(n) = T(nL) + T(n − nL − 1) + Θ(1)
![Page 35: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/35.jpg)
Complexity of Tree Walks
The general recurrence is
T(n) = T(nL) + T(n − nL − 1) + Θ(1)
INORDER-TREE-WALK Θ(n)PREORDER-TREE-WALK Θ(n)POSTORDER-TREE-WALK Θ(n)REVERSE-ORDER-TREE-WALK Θ(n)
![Page 36: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/36.jpg)
Complexity of Tree Walks
The general recurrence is
T(n) = T(nL) + T(n − nL − 1) + Θ(1)
INORDER-TREE-WALK Θ(n)PREORDER-TREE-WALK Θ(n)POSTORDER-TREE-WALK Θ(n)REVERSE-ORDER-TREE-WALK Θ(n)
We could prove this using the substitution method
![Page 37: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/37.jpg)
Complexity of Tree Walks
The general recurrence is
T(n) = T(nL) + T(n − nL − 1) + Θ(1)
INORDER-TREE-WALK Θ(n)PREORDER-TREE-WALK Θ(n)POSTORDER-TREE-WALK Θ(n)REVERSE-ORDER-TREE-WALK Θ(n)
We could prove this using the substitution method
Can we do better?
![Page 38: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/38.jpg)
Complexity of Tree Walks
The general recurrence is
T(n) = T(nL) + T(n − nL − 1) + Θ(1)
INORDER-TREE-WALK Θ(n)PREORDER-TREE-WALK Θ(n)POSTORDER-TREE-WALK Θ(n)REVERSE-ORDER-TREE-WALK Θ(n)
We could prove this using the substitution method
Can we do better? No!
◮ the length of the output is Θ(n)
![Page 39: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/39.jpg)
Minimum and Maximum Keys
![Page 40: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/40.jpg)
Minimum and Maximum Keys
Recall the binary-search-tree property
◮ for all nodes x, y, and z
◮ y ∈ left-subtree(x) ⇒ y.key ≤ x.key
◮ z ∈ right-subtree(x) ⇒ z.key ≥ x.key
![Page 41: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/41.jpg)
Minimum and Maximum Keys
Recall the binary-search-tree property
◮ for all nodes x, y, and z
◮ y ∈ left-subtree(x) ⇒ y.key ≤ x.key
◮ z ∈ right-subtree(x) ⇒ z.key ≥ x.key
So, the minimum key is in all the way to the left
◮ similarly, the maximum key is all the way to the right
TREE-MINIMUM(x)
1 while x. left , NIL
2 x = x. left3 return x
TREE-MAXIMUM(x)
1 while x.right , NIL
2 x = x.right3 return x
![Page 42: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/42.jpg)
Successor and Predecessor
Given a node x, find the node containing the next key value
![Page 43: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/43.jpg)
Successor and Predecessor
Given a node x, find the node containing the next key value
12
5 18
2 9 15 19
4 13 17
![Page 44: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/44.jpg)
Successor and Predecessor
Given a node x, find the node containing the next key value
12
5 18
2 9 15 19
4 13 17
![Page 45: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/45.jpg)
Successor and Predecessor
Given a node x, find the node containing the next key value
12
5 18
2 9 15 19
4 13 17
![Page 46: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/46.jpg)
Successor and Predecessor
Given a node x, find the node containing the next key value
12
5 18
2 9 15 19
4 13 17
![Page 47: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/47.jpg)
Successor and Predecessor
Given a node x, find the node containing the next key value
12
5 18
2 9 15 19
4 13 17
![Page 48: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/48.jpg)
Successor and Predecessor
Given a node x, find the node containing the next key value
12
5 18
2 9 15 19
4 13 17
The successor of x is theminimum of the right subtree of x, if that exists
![Page 49: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/49.jpg)
Successor and Predecessor
Given a node x, find the node containing the next key value
12
5 18
2 9 15 19
4 13 17
The successor of x is theminimum of the right subtree of x, if that exists
![Page 50: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/50.jpg)
Successor and Predecessor
Given a node x, find the node containing the next key value
12
5 18
2 9 15 19
4 13 17
The successor of x is theminimum of the right subtree of x, if that exists
![Page 51: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/51.jpg)
Successor and Predecessor
Given a node x, find the node containing the next key value
12
5 18
2 9 15 19
4 13 17
The successor of x is theminimum of the right subtree of x, if that exists
![Page 52: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/52.jpg)
Successor and Predecessor
Given a node x, find the node containing the next key value
12
5 18
2 9 15 19
4 13 17
The successor of x is theminimum of the right subtree of x, if that exists
![Page 53: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/53.jpg)
Successor and Predecessor
Given a node x, find the node containing the next key value
12
5 18
2 9 15 19
4 13 17
The successor of x is theminimum of the right subtree of x, if that exists
Otherwise it is the first ancestor a of x such that x falls in the left subtree of a
![Page 54: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/54.jpg)
Successor and Predecessor(2)
TREE-SUCCESSOR(x)
1 if x.right , NIL
2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y
![Page 55: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/55.jpg)
Successor and Predecessor(2)
TREE-SUCCESSOR(x)
1 if x.right , NIL
2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y
12
5 18
2 9 15 19
4 13 17
![Page 56: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/56.jpg)
Successor and Predecessor(2)
TREE-SUCCESSOR(x)
1 if x.right , NIL
2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y
12
5 18
2 9 15 19
4 13 17
![Page 57: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/57.jpg)
Successor and Predecessor(2)
TREE-SUCCESSOR(x)
1 if x.right , NIL
2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y
12
5 18
2 9 15 19
4 13 17
![Page 58: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/58.jpg)
Successor and Predecessor(2)
TREE-SUCCESSOR(x)
1 if x.right , NIL
2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y
12
5 18
2 9 15 19
4 13 17
![Page 59: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/59.jpg)
Successor and Predecessor(2)
TREE-SUCCESSOR(x)
1 if x.right , NIL
2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y
12
5 18
2 9 15 19
4 13 17
![Page 60: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/60.jpg)
Successor and Predecessor(2)
TREE-SUCCESSOR(x)
1 if x.right , NIL
2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y
12
5 18
2 9 15 19
4 13 17
![Page 61: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/61.jpg)
Successor and Predecessor(2)
TREE-SUCCESSOR(x)
1 if x.right , NIL
2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y
12
5 18
2 9 15 19
4 13 17
![Page 62: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/62.jpg)
Successor and Predecessor(2)
TREE-SUCCESSOR(x)
1 if x.right , NIL
2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y
12
5 18
2 9 15 19
4 13 17
![Page 63: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/63.jpg)
Successor and Predecessor(2)
TREE-SUCCESSOR(x)
1 if x.right , NIL
2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y
12
5 18
2 9 15 19
4 13 17
![Page 64: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/64.jpg)
Successor and Predecessor(2)
TREE-SUCCESSOR(x)
1 if x.right , NIL
2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y
12
5 18
2 9 15 19
4 13 17
![Page 65: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/65.jpg)
Search
![Page 66: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/66.jpg)
Search
Binary search (thus the name of the tree)
![Page 67: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/67.jpg)
Search
Binary search (thus the name of the tree)
TREE-SEARCH(x, k)
1 if x = NIL or k = x.key2 return x3 if k < x.key4 return TREE-SEARCH(x. left, k)5 else return TREE-SEARCH(x.right, k)
![Page 68: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/68.jpg)
Search
Binary search (thus the name of the tree)
TREE-SEARCH(x, k)
1 if x = NIL or k = x.key2 return x3 if k < x.key4 return TREE-SEARCH(x. left, k)5 else return TREE-SEARCH(x.right, k)
Is this correct?
![Page 69: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/69.jpg)
Search
Binary search (thus the name of the tree)
TREE-SEARCH(x, k)
1 if x = NIL or k = x.key2 return x3 if k < x.key4 return TREE-SEARCH(x. left, k)5 else return TREE-SEARCH(x.right, k)
Is this correct? Yes, thanks to the binary-search-tree property
![Page 70: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/70.jpg)
Search
Binary search (thus the name of the tree)
TREE-SEARCH(x, k)
1 if x = NIL or k = x.key2 return x3 if k < x.key4 return TREE-SEARCH(x. left, k)5 else return TREE-SEARCH(x.right, k)
Is this correct? Yes, thanks to the binary-search-tree property
Complexity?
![Page 71: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/71.jpg)
Search
Binary search (thus the name of the tree)
TREE-SEARCH(x, k)
1 if x = NIL or k = x.key2 return x3 if k < x.key4 return TREE-SEARCH(x. left, k)5 else return TREE-SEARCH(x.right, k)
Is this correct? Yes, thanks to the binary-search-tree property
Complexity?
T(n) = Θ(depth of the tree)
![Page 72: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/72.jpg)
Search
Binary search (thus the name of the tree)
TREE-SEARCH(x, k)
1 if x = NIL or k = x.key2 return x3 if k < x.key4 return TREE-SEARCH(x. left, k)5 else return TREE-SEARCH(x.right, k)
Is this correct? Yes, thanks to the binary-search-tree property
Complexity?
T(n) = Θ(depth of the tree)
T(n) = O(n)
![Page 73: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/73.jpg)
Search (2)
![Page 74: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/74.jpg)
Search (2)
Iterative binary search
![Page 75: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/75.jpg)
Search (2)
Iterative binary search
ITERATIVE-TREE-SEARCH(T, k)
1 x = T .root2 while x , NIL ∧ k , x.key3 if k < x.key4 x = x. left5 else x = x.right6 return x
![Page 76: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/76.jpg)
Insertion
![Page 77: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/77.jpg)
Insertion
12
5 18
2 9 15 19
4 13 17
![Page 78: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/78.jpg)
Insertion
12
5 18
2 9 15 19
4 13 17
Idea
◮ in order to insert x, we search for x (more precisely x.key)
◮ if we don’t find it, we add it where the search stopped
![Page 79: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/79.jpg)
Insertion (2)
TREE-INSERT(T, z)
1 y = NIL
2 x = T .root3 while x , NIL
4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL
10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z
![Page 80: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/80.jpg)
Insertion (2)
TREE-INSERT(T, z)
1 y = NIL
2 x = T .root3 while x , NIL
4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL
10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z
12
5 18
2 9 15
4 13 17
![Page 81: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/81.jpg)
Insertion (2)
TREE-INSERT(T, z)
1 y = NIL
2 x = T .root3 while x , NIL
4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL
10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z
12
5 18
2 9 15
4 13 17
6
![Page 82: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/82.jpg)
Insertion (2)
TREE-INSERT(T, z)
1 y = NIL
2 x = T .root3 while x , NIL
4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL
10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z
12
5 18
2 9 15
4 13 17
6
![Page 83: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/83.jpg)
Insertion (2)
TREE-INSERT(T, z)
1 y = NIL
2 x = T .root3 while x , NIL
4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL
10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z
12
5 18
2 9 15
4 13 17
6
![Page 84: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/84.jpg)
Insertion (2)
TREE-INSERT(T, z)
1 y = NIL
2 x = T .root3 while x , NIL
4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL
10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z
12
5 18
2 9 15
4 13 176
![Page 85: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/85.jpg)
Insertion (2)
TREE-INSERT(T, z)
1 y = NIL
2 x = T .root3 while x , NIL
4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL
10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z
12
5 18
2 9 15
4 13 176
![Page 86: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/86.jpg)
Insertion (2)
TREE-INSERT(T, z)
1 y = NIL
2 x = T .root3 while x , NIL
4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL
10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z
12
5 18
2 9 15
4 13 176
T(n) = Θ(h)
![Page 87: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/87.jpg)
Observation
Both insertion and search operations have complexity h, where h is the heightof the tree
![Page 88: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/88.jpg)
Observation
Both insertion and search operations have complexity h, where h is the heightof the tree
h = O(log n) in the average case
◮ i.e., with a random insertion order
![Page 89: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/89.jpg)
Observation
Both insertion and search operations have complexity h, where h is the heightof the tree
h = O(log n) in the average case
◮ i.e., with a random insertion order
h = O(n) in some particular cases
![Page 90: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/90.jpg)
Observation
Both insertion and search operations have complexity h, where h is the heightof the tree
h = O(log n) in the average case
◮ i.e., with a random insertion order
h = O(n) in some particular cases
◮ i.e., with ordered sequences
![Page 91: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/91.jpg)
Observation
Both insertion and search operations have complexity h, where h is the heightof the tree
h = O(log n) in the average case
◮ i.e., with a random insertion order
h = O(n) in some particular cases
◮ i.e., with ordered sequences
◮ the problem is that the “worst” case is not that uncommon
![Page 92: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/92.jpg)
Observation
Both insertion and search operations have complexity h, where h is the heightof the tree
h = O(log n) in the average case
◮ i.e., with a random insertion order
h = O(n) in some particular cases
◮ i.e., with ordered sequences
◮ the problem is that the “worst” case is not that uncommon
Idea: use randomization to turn all cases in the average case
![Page 93: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/93.jpg)
Randomized Insertion
Idea 1: insert every sequence as a random sequence
![Page 94: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/94.jpg)
Randomized Insertion
Idea 1: insert every sequence as a random sequence
◮ i.e., given A = 〈1, 2, 3, . . . , n〉, insert a random permutation of A
![Page 95: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/95.jpg)
Randomized Insertion
Idea 1: insert every sequence as a random sequence
◮ i.e., given A = 〈1, 2, 3, . . . , n〉, insert a random permutation of A
◮ problem: A is not necessarily known in advance
![Page 96: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/96.jpg)
Randomized Insertion
Idea 1: insert every sequence as a random sequence
◮ i.e., given A = 〈1, 2, 3, . . . , n〉, insert a random permutation of A
◮ problem: A is not necessarily known in advance
Idea 2: we can obtain a random permutation of the input sequence byrandomly alternating two insertion procedures
◮ tail insertion: this is what TREE-INSERT does
![Page 97: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/97.jpg)
Randomized Insertion
Idea 1: insert every sequence as a random sequence
◮ i.e., given A = 〈1, 2, 3, . . . , n〉, insert a random permutation of A
◮ problem: A is not necessarily known in advance
Idea 2: we can obtain a random permutation of the input sequence byrandomly alternating two insertion procedures
◮ tail insertion: this is what TREE-INSERT does
◮ head insertion: for this we need a new procedure TREE-ROOT-INSERT
◮ inserts n in T as if n was inserted as the first element
![Page 98: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/98.jpg)
Randomized Insertion (2)
TREE-RANDOMIZED-INSERT1(T, z)
1 r = uniformly random value from {1, . . . , t.size + 1}2 if r = 13 TREE-ROOT-INSERT(T, z)4 else TREE-INSERT(T, z)
![Page 99: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/99.jpg)
Randomized Insertion (2)
TREE-RANDOMIZED-INSERT1(T, z)
1 r = uniformly random value from {1, . . . , t.size + 1}2 if r = 13 TREE-ROOT-INSERT(T, z)4 else TREE-INSERT(T, z)
Does this really simulate a random permutation?
◮ i.e., with all permutations being equally likely?
![Page 100: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/100.jpg)
Randomized Insertion (2)
TREE-RANDOMIZED-INSERT1(T, z)
1 r = uniformly random value from {1, . . . , t.size + 1}2 if r = 13 TREE-ROOT-INSERT(T, z)4 else TREE-INSERT(T, z)
Does this really simulate a random permutation?
◮ i.e., with all permutations being equally likely?
◮ no, clearly the last element can only go to the top or to the bottom
![Page 101: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/101.jpg)
Randomized Insertion (2)
TREE-RANDOMIZED-INSERT1(T, z)
1 r = uniformly random value from {1, . . . , t.size + 1}2 if r = 13 TREE-ROOT-INSERT(T, z)4 else TREE-INSERT(T, z)
Does this really simulate a random permutation?
◮ i.e., with all permutations being equally likely?
◮ no, clearly the last element can only go to the top or to the bottom
It is true that any node has the same probability of being inserted at the top
![Page 102: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/102.jpg)
Randomized Insertion (2)
TREE-RANDOMIZED-INSERT1(T, z)
1 r = uniformly random value from {1, . . . , t.size + 1}2 if r = 13 TREE-ROOT-INSERT(T, z)4 else TREE-INSERT(T, z)
Does this really simulate a random permutation?
◮ i.e., with all permutations being equally likely?
◮ no, clearly the last element can only go to the top or to the bottom
It is true that any node has the same probability of being inserted at the top
◮ this suggests a recursive application of this same procedure
![Page 103: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/103.jpg)
Randomized Insertion (3)
![Page 104: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/104.jpg)
Randomized Insertion (3)
TREE-RANDOMIZED-INSERT(t, z)
1 if t = NIL
2 return z3 r = uniformly random value from {1, . . . , t.size + 1}4 if r = 1 // Pr[r = 1] = 1/(t.size + 1)5 z.size = t.size + 16 return TREE-ROOT-INSERT(t, z)7 if z.key < t.key8 t. left = TREE-RANDOMIZED-INSERT(t. left, z)9 else t.right = TREE-RANDOMIZED-INSERT(t.right, z)10 t.size = t.size + 111 return t
![Page 105: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/105.jpg)
Randomized Insertion (3)
TREE-RANDOMIZED-INSERT(t, z)
1 if t = NIL
2 return z3 r = uniformly random value from {1, . . . , t.size + 1}4 if r = 1 // Pr[r = 1] = 1/(t.size + 1)5 z.size = t.size + 16 return TREE-ROOT-INSERT(t, z)7 if z.key < t.key8 t. left = TREE-RANDOMIZED-INSERT(t. left, z)9 else t.right = TREE-RANDOMIZED-INSERT(t.right, z)10 t.size = t.size + 111 return t
Looks like this one really simulates a random permutation. . .
![Page 106: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/106.jpg)
Rotation
![Page 107: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/107.jpg)
Rotation
x
b
a
k ≤ a a ≤ k ≤ b k ≥ b
![Page 108: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/108.jpg)
Rotation
x
b
a
k ≤ a a ≤ k ≤ b k ≥ b
x = RIGHT-ROTATE(x)
![Page 109: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/109.jpg)
Rotation
x
b
a
k ≤ a a ≤ k ≤ b k ≥ b
x = RIGHT-ROTATE(x)
x = LEFT-ROTATE(x)
![Page 110: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/110.jpg)
Rotation
xb
a
k ≤ a a ≤ k ≤ b
k ≥ b
xa
b
k ≤ a
a ≤ k ≤ b k ≥ b
LEFT-ROTATE
RIGHT-ROTATE
RIGHT-ROTATE(x)
1 l = x. left2 x. left = l.right3 l.right = x4 return l
LEFT-ROTATE(x)
1 r = x.right2 x.right = r. left3 r. left = x4 return r
![Page 111: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/111.jpg)
Root Insertion
12
5 18
. . . . . . . . . . . .
![Page 112: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/112.jpg)
Root Insertion
12
5 18
. . . . . . . . . . . .
15
![Page 113: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/113.jpg)
Root Insertion
12
5 18
. . . . . . . . . . . .
15
root-insert
1. Recursively insert z at the root of the appropriate subtree (right)
![Page 114: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/114.jpg)
Root Insertion
12
5 15
. . . . . . . . . 18
. . . . . .
1. Recursively insert z at the root of the appropriate subtree (right)
![Page 115: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/115.jpg)
Root Insertion
12
5 15
. . . . . . . . . 18
. . . . . .
left-rotate
1. Recursively insert z at the root of the appropriate subtree (right)
2. Rotate x with z (left-rotate)
![Page 116: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/116.jpg)
Root Insertion
12
5
15
. . . . . .
. . .
18
. . . . . .
1. Recursively insert z at the root of the appropriate subtree (right)
2. Rotate x with z (left-rotate)
![Page 117: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/117.jpg)
Root Insertion (2)
TREE-ROOT-INSERT(x, z)
1 if x = NIL
2 return z3 if z.key < x.key4 x. left = TREE-ROOT-INSERT(x. left, z)5 return RIGHT-ROTATE(x)6 else x.right = TREE-ROOT-INSERT(x.right, z)7 return LEFT-ROTATE(x)
![Page 118: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/118.jpg)
Observation
General strategies to deal with complexity in the worst case
![Page 119: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/119.jpg)
Observation
General strategies to deal with complexity in the worst case
◮ randomization: turns any case into the average case
◮ the worst case is still possible, but it is extremely improbable
![Page 120: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/120.jpg)
Observation
General strategies to deal with complexity in the worst case
◮ randomization: turns any case into the average case
◮ the worst case is still possible, but it is extremely improbable
◮ amortized maintenance: e.g., balancing a BST or resizing a hash table
◮ relatively expensive but “amortized” operations
![Page 121: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/121.jpg)
Observation
General strategies to deal with complexity in the worst case
◮ randomization: turns any case into the average case
◮ the worst case is still possible, but it is extremely improbable
◮ amortized maintenance: e.g., balancing a BST or resizing a hash table
◮ relatively expensive but “amortized” operations
◮ optimized data structures: a self-balanced data structure
◮ guaranteed O(log n) complexity bounds
![Page 122: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/122.jpg)
Deletion
![Page 123: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/123.jpg)
Deletion
23
6
18
15
5 20
2 12 17
31
27
4 10 16
7
![Page 124: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/124.jpg)
Deletion
23
6
18
15
5 20
2 12 17
31
27
4 10 16
7
1. z has no children
![Page 125: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/125.jpg)
Deletion
23
6
18
15
5 20
2 12 17
31
27
4 10 16
7
X
1. z has no children
◮ simply remove z
![Page 126: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/126.jpg)
Deletion
23
6
15
5 20
2 12 17
31
27
4 10 16
7
1. z has no children
◮ simply remove z
![Page 127: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/127.jpg)
Deletion
23
6
15
5 20
2 12 17
31
27
4 10 16
7
1. z has no children
◮ simply remove z
2. z has one child
![Page 128: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/128.jpg)
Deletion
23
6
15
5 20
2 12 17
31
27
4 10 16
7
X
1. z has no children
◮ simply remove z
2. z has one child
◮ remove z
![Page 129: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/129.jpg)
Deletion
6
15
5 20
2 12 17
31
27
4 10 16
7
1. z has no children
◮ simply remove z
2. z has one child
◮ remove z
◮ connect z.parent to z. right
![Page 130: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/130.jpg)
Deletion
6
15
5 20
2 12 17
31
27
4 10 16
7
1. z has no children
◮ simply remove z
2. z has one child
◮ remove z
◮ connect z.parent to z. right
![Page 131: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/131.jpg)
Deletion
6
15
5 20
2 12 17
31
27
4 10 16
7
1. z has no children
◮ simply remove z
2. z has one child
◮ remove z
◮ connect z.parent to z. right
3. z has two children
![Page 132: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/132.jpg)
Deletion
6
15
5 20
2 12 17
31
27
4 10 16
7
X
1. z has no children
◮ simply remove z
2. z has one child
◮ remove z
◮ connect z.parent to z. right
3. z has two children
◮ replace z withy = TREE-SUCCESSOR(z)
◮ remove y (1 child!)
![Page 133: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/133.jpg)
Deletion
15
6 20
2 12 17
31
27
4 10 16
7
1. z has no children
◮ simply remove z
2. z has one child
◮ remove z
◮ connect z.parent to z. right
3. z has two children
◮ replace z withy = TREE-SUCCESSOR(z)
◮ remove y (1 child!)
◮ connect y.parent to y. right
![Page 134: Binary Search Trees · Binary Search Tree A binary search tree implements of a dynamic set over a totally ordered domain Interface TREE-INSERT(T,k) adds a key k to the dictionary](https://reader033.fdocuments.net/reader033/viewer/2022052408/5f091b9d7e708231d4254464/html5/thumbnails/134.jpg)
Deletion (2)
TREE-DELETE(T, z)
1 if z. left = NIL or z. right = NIL
2 y = z3 else y = TREE-SUCCESSOR(z)4 if y. left , NIL
5 x = y. left6 else x = y. right7 if x , NIL
8 x.parent = y.parent9 if y.parent = NIL
10 T . root = x11 else if y = y.parent. left12 y.parent. left = x13 else y.parentright = x14 if y , z15 z.key = y.key16 copy any other data from y into z