Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement.

Similar presentations


Presentation on theme: "Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement."— Presentation transcript:

1 Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement from the slides of Prof. Hsin-Hsi Chen (NTU). Chapter 9 Heap Structures

2 Outline MIN-MAX Heaps Deaps Leftist Trees Binomial Heaps Fibonacci Heaps

3 MIN-MAX Heaps Definition

4 MIN-MAX Heaps Complete binary tree. Set root on a min level. A node x in min level would have smaller key value than all its descendents. (x is a min node.)

5 MIN-MAX Heaps 7 7040 3010915 5045203012 min max min max

6 MIN-MAX Heaps Insertion into a min-max heap

7 7 7040 3010915 5045203012 min max min max 5

8 Insertion into a min-max heap 7 7040 305915 5045203012 min max min max 10

9 Insertion into a min-max heap 5 7040 307915 5045203012 min max min max 10

10 Insertion into a min-max heap Check if it satisfies min heap. (Compare with its parent.) If no, move the key of current parent to current position. If yes, skip.

11 Insertion into a min-max heap Initial Heap={7,70,40,30,9,10, 15,45,50,30,20,12} *n=13 item=80 parent=6 80>10 → verify_max(heap,13,80) 7 70 40 3010915 5045203012 min max min max 80

12 Insertion into a min-max heap Input verify_max(Heap,13,80) grandparent=3 80>40 → heap[13]=heap[3] i=3 grandparent=0 7 70 40 3010915 5045203012 min max min max 80

13 Insertion into a min-max heap Input verify_max(Heap,3,80) → grandparent=null break; heap[3]=80; 7 70 80 3010915 5045203012 min max min max 40

14 Insertion into a min-max heap The time complexity of insertion into a min-max heap with n elements is O(log n). A min-max heap with n elements has O(log n) levels.

15 MIN-MAX Heaps Deletion of min element

16 The smallest element is in the root. We do the deletion as follows: 1. Remove the root node and the node x which is the end of the heap. 2. Reinsert the key of x into the heap.

17 Deletion of min element 7 7040 3010915 5045203012 min max min max

18 Deletion of min element 7040 3010915 50452030 min max min max 12

19 Deletion of min element The reinsertion may have 2 cases: 1. No child. (Only one node in the heap) i i It means the item is the only one element, so it should be at root in heap.

20 Deletion of min element 2. The root has at least one child Find the min value. (Let this be node k.) a. A item.key ≦ heap[k].key i i It means the item is the min element, and the min element should be at root in heap.

21 Deletion of min element b. item.key> heap[k].key and k is child of root. i Since k is in max level, it has no descendants. k k i

22 Deletion of min element c. item.key> heap[k].key and k is grandchild of root. i Example (p>i,): We should make sure the node in max level contains the largest key. Then redo insertion to the subtree with the root in red line. p k k p i

23 Deletion of min element Get the min value of the heap. Move k to the root of this heap. Swap i and p in case 2c.

24 Deletion of min element Deletion of min element of a min-max heap with n elements need O(log n) time. In each iteration, i moves down two levels. Since a min-max heap is a complete binary tree, heap has O(log n) levels.

25 Deaps Definition

26 Deaps Complete binary tree. Either empty or satisfies the properties 1. The root contains no element. 2. The left subtree is a min-heap. 3. The right subtree is a max-heap. 4. If the right subtree is not empty. Let i be any node in left subtree, and j be the corresponding node in the right subtree. If no, choose the parent one. i_key ≦ j_key.

27 Deaps The relation between i and j. 1 23 4 6 5 7 981110 12 Ex1: i=4; j=4+2^(2-1)=6; Ex2: i=9; j=9+2^(3-1)=13; j>12 j=6;

28 Deaps 545 1025840 191530920

29 Deaps Insertion into a deap

30 The insertion steps 1. max_heap(n): Check iff n is a position in the max-heap of the deap. 2. min_partner(n) or max_partner(n) : Compute the min-heap/max-heap node that corresponding to n. ( / ) 3. Compare key of i and j to satisfy deap. 4. min_insert or max_insert.

31 Insertion into a deap 545 1025840 1915309204 j

32 Insertion into a deap 545 1025840 1915309204 j i

33 Insertion into a deap 545 1025840 4153092019

34 Insertion into a deap 445 525840 10153092019

35 Insertion into a deap Step 1. max_heap. Step 2. min_partner. Step 3. Compare key of i and j. Step 4. min_insert or max_insert.

36 Insertion into a deap The time complexity is O(log n) as the height of the deap is O(log n).

37 Deaps Deletion of min element

38 Insertion into a deap The insertion steps 1. Save the last element as temp and remove this node from deap. 2. Find the node with smaller key from the children of removed minimum element and loop down until reaching leaves. 3. Insert temp into the left subtree of deap.

39 Deletion of min element 545 1025840 191530920 temp

40 Deletion of min element 45 1025840 1915309 j temp:20 i

41 Deletion of min element 845 102540 1915309 j i temp:20

42 Deletion of min element 845 1025940 191530 i temp:20

43 Deletion of min element 845 1025940 191530 i temp:20

44 Deletion of min element 845 1025940 19153020

45 Deletion of min element Step 1. Save the min element. Step 2. Find the node with smaller key. Step 3. (Exercise 2).

46 Deletion of min element The time complexity is O(log n) as the height of the deap is O(log n).

47 Leftist Trees Definition

48 Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity. Can meld two leftist tree priority queues in O(log n) time. For any node x in an extended binary tree, let shortest(x) be the length of a shortest path from x to an external node in the subtree rooted at x.

49 Leftist Trees AG BHCI EDFJ Two binary trees

50 Extended binary trees Leftist Trees AG BHCI EDFJ

51 The number inside each internal node x is shortest(x). Leftist Trees 22 2111 1111

52 Definition A leftist tree is a binary tree such that if it is not empty, then for every internal node x: 22 2111 1111

53 Leftist Trees By the definition Let x be the root of a leftist tree that has n internal nodes. a) x shortest(x) A binary tree whose shortest height is shortest(x) means for every path from root to leaf has at least shortest(x) nodes. Such that it has at least 2 shortest(x) - 1 nodes.

54 Leftist Trees b) The rightmost root to external node path is the shortest root to external node path. x shortest(x) Base on the definition of leftist tree.

55 Leftist Trees Definition A min-leftist tree (max leftist tree) is a leftist tree in which the key value in each node is no larger (smaller) than the key values in its children (if any). 2 750 11 80 13

56 Leftist Trees Combination of leftist trees

57 The combination step (min-leftist trees) 1. Choose minimum root of the two trees, A and B. 2. Leave the left subtree of smaller root (suppose A) unchanged and combine the right subtree of A with B. Back to step 1, until no remaining vertices. 3. Compare shortest(x) and swap to make it satisfy the definition of leftist trees.

58 Combination of leftist trees Step 1. Choose smaller root and set as a. Step 2. If a has no right_chlid, set b as a ’ s right_child. Else, recursively combine a ’ s right_child and b. Step 3. Make the combined tree satisfied the leftist tree property.

59 Combination of leftist trees 2 7 50 1180 13 5 98 12 10 20 15 18

60 Combination of leftist trees 2 7 50 11 8013 5 9 8 12 10 20 15 18

61 Combination of leftist trees 2 7 50 11 80 13 5 9 8 12 10 20 15 18

62 Combination of leftist trees 2 7 50 11 80 13 5 9 8 12 10 20 15 18

63 Combination of leftist trees 2 7 50 11 80 13 5 9 8 1210 20 15 18 2 1 1 1 2 2 1 1111 2 1 1

64 Combination of leftist trees 2 7 50 11 80 13 5 9 8 1210 20 15 18 2 1 1 1 2 2 1 1111 2 1 1

65 Combination of leftist trees Both insert and delete min operations can be implemented by using the combine operation. Insert: Treat the inserting node as a single node binary tree. Combine with the original one. Delete: Remove the node can get two separate subtrees. Combine the two trees.

66 Binomial Heaps Definition

67 Binomial Trees [Definition] Binomial trees A binomial tree B k has 2 k nodes with height be k. BkBk B k-1 B0B0 B1B1 B2B2 B3B3

68 Binomial Trees It has nodes at depth i. The ith child of root is the root of subtree B i-1. B4B4 B2B2 B3B3 B1B1 B0B0 Depth 1 : 4 nodes. Depth 2 : 6 nodes. Depth 3 : 4 nodes.

69 Binomial Heaps Collection of min (max) trees. The min trees should be Binomial trees. 10 8 5 3 6 4 15 12 20 30 7 1 16 9

70 Binomial Heaps The representation of B-heap: Degree: number of children a node has. Child: point to any one of its children. Left_link, Right_link: maintain doubly linked circular list of siblings. The position of pointer a is the min element.

71 Binomial Heaps 8 10 3 54 6 1 71612 1530 20 9 Siblings parent child a pointer a

72 Binomial Heaps Combination of binomial heaps

73 8 10 3 54 6 1 71612 1530 20 9 a 40

74 Combination of binomial heaps 8 10 3 54 6 1 71612 1530 20 9 a 40

75 Pairwise combine Combination of binomial heaps 8 10 3 54 6 1 71612 1530 20 9 a 40

76 Pairwise combine Combination of binomial heaps 8 10 3 54 6 1 71612 1530 20 9 a 40

77 Pairwise combine Combination of binomial heaps 8 10 3 54 6 1 71612 1530 20 9 a 40

78 Pairwise combine Combination of binomial heaps 8 10 3 54 6 1 71612 1530 20 9 a 40

79 Pairwise combine Combination of binomial heaps 8 10 3 54 6 1 71612 1530 20 9 a 40

80 Pairwise combine Combination of binomial heaps 8 10 3 54 6 1 71612 1530 20 9 a 40

81 Pairwise combine Combination of binomial heaps 8 10 3 54 6 1 71612 1530 20 9 a 40

82 Pairwise combine Combination of binomial heaps 8 10 3 54 6 1 71612 1530 20 9 a 40

83 Combination of binomial heaps 8 10 3 54 6 1 71612 1530 20 9 a 40

84 Combination of binomial heaps The insertion is to combine a single vertex b-heap to original b-heap. After we delete the min element, we get several b-heaps that originally subtrees of the removed vertex. Then combine them together.

85 Time complexity

86 Fibonacci Heaps Definition

87 Fibonacci Heaps Collection of min (max) trees. The min trees need not be Binomial trees. B-heaps are a special case of F-heaps. So that what B-heaps can do can be done in F-heaps. More than that, F-heap may delete an arbitrary node and decrease key.

88 Fibonacci Heaps Deletion and Decrease key

89 Deletion 8 10 3 54 6 1 71612 1530 20 9

90 Deletion and Decrease key Deletion 8 10 3 54 6 1 716 1530 20 9

91 Deletion and Decrease key Deletion 8 10 3 54 6 1 716 1530 20 9

92 Deletion and Decrease key Decrease key 8 10 3 54 6 1 71612 1530 20 9 11

93 Deletion and Decrease key Decrease key 8 10 3 54 6 1 71612 1130 20 9

94 Deletion and Decrease key Decrease key 8 10 3 54 6 1 71612 1130 20 9

95 Time complexity


Download ppt "Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement."

Similar presentations


Ads by Google