Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Data Structures By: Nikhil Bhargava. Outline Introduction to ADT. Introduction to ADT. Binary Search Tree (BST) Binary Search Tree (BST) –Definition.

Similar presentations


Presentation on theme: "Advanced Data Structures By: Nikhil Bhargava. Outline Introduction to ADT. Introduction to ADT. Binary Search Tree (BST) Binary Search Tree (BST) –Definition."— Presentation transcript:

1 Advanced Data Structures By: Nikhil Bhargava

2 Outline Introduction to ADT. Introduction to ADT. Binary Search Tree (BST) Binary Search Tree (BST) –Definition. –Insertion, Deletion and Search. –Performance. Binary Heaps. Binary Heaps. Binomial Trees. Binomial Trees. Binomial Heaps. Binomial Heaps. Red-Black Trees Red-Black Trees –Definition. –Addition and Deletion. Skip Lists. Skip Lists. 1/14/2016 Copyright © 2009. All Rights Reserved by author2

3 Introduction What’s the need of ADT? What’s the need of ADT? Simply store everything in a big array. Simply store everything in a big array. Be happy Be happy Problem Problem –Search for a number k in a set of N numbers Solution Solution –Store numbers in an array of size N. –Iterate through array until find k. –Number of checks Best Case : 1 (K=15) Best Case : 1 (K=15) Worst case : N (k=27) Worst case : N (k=27) Average Case : N/2 Average Case : N/2 1/14/2016 Copyright © 2009. All Rights Reserved by author3

4 Introduction Better solution Better solution –Store numbers in a Binary Search Tree (BST). –Search tree until find k. –Number of checks. Best case: 1 (k=15) Best case: 1 (k=15) Worst case: log 2 N (k=27) Worst case: log 2 N (k=27) Average case: (log 2 N) / 2 Average case: (log 2 N) / 2 1/14/2016 Copyright © 2009. All Rights Reserved by author4

5 Analysis What’s the difference b/w both the sols. What’s the difference b/w both the sols. –(N) vs. (log 2 N). 1/14/2016 Copyright © 2009. All Rights Reserved by author5

6 Analysis Assume Assume –N=1,000,000,000 1 billion (Walmart transactions in 100 days) 1 billion (Walmart transactions in 100 days) –1 GHz processor=10 9 cycles per second. Solution 1 (10 cycles per check) Solution 1 (10 cycles per check) –Worst case: 1 billion checks = 10 seconds Solution 2 (100 cycles per check) Solution 2 (100 cycles per check) –Worst case: 30 checks = 0.000003 seconds 1/14/2016 Copyright © 2009. All Rights Reserved by author6

7 Moral of the story!!! Choosing most appropriate data structure makes the system efficient, fast and cost effective. Choosing most appropriate data structure makes the system efficient, fast and cost effective. Appropriate data structures ease design and improve performance. Appropriate data structures ease design and improve performance. Designing appropriate data structure and finding associated algorithms for a problem is a major challenge. Designing appropriate data structure and finding associated algorithms for a problem is a major challenge. 1/14/2016 Copyright © 2009. All Rights Reserved by author7

8 Binary Search Tree (BST) A BST is a binary tree storing keys (or key- value entries) at its internal nodes and satisfying the following property: A BST is a binary tree storing keys (or key- value entries) at its internal nodes and satisfying the following property: –Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) ≤ key(v) ≤ key(w) External nodes do not store items. External nodes do not store items. 1/14/2016 Copyright © 2009. All Rights Reserved by author8

9 Binary Search Tree (BST) An in-order traversal of a binary search trees visits the keys in increasing order. An in-order traversal of a binary search trees visits the keys in increasing order. 1/14/2016 Copyright © 2009. All Rights Reserved by author9

10 BST – Search Operation To search for a key k, we trace a downward path starting at the root, To search for a key k, we trace a downward path starting at the root, The next node visited depends on the outcome of the comparison of k with the key of the current node. The next node visited depends on the outcome of the comparison of k with the key of the current node. If we reach a leaf, the key is not found and we return null. If we reach a leaf, the key is not found and we return null. 1/14/2016 Copyright © 2009. All Rights Reserved by author10

11 BST – Search Operation Algorithm TreeSearch(k, v) if T.isExternal (v) return v if k < key(v) return TreeSearch(k, T.left(v)) else if k = key(v) return v else { k > key(v) } return TreeSearch(k, T.right(v)) 1/14/2016 Copyright © 2009. All Rights Reserved by author11

12 BST – Search Operation For e.g find (4) For e.g find (4) Call TreeSearch(4,root) 1/14/2016 Copyright © 2009. All Rights Reserved by author12

13 BST – Insert Operation To perform operation insert(k, o), we search for key k (using TreeSearch) To perform operation insert(k, o), we search for key k (using TreeSearch) Assume k is not already in the tree, and let w be the leaf reached by the Search. Assume k is not already in the tree, and let w be the leaf reached by the Search. We insert k at node w and expand w into an internal node. We insert k at node w and expand w into an internal node. For e.g. insert (5) For e.g. insert (5) 1/14/2016 Copyright © 2009. All Rights Reserved by author13

14 BST – Insert Operation 1/14/2016 Copyright © 2009. All Rights Reserved by author14

15 BST – Delete Operation To perform operation remove(k), we search for key k. To perform operation remove(k), we search for key k. Assume key k is in the tree, and let v be the node storing k. Assume key k is in the tree, and let v be the node storing k. If node v has a leaf child w, we remove v and w from the tree with operation removeExternal(w), which removes w and its parent. If node v has a leaf child w, we remove v and w from the tree with operation removeExternal(w), which removes w and its parent. For e.g. remove (4) For e.g. remove (4) 1/14/2016 Copyright © 2009. All Rights Reserved by author15

16 BST – Delete Operation 1/14/2016 Copyright © 2009. All Rights Reserved by author16

17 BST – Delete Operation We consider the case where the key k to be removed is stored at a node v whose children are both internal We consider the case where the key k to be removed is stored at a node v whose children are both internal –we find the internal node w that follows v in an inorder traversal. –we copy key(w) into node v –we remove node w and its left child z (which must be a leaf) by means of operation removeExternal(z). –Add key to inorder successor of w. –For e.g remove (3) 1/14/2016 Copyright © 2009. All Rights Reserved by author17

18 BST – Delete Operation 1/14/2016 Copyright © 2009. All Rights Reserved by author18

19 BST – Delete Operation 1/14/2016 Copyright © 2009. All Rights Reserved by author19

20 BST - Performance Consider a dictionary with n items implemented by means of a binary search tree of height h. Consider a dictionary with n items implemented by means of a binary search tree of height h. –the space used is O(n). –methods find, insert and remove take O(h) time. The height h is O(n) in the worst case and O(log n) in the best case. The height h is O(n) in the worst case and O(log n) in the best case. 1/14/2016 Copyright © 2009. All Rights Reserved by author20

21 BST - Performance 1/14/2016 Copyright © 2009. All Rights Reserved by author21

22 Binary Heap: Definition Binary heap. Binary heap. –Almost complete binary tree. filled on all levels, except last, where filled from left to right. filled on all levels, except last, where filled from left to right. –Min-heap ordered. every child greater than (or equal to) parent. every child greater than (or equal to) parent. 06 14 78 18 81 7791 45 5347 64 84 99 83 1/14/2016 Copyright © 2009. All Rights Reserved by author22

23 Binary Heap: Properties Properties. Properties. –Min element is in root. –Heap with N elements has height =  log 2 N . 6 14 78 18 81 7791 45 5347 64 84 99 83 N = 14 Height = 3 1/14/2016 Copyright © 2009. All Rights Reserved by author23

24 Binary Heaps: Array Impl. Implementing binary heaps. Implementing binary heaps. –Use an array: no need for explicit parent or child pointers. Parent(i) =  i/2  Parent(i) =  i/2  Left(i) = 2i Left(i) = 2i Right(i) = 2i + 1 Right(i) = 2i + 1 06 14 78 18 81 7791 45 5347 64 84 99 83 1 2 3 4 56 7 8910 11 12 13 14 1/14/2016 Copyright © 2009. All Rights Reserved by author 24

25 Binary Heap: Insertion Insert element x into heap. Insert element x into heap. –Insert into next available slot. –Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence Peter principle: nodes rise to level of incompetence 06 14 78 18 81 7791 45 5347 64 84 99 83 42 next free slot 1/14/2016 Copyright © 2009. All Rights Reserved by author25

26 Binary Heap: Insertion Insert element x into heap. Insert element x into heap. –Insert into next available slot. –Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence Peter principle: nodes rise to level of incompetence 06 14 78 18 81 7791 45 5347 64 84 99 83 42 swap with parent 1/14/2016 Copyright © 2009. All Rights Reserved by author26

27 Binary Heap: Insertion Insert element x into heap. Insert element x into heap. –Insert into next available slot. –Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence Peter principle: nodes rise to level of incompetence 06 14 78 18 81 7791 45 4247 64 84 99 83 42 53 swap with parent 1/14/2016 Copyright © 2009. All Rights Reserved by author27

28 Binary Heap: Insertion Insert element x into heap. Insert element x into heap. –Insert into next available slot. –Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence Peter principle: nodes rise to level of incompetence –O(log N) operations. 06 14 78 18 81 7791 42 4547 64 84 99 83 53 stop: heap ordered 1/14/2016 Copyright © 2009. All Rights Reserved by author28

29 Binary Heap: Decrease Key Decrease key of element x to k. Decrease key of element x to k. –Bubble up until it's heap ordered. –O(log N) operations. 06 14 78 18 81 7791 42 4547 64 84 99 83 53 1/14/2016 Copyright © 2009. All Rights Reserved by author29

30 Binary Heap: Delete Min Delete minimum element from heap. Delete minimum element from heap. –Exchange root with rightmost leaf. –Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted. power struggle principle: better subordinate is promoted. 06 14 78 18 81 7791 42 4547 64 84 99 83 53 1/14/2016 Copyright © 2009. All Rights Reserved by author30

31 Binary Heap: Delete Min Delete minimum element from heap. Delete minimum element from heap. –Exchange root with rightmost leaf. –Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted. power struggle principle: better subordinate is promoted. 53 14 78 18 81 7791 42 4547 64 84 99 83 06 1/14/2016 Copyright © 2009. All Rights Reserved by author31

32 Binary Heap: Delete Min Delete minimum element from heap. Delete minimum element from heap. –Exchange root with rightmost leaf. –Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted. power struggle principle: better subordinate is promoted. 53 14 78 18 81 7791 42 4547 64 84 99 83 exchange with left child 1/14/2016 Copyright © 2009. All Rights Reserved by author32

33 Binary Heap: Delete Min Delete minimum element from heap. Delete minimum element from heap. –Exchange root with rightmost leaf. –Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted. power struggle principle: better subordinate is promoted. 14 53 78 18 81 7791 42 4547 64 84 99 83 exchange with right child 1/14/2016 Copyright © 2009. All Rights Reserved by author33

34 Binary Heap: Delete Min Delete minimum element from heap. Delete minimum element from heap. –Exchange root with rightmost leaf. –Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted. power struggle principle: better subordinate is promoted. –O(log N) operations. 14 18 78 53 81 7791 42 4547 64 84 99 83 stop: heap ordered 1/14/2016 Copyright © 2009. All Rights Reserved by author34

35 Binary Heap: Heapsort Heapsort. Heapsort. –Insert N items into binary heap. –Perform N delete-min operations. –O(N log N) sort. –No extra storage. 1/14/2016 Copyright © 2009. All Rights Reserved by author35

36 H1H1 H2H2 14 78 18 81 7791 11 6253 64 84 99 41 Binary Heap: Union Combine two binary heaps H 1 and H 2 into one heap. Combine two binary heaps H 1 and H 2 into one heap. No easy solution. No easy solution. –  (N) operations apparently required. Can support fast union with fancier heaps. Can support fast union with fancier heaps. 1/14/2016 Copyright © 2009. All Rights Reserved by author36

37 Binomial Tree Binomial tree. Binomial tree. –Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 1/14/2016 Copyright © 2009. All Rights Reserved by author37

38 Binomial Tree Useful properties of order k binomial tree B k Useful properties of order k binomial tree B k –Number of nodes = 2 k. –Height = k. –Degree of root = k. –Deleting root yields binomial trees B k-1, …, B 0. Proof. Proof. –By induction on k. B0B0 B1B1 B2B2 B3B3 B4B4 B1B1 BkBk B k +1 B2B2 B0B0 1/14/2016 Copyright © 2009. All Rights Reserved by author38

39 Binomial Tree A property useful for naming the data structure. A property useful for naming the data structure. –B k has nodes at depth i. B4B4 depth 2 depth 3 depth 4 depth 0 depth 1 1/14/2016 Copyright © 2009. All Rights Reserved by author39

40 Binomial Heap Binomial heap (Vuillemin, 1978). Binomial heap (Vuillemin, 1978). –Sequence of binomial trees that satisfy binomial heap property. each tree is min-heap ordered. each tree is min-heap ordered. 0 or 1 binomial tree of order k. 0 or 1 binomial tree of order k. B4B4 B0B0 B1B1 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 1/14/2016 Copyright © 2009. All Rights Reserved by author 40

41 Binomial Heap: Implementation Implementation. Implementation. –Represent trees using left-child, right sibling pointers. three links per node (parent, left, right). three links per node (parent, left, right). –Roots of trees connected with singly linked list. degrees of trees strictly decreasing from left to right. degrees of trees strictly decreasing from left to right. 50 483117 44 10 6 37 318 29 6 37 318 48 3150 10 4417 heap 29 Leftist Power-of-2 Heap Binomial Heap Parent Left Right 1/14/2016 Copyright © 2009. All Rights Reserved by author 41

42 Binomial Heap: Properties Properties of N-node binomial heap. Properties of N-node binomial heap. –Min key contained in root of B 0, B 1,..., B k. –Contains binomial tree B i iff b i = 1 where b n  b 2 b 1 b 0 is binary representation of N. –At most  log 2 N  + 1 binomial trees. –Height   log 2 N . B4B4 B0B0 B1B1 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 N = 19 # trees = 3 height = 4 binary = 10011 1/14/2016 Copyright © 2009. All Rights Reserved by author 42

43 Binomial Heap: Union Create heap H that is union of heaps H' and H''. Create heap H that is union of heaps H' and H''. –"Mergeable heaps." –Easy if H' and H'' are each order k binomial trees. connect roots of H' and H.'' connect roots of H' and H.'' choose smaller key to be root of H. choose smaller key to be root of H. H'' 55 45 32 30 24 2322 50 483117 44 82910 6 H' 1/14/2016 Copyright © 2009. All Rights Reserved by author43

44 Binomial Heap: Union 0011 1001+ 0111 11 1 1 0 1 19 + 7 = 26 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 41 33 28 15 25 712 + 1/14/2016 Copyright © 2009. All Rights Reserved by author 44

45 Binomial Heap: Union 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 41 33 28 15 25 712 + 1/14/2016 Copyright © 2009. All Rights Reserved by author45

46 Binomial Heap: Union 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 + 12 18 12 1/14/2016 Copyright © 2009. All Rights Reserved by author46

47 55 45 32 30 24 2322 50 483117 4 82910 6 37 3 41 33 28 15 25 7 + 12 18 25 37 7 3 18 12 18 12 1/14/2016 Copyright © 2009. All Rights Reserved by author47

48 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 12 + 18 25 37 7 3 41 283325 37 157 3 18 12 18 12 1/14/2016 Copyright © 2009. All Rights Reserved by author48

49 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 + 18 12 41 283325 37 157 3 12 18 25 37 7 3 41 283325 37 157 3 18 12 1/14/2016 Copyright © 2009. All Rights Reserved by author 49

50 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 + 18 12 41 283325 37 157 3 12 18 25 37 7 3 41 283325 37 157 3 55 45 32 30 24 2322 50 483117 44 82910 6 18 12 1/14/2016 Copyright © 2009. All Rights Reserved by author 50

51 Binomial Heap: Union Create heap H that is union of heaps H' and H''. Create heap H that is union of heaps H' and H''. –Analogous to binary addition. Running time O(log N) Running time O(log N) –Proportional to number of trees in root lists  2(  log 2 N  + 1). 0011 1001+ 0111 11 1 1 0 1 19 + 7 = 26 1/14/2016 Copyright © 2009. All Rights Reserved by author 51

52 3 37 618 55 45 32 30 24 2322 50 483117 44 82910 H Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. Delete node with minimum key in binomial heap H. –Find root x with min key in root list of H, and delete –H'  broken binomial trees –H  Union(H', H) Running time O(log N) Running time O(log N) 1/14/2016 Copyright © 2009. All Rights Reserved by author52

53 Binomial Heap: Delete Min Delete node with min key in binomial heap H. Delete node with min key in binomial heap H. –Find root x with min key in root list of H, and delete –H'  broken binomial trees –H  Union(H', H) Running time O(log N) Running time O(log N) 55 45 32 30 24 2322 50 483117 37 618 44 82910 H H' 1/14/2016 Copyright © 2009. All Rights Reserved by author 53

54 3 37 618 55 x 32 30 24 2322 50 483117 44 82910 H Binomial Heap: Decrease Key Decrease key of node x in binomial heap H. Decrease key of node x in binomial heap H. –Suppose x is in binomial tree B k. –Bubble node x up the tree if x is too small. Running time O(log N) Running time O(log N) –Proportional to depth of node x   log 2 N . depth = 3 1/14/2016 Copyright © 2009. All Rights Reserved by author54

55 Binomial Heap: Delete Delete node x in binomial heap H. Delete node x in binomial heap H. –Decrease key of x to - . –Delete min. Running time O(log N) Running time O(log N) 1/14/2016 Copyright © 2009. All Rights Reserved by author55

56 Binomial Heap: Insert Insert a new node x into binomial heap H. Insert a new node x into binomial heap H. –H'  MakeHeap(x) –H  Union(H', H) Running time. O(log N) Running time. O(log N) 3 37 618 55 45 32 30 24 2322 50 483117 44 82910 H x H' 1/14/2016 Copyright © 2009. All Rights Reserved by author56

57 Binomial Heap: Sequence of Inserts Insert a new node x into binomial heap H. Insert a new node x into binomial heap H. –If N =.......0, then only 1 steps. –If N =......01, then only 2 steps. –If N =.....011, then only 3 steps. –If N =....0111, then only 4 steps. 50 483117 44 2910 3 37 6 x 1/14/2016 Copyright © 2009. All Rights Reserved by author57

58 Binomial Heap: Sequence of Inserts Inserting 1 item can take  (log N) time. Inserting 1 item can take  (log N) time. –If N = 11...111, then log 2 N steps. But, inserting sequence of N items takes O(N) time! But, inserting sequence of N items takes O(N) time! –(N/2)(1) + (N/4)(2) + (N/8)(3) +...  2N –Amortized analysis. –Basis for getting most operations down to constant time. operations down to constant time. 1/14/2016 Copyright © 2009. All Rights Reserved by author58

59 Red-Black Tree Popular alternative to the BST tree (Why??). Popular alternative to the BST tree (Why??). Operations take O(log N) time in worst case. Operations take O(log N) time in worst case. Height is at most 2log(N+1). Height is at most 2log(N+1). A red-black tree is a binary search tree with one extra attribute for each node: the color, which is either red or black. A red-black tree is a binary search tree with one extra attribute for each node: the color, which is either red or black. The root is black. The root is black. If node is red, its children must be black. If node is red, its children must be black. Every path from a node to a null reference must contain the same number of black nodes. Every path from a node to a null reference must contain the same number of black nodes. Basic operations to conform with rules are color changes and tree rotations. Basic operations to conform with rules are color changes and tree rotations. 1/14/2016 Copyright © 2009. All Rights Reserved by author59

60 Theorem 1 – In a red-black tree, at least half the nodes on any path from the root to a leaf must be black. Proof – If there is a red node on the path, there must be a corresponding black node. 1/14/2016 Copyright © 2009. All Rights Reserved by author60 Red-Black Tree

61 Theorem 2 – In a red-black tree, no path from any node N, to a leaf is more than twice as long as any other path from N to any other leaf. Proof: By definition, every path from a node to any leaf contains the same number of black nodes. By Theorem1, a least ½ the nodes on any such path are black. Therefore, there can no more than twice as many nodes on any path from N to a leaf as on any other path. Therefore the length of every path is no more than twice as long as any other path. 1/14/2016 Copyright © 2009. All Rights Reserved by author61 Red-Black Tree

62 Theorem 3 – A red-black tree with n internal nodes has height h <= 2 log(n + 1). Proof: Let h be the height of the red-black tree with root x. By Theorem 1, bh(x) >= h/2 From Theorem 1, n >= 2bh(x) - 1 Therefore n >= 2 h/2 – 1 n + 1 >= 2h/2 log(n + 1) >= h/2 2log(n + 1) >= h 1/14/2016 Copyright © 2009. All Rights Reserved by author62 Red-Black Tree

63 Bottom-Up Insertion Cases: Cases: –0: X is the root – color it black. –1: Both parent and uncle are red – color parent and uncle black, color grandparent red, point X to grandparent, check new situation. –2 (zig-zag): Parent is red, but uncle is black. X and its parent are opposite type children – color grandparent red, color X black, rotate left on parent, rotate right on grandparent. –3 (zig-zig): Parent is red, but uncle is black. X and its parent are both left or both right children – color parent black, color grandparent red, rotate right on grandparent. 1/14/2016 Copyright © 2009. All Rights Reserved by author63

64 Top-Down Red-Black Trees In T-Down insertion, the corrections are done while traversing down the tree to the insertion point. In T-Down insertion, the corrections are done while traversing down the tree to the insertion point. When the actual insertion is done, no further corrections are needed, so no need to traverse back up the tree. When the actual insertion is done, no further corrections are needed, so no need to traverse back up the tree. So, T-Down insertion can be done iteratively which is generally faster. So, T-Down insertion can be done iteratively which is generally faster. Insertion is always done as a leaf (as in ordinary BST insertion). Insertion is always done as a leaf (as in ordinary BST insertion). 1/14/2016 Copyright © 2009. All Rights Reserved by author64

65 Process On the way down, when we see a node X that has two red children, we make X red and its two children black. On the way down, when we see a node X that has two red children, we make X red and its two children black. If X’s parent is red, we can apply either the single or double rotation to keep us from having two consecutive red nodes. If X’s parent is red, we can apply either the single or double rotation to keep us from having two consecutive red nodes. X’s parent and the parent’s sibling cannot both be red, since their colors would already have been flipped in that case. X’s parent and the parent’s sibling cannot both be red, since their colors would already have been flipped in that case. 1/14/2016 Copyright © 2009. All Rights Reserved by author65

66 Example: Insert 45 30 70 85 5 60 80 10 90 15 20 50 4055 65 Two red children 1/14/2016 Copyright © 2009. All Rights Reserved by author66

67 Example (Cont.) 30 70 85 5 60 80 10 90 15 20 50 4055 65 flip colors - two red nodes 1/14/2016 Copyright © 2009. All Rights Reserved by author67

68 Example (Cont.): Do a single rotation 30 70 85 5 60 80 10 90 15 20 50 4055 65 1/14/2016 Copyright © 2009. All Rights Reserved by author68

69 Example (Cont.): Now Insert 45 30 70 85 5 60 80 10 90 15 20 50 4055 65 45 1/14/2016 Copyright © 2009. All Rights Reserved by author69

70 Important note Since the parent of the newly inserted node was black, we are done. Since the parent of the newly inserted node was black, we are done. Had the parent of the inserted node been red, one more rotation would have had to be performed. Had the parent of the inserted node been red, one more rotation would have had to be performed. Although red-black trees have slightly weaker balancing properties, their performance in experimentally almost identical to that of AVL trees. Although red-black trees have slightly weaker balancing properties, their performance in experimentally almost identical to that of AVL trees. 1/14/2016 Copyright © 2009. All Rights Reserved by author70

71 Top-Down Deletions Recall that in deleting from a binary search tree, the only nodes which are actually removed are leaves or nodes with exactly one child. Recall that in deleting from a binary search tree, the only nodes which are actually removed are leaves or nodes with exactly one child. Nodes with two children are never removed. Their contents are just replaced. Nodes with two children are never removed. Their contents are just replaced. If the node to be deleted is red, there is no problem - just delete the node. If the node to be deleted is red, there is no problem - just delete the node. If the node to be deleted is black, its removal will violate property. If the node to be deleted is black, its removal will violate property. The solution is to ensure that any node to be deleted is red. The solution is to ensure that any node to be deleted is red. 1/14/2016 Copyright © 2009. All Rights Reserved by author71

72 Deterministic Skip Lists A probabilistically balanced linked list. A probabilistically balanced linked list. Invented in 1986 by William Pugh. Invented in 1986 by William Pugh. Definition: Two elements are linked if there exists at least one link going from one to another. Definition: Two elements are linked if there exists at least one link going from one to another. Definition: The gap size between two elements linked at height h is equal to the number of elements of height h-1 between them. Definition: The gap size between two elements linked at height h is equal to the number of elements of height h-1 between them. 1/14/2016 Copyright © 2009. All Rights Reserved by author72

73 Skip List 3 6 NIL 7 9 12 17 19 21 25 26 3 6 7 9 12 17 19 21 25 26 NIL xtra pointers every eighth item - full structure skip list - same link distribution, random choice 1/14/2016 Copyright © 2009. All Rights Reserved by author73

74 Search time In the deterministic version (a-d): In the deterministic version (a-d): –in a, we need to check at most n nodes –in b, at most  n/2  +1 nodes –in c, at most  n/4  +2 nodes –in general, at most  log N  nodes Efficient search, but impractical insertion and deletion. Efficient search, but impractical insertion and deletion. Not a real time data structure. Not a real time data structure. Probabilistic in nature. Probabilistic in nature. 1/14/2016 Copyright © 2009. All Rights Reserved by author74

75 Levels A node with k forward pointers is called a level k node. A node with k forward pointers is called a level k node. If every (2 i )th node has a pointer 2 i nodes ahead, they have the following distribution: If every (2 i )th node has a pointer 2 i nodes ahead, they have the following distribution: levelpercent 150 225 312.5 …… 1/14/2016 Copyright © 2009. All Rights Reserved by author75

76 Central idea in skip lists Choose levels of nodes randomly, but in the same proportions (as in e). Choose levels of nodes randomly, but in the same proportions (as in e). A node’s i th forward pointer, points to the next node of level i or higher. A node’s i th forward pointer, points to the next node of level i or higher. Insertions and deletions require only local modifications. Insertions and deletions require only local modifications. A node’s level never changes after first being chosen. A node’s level never changes after first being chosen. 1/14/2016 Copyright © 2009. All Rights Reserved by author76

77 Insertion To perform insertion, we must make sure that when a new node of height h is added, it doesn’t create a gap of four heights of h node (in 1-2-3 deterministic skip list). To perform insertion, we must make sure that when a new node of height h is added, it doesn’t create a gap of four heights of h node (in 1-2-3 deterministic skip list). 1/14/2016 Copyright © 2009. All Rights Reserved by author77

78 Data Structure Selection in Programming Tough question (Agree or not !!!). Tough question (Agree or not !!!). Comes with practice and experience. Comes with practice and experience. Remember the golden rule. Remember the golden rule. Data Structure will affect the runtime of your algorithm, but not the result Pick the data structure depending on the problem Pick the data structure depending on the problem –time vs. space tradeoff (e.g. hash tables provide fast search but need many more slots than entries). 1/14/2016 Copyright © 2009. All Rights Reserved by author78

79 Data Structure Selection in Programming Think about these questions before deciding on the right data structure Think about these questions before deciding on the right data structure –What are my end objectives? –What features does the data structure provide? –what are costs (time, space, etc.) associated with them? (Free lunch is only available in a mouse trap!!) – How well does this combination suit the particular algorithm I have in mind? 1/14/2016 Copyright © 2009. All Rights Reserved by author79

80 Some tips… At least 90% of the data structures used in totality are simple arrays. At least 90% of the data structures used in totality are simple arrays. The rest are mainly linked lists or binary trees. The rest are mainly linked lists or binary trees. Very rarely is it worth using a hash or a red black tree. Very rarely is it worth using a hash or a red black tree. If you need to find something with equality only, then hash. If you need to find something with equality only, then hash. If you need to find a range of something (e.g. where ‘x’ between ‘a’ and ‘b’ or where ‘y’ >= ‘z’) then use a tree or skiplist. If you need to find a range of something (e.g. where ‘x’ between ‘a’ and ‘b’ or where ‘y’ >= ‘z’) then use a tree or skiplist. 1/14/2016 Copyright © 2009. All Rights Reserved by author80

81 Some tips… If you need to order a list by some kind of importance and modify the list on the fly, then a priority queue is wanted. If you need to order a list by some kind of importance and modify the list on the fly, then a priority queue is wanted. If you need to solve problems involving some sort of web like structure, then a graph is often wanted. If you need to solve problems involving some sort of web like structure, then a graph is often wanted. If you want to access data by content rather than by id number, there’s nothing like a hash table. If you want to access data by content rather than by id number, there’s nothing like a hash table. If you want to keep data sorted, despite arbitrary inserts and deletes, you are looking at a red black tree. If you want to keep data sorted, despite arbitrary inserts and deletes, you are looking at a red black tree. 1/14/2016 Copyright © 2009. All Rights Reserved by author81

82 References [1] Advanced Data Structures by Goodrich and Tamassia, 2 nd edition. and Tamassia, 2 nd edition. [2] Introduction to Algorithms by Cormen et. al., 2 nd edition. et. al., 2 nd edition. [3] Binary and Binomial Heaps, Princeton university, notes by Kevin Wayne. university, notes by Kevin Wayne. [4] Lecture notes, Dr. S.N Maheshwari, CSE- IIT Delhi. IIT Delhi. 1/14/2016 Copyright © 2009. All Rights Reserved by author82

83 Questions 1/14/201683 Copyright © 2009. All Rights Reserved by author

84 1/14/201684 Copyright © 2009. All Rights Reserved by author


Download ppt "Advanced Data Structures By: Nikhil Bhargava. Outline Introduction to ADT. Introduction to ADT. Binary Search Tree (BST) Binary Search Tree (BST) –Definition."

Similar presentations


Ads by Google