advanced data structures
play

Advanced Data Structures Lecturer: Shi Li Department of Computer - PowerPoint PPT Presentation

CSE 431/531: Algorithm Analysis and Design (Spring 2020) Advanced Data Structures Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Outline Heap: Concrete Data Structure for Priority Queue 1 Self-Balancing


  1. Binary Search Trees: Insertition 8 3 10 1 6 14 4 7 13 20/39

  2. Binary Search Trees: Insertition 8 5 3 10 1 6 14 4 7 13 20/39

  3. Binary Search Trees: Insertition 8 5 3 10 1 6 14 4 7 13 20/39

  4. Binary Search Trees: Insertition 8 5 3 10 1 6 14 4 7 13 20/39

  5. Binary Search Trees: Insertition 8 5 3 10 1 6 14 4 7 13 20/39

  6. Binary Search Trees: Insertition 8 5 3 10 1 6 14 4 7 13 20/39

  7. Binary Search Trees: Insertition 8 3 10 1 6 14 4 7 13 5 20/39

  8. 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

  9. Binary Search Trees: Deletion 20 8 3 10 1 5 14 2 4 7 13 6 22/39

  10. Binary Search Trees: Deletion 20 8 3 10 1 5 14 2 4 7 13 6 22/39

  11. Binary Search Trees: Deletion 20 8 3 10 1 5 14 2 4 7 13 6 22/39

  12. Binary Search Trees: Deletion 20 8 7 3 10 1 5 14 2 4 7 13 6 22/39

  13. Binary Search Trees: Deletion 20 8 7 3 10 1 5 14 2 4 7 6 13 22/39

  14. Binary Search Trees: Rank 23/39

  15. 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

  16. 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

  17. Running Time for Operations each operation takes time O ( d ) . d = depth of tree best case: worst case: 25/39

  18. Running Time for Operations each operation takes time O ( d ) . d = depth of tree best case: d = Θ(lg n ) worst case: 25/39

  19. 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

  20. 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

  21. Self-Balancing BST: automatically keep the height of tree small 26/39

  22. Self-Balancing BST: automatically keep the height of tree small AVL tree red-black tree Splay tree Treap ... 26/39

  23. Self-Balancing BST: automatically keep the height of tree small AVL tree red-black tree Splay tree Treap ... 26/39

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. Depth of AVL tree f ( d ) : minimum number of nodes in an AVL tree of depth d f ( d ) = 2 Θ( d ) 30/39

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. Maintain Balance Property 32/39

  40. Maintain Balance Property A : the deepest node such that the balance property is not satisfied after insertion A 32/39

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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

  50. 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

  51. 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

  52. 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

  53. 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

  54. 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

  55. 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

  56. 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

  57. 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

  58. 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