Binary Search Trees: Insertition 8 3 10 1 6 14 4 7 13 20/39
Binary Search Trees: Insertition 8 5 3 10 1 6 14 4 7 13 20/39
Binary Search Trees: Insertition 8 5 3 10 1 6 14 4 7 13 20/39
Binary Search Trees: Insertition 8 5 3 10 1 6 14 4 7 13 20/39
Binary Search Trees: Insertition 8 5 3 10 1 6 14 4 7 13 20/39
Binary Search Trees: Insertition 8 5 3 10 1 6 14 4 7 13 20/39
Binary Search Trees: Insertition 8 3 10 1 6 14 4 7 13 5 20/39
Binary Search Trees: Insertion insert ( v, key ) if key < v.key 1 if v.left = nil then 2 create a new node u 3 u.key ← key, u.left ← nil , u.right ← nil 4 v.left ← u 5 else insert ( v.left, key ) 6 else 7 if v.right = nil then 8 create a new node u 9 u.key ← key, u.left ← nil , u.right ← nil 10 v.right ← u 11 else insert ( v.right, key ) 12 21/39
Binary Search Trees: Deletion 20 8 3 10 1 5 14 2 4 7 13 6 22/39
Binary Search Trees: Deletion 20 8 3 10 1 5 14 2 4 7 13 6 22/39
Binary Search Trees: Deletion 20 8 3 10 1 5 14 2 4 7 13 6 22/39
Binary Search Trees: Deletion 20 8 7 3 10 1 5 14 2 4 7 13 6 22/39
Binary Search Trees: Deletion 20 8 7 3 10 1 5 14 2 4 7 6 13 22/39
Binary Search Trees: Rank 23/39
Binary Search Trees: Rank Need to maintain a field “size” 8 9 3 10 3 5 1 6 14 1 3 2 4 7 13 1 1 1 23/39
Binary Search Trees: Rank rank ( v, key ) if key ≤ v.key 1 if v.left = nil then return 1 2 else return rank ( v.left, key ) 3 else 4 if v.right = nil then return v.size + 1 5 else return v.size − v.right.size + rank ( v.right, key ) 6 24/39
Running Time for Operations each operation takes time O ( d ) . d = depth of tree best case: worst case: 25/39
Running Time for Operations each operation takes time O ( d ) . d = depth of tree best case: d = Θ(lg n ) worst case: 25/39
Running Time for Operations each operation takes time O ( d ) . d = depth of tree best case: d = Θ(lg n ) worst case: d = Θ( n ) 25/39
Running Time for Operations each operation takes time O ( d ) . d = depth of tree best case: d = Θ(lg n ) worst case: d = Θ( n ) 1 2 3 4 4 5 2 6 6 1 3 5 7 7 25/39
Self-Balancing BST: automatically keep the height of tree small 26/39
Self-Balancing BST: automatically keep the height of tree small AVL tree red-black tree Splay tree Treap ... 26/39
Self-Balancing BST: automatically keep the height of tree small AVL tree red-black tree Splay tree Treap ... 26/39
AVL Tree Property of an AVL tree For every node v in the tree, the depths of the left-sub-tree of v and right-sub-tree of v differ by at most 1. 8 3 10 1 6 14 4 7 13 27/39
AVL Tree Property of an AVL tree For every node v in the tree, the depths of the left-sub-tree of v and right-sub-tree of v differ by at most 1. 8 0 vs 2 3 10 1 6 14 4 7 13 27/39
AVL Tree Property of an AVL tree For every node v in the tree, the depths of the left-sub-tree of v and right-sub-tree of v differ by at most 1. 8 0 vs 2 3 10 1 6 14 4 7 13 not balanced 27/39
AVL Tree Property of an AVL tree For every node v in the tree, the depths of the left-sub-tree of v and right-sub-tree of v differ by at most 1. 8 8 0 vs 2 3 10 3 10 1 6 14 1 6 9 14 4 7 13 4 7 13 not balanced balanced 27/39
AVL Tree Property of an AVL tree For every node v in the tree, the depths of the left-sub-tree of v and right-sub-tree of v differ by at most 1. Why does the property guarantee that the height of a tree is O (log n ) ? 28/39
AVL Tree Property of an AVL tree For every node v in the tree, the depths of the left-sub-tree of v and right-sub-tree of v differ by at most 1. Why does the property guarantee that the height of a tree is O (log n ) ? f ( d ) : minimum number of nodes in an AVL tree of depth d 28/39
AVL Tree Property of an AVL tree For every node v in the tree, the depths of the left-sub-tree of v and right-sub-tree of v differ by at most 1. Why does the property guarantee that the height of a tree is O (log n ) ? f ( d ) : minimum number of nodes in an AVL tree of depth d f (0) = 0 , f (1) = 1 , f (2) = 2 , f (3) = 4 , f (4) = 7 · · · 28/39
f ( d ) : minimum number of nodes in an AVL tree of depth d Recursion: f (0) = 0 f (1) = 1 f ( d ) = f ( d − 1) + f ( d − 2) + 1 d ≥ 2 29/39
f ( d ) : minimum number of nodes in an AVL tree of depth d Recursion: f (0) = 0 f (1) = 1 f ( d ) = f ( d − 1) + f ( d − 2) + 1 d ≥ 2 f ( d ) = 2 Θ( d ) 29/39
Depth of AVL tree f ( d ) : minimum number of nodes in an AVL tree of depth d f ( d ) = 2 Θ( d ) 30/39
Depth of AVL tree f ( d ) : minimum number of nodes in an AVL tree of depth d f ( d ) = 2 Θ( d ) If a AVL tree has size n and depth d , then n ≥ f ( d ) 30/39
Depth of AVL tree f ( d ) : minimum number of nodes in an AVL tree of depth d f ( d ) = 2 Θ( d ) If a AVL tree has size n and depth d , then n ≥ f ( d ) Thus, d = O (log n ) 30/39
Property of an AVL tree For every node v in the tree, the depths of the left-sub-tree of v and right-sub-tree of v differ by at most 1. 8 8 0 vs 2 3 10 3 10 1 6 14 1 6 9 14 4 7 13 4 7 13 not balanced balanced 31/39
Property of an AVL tree For every node v in the tree, the depths of the left-sub-tree of v and right-sub-tree of v differ by at most 1. 8 8 0 vs 2 3 10 3 10 1 6 14 1 6 9 14 4 7 13 4 7 13 not balanced balanced How can we maintain the property? 31/39
Property of an AVL tree For every node v in the tree, the depths of the left-sub-tree of v and right-sub-tree of v differ by at most 1. 8 8 0 vs 2 3 10 3 10 1 6 14 1 6 9 14 4 7 13 4 7 13 not balanced balanced How can we maintain the property? Assume we only do insertions; there are no deletions. 31/39
Maintain Balance Property 32/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion A 32/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A A 32/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A A B 32/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 1: we inserted an element to the left-sub-tree of B A B 32/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 1: we inserted an element to the left-sub-tree of B A B A R B R B L 32/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 1: we inserted an element to the left-sub-tree of B A d + 2 d B A R B R B L 32/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 1: we inserted an element to the left-sub-tree of B A d + 2 d B A R d + 1 B R B L 32/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 1: we inserted an element to the left-sub-tree of B A d + 2 d B A R d + 1 d B R B L 32/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 1: we inserted an element to the left-sub-tree of B A B A d + 2 d B B L A R B R A R d + 1 d B R B L 32/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 1: we inserted an element to the left-sub-tree of B d + 2 A B d + 1 A d + 2 d + 1 d B B L A R d d B R A R d + 1 d B R B L 32/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A 33/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 2: we inserted an element to the right-sub-tree of B 33/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 2: we inserted an element to the right-sub-tree of B C : the root of right-sub-tree of B 33/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 2: we inserted an element to the right-sub-tree of B C : the root of right-sub-tree of B A B A R C B L C R C L 33/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 2: we inserted an element to the right-sub-tree of B C : the root of right-sub-tree of B A d + 2 d B A R C B L C R C L 33/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 2: we inserted an element to the right-sub-tree of B C : the root of right-sub-tree of B A d + 2 d B A R d + 1 d C B L C R C L 33/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 2: we inserted an element to the right-sub-tree of B C : the root of right-sub-tree of B A d + 2 d B A R d + 1 d C B L d − 1 d C R C L 33/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 2: we inserted an element to the right-sub-tree of B C : the root of right-sub-tree of B A C d + 2 d B B A A R d + 1 d C B L C R C L A R B L d − 1 d C R C L 33/39
Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion Wlog, we inserted an element to the left-sub-tree of A B : the root of left-sub-tree of A case 2: we inserted an element to the right-sub-tree of B C : the root of right-sub-tree of B d + 2 A C d + 1 d + 1 d + 2 d B B A A R d + 1 d C d − 1 d B L d d C R C L A R B L d − 1 d C R C L 33/39
Recommend
More recommend