Presentation is loading. Please wait.

Presentation is loading. Please wait.

Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 8 4 7 67 89 3 7 10 1 11 5 2.

Similar presentations


Presentation on theme: "Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 8 4 7 67 89 3 7 10 1 11 5 2."— Presentation transcript:

1 Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 8 4 7 67 89 3 7 10 1 11 5 2

2 Initializing A Max Heap Start at rightmost array position that has a child. 8 4 7 67 89 3 7 10 1 11 5 2 Index is n/2.

3 Initializing A Max Heap Move to next lower array position. 8 4 7 67 89 3 7 10 1 5 11 2

4 Initializing A Max Heap 8 4 7 67 89 3 7 10 1 5 11 2

5 Initializing A Max Heap 8 9 7 67 84 3 7 10 1 5 11 2

6 Initializing A Max Heap 8 9 7 67 84 3 7 10 1 5 11 2

7 Initializing A Max Heap 8 9 7 63 84 7 7 10 1 5 11 2

8 Initializing A Max Heap 8 9 7 63 84 7 7 10 1 5 11 2

9 Initializing A Max Heap 8 9 7 63 84 7 7 10 1 5 11 Find a home for 2.

10 Initializing A Max Heap 8 9 7 63 84 7 7 5 1 11 Find a home for 2. 10

11 Initializing A Max Heap 8 9 7 63 84 7 7 2 1 11 Done, move to next lower array position. 10 5

12 Initializing A Max Heap 8 9 7 63 84 7 7 2 1 11 10 5 Find home for 1.

13 11 Initializing A Max Heap 8 9 7 63 84 7 7 2 10 5 Find home for 1.

14 Initializing A Max Heap 8 9 7 63 84 7 7 2 11 10 5 Find home for 1.

15 Initializing A Max Heap 8 9 7 63 84 7 7 2 11 10 5 Find home for 1.

16 Initializing A Max Heap 8 9 7 63 84 7 7 2 11 10 5 Done. 1

17 Time Complexity 87 63 4 7 7 10 11 5 2 9 8 1 Height of heap = h. Number of subtrees with root at level j is <= 2 j-1. Time for each subtree is O(h-j+1).

18 Complexity Time for level j subtrees is <= 2 j-1 (h-j+1) = t(j). Total time is t(1) + t(2) + … + t(h-1) =

19 Heap Sort Heap Sort (program 9.12) –create a max heap –keep extracting the root element from the max heap, and put the element into the result array accordingly time complexity = initialization time + n × deleting element time = O(n)+O(n × log n) = O(n log n)

20 Heap Sort --example

21

22

23 Comparison of sorting methods

24

25

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

27 Extended Binary Trees Start with any binary tree and add an external node wherever there is an empty subtree. Result is an extended binary tree.

28 A Binary Tree

29 An Extended Binary Tree number of external nodes is n+1

30 The Function s() For any node x in an extended binary tree, let s(x) be the length of a shortest path from x to an external node in the subtree rooted at x.

31 s() Values Example

32 0000 00 00 0 0 11 1 211 21 2

33 Properties Of s() If x is an external node, then s(x) = 0. Otherwise, s(x) = min {s(leftChild(x)), s(rightChild(x))} + 1

34 Height Biased Leftist Trees A binary tree is a (height biased) leftist tree iff for every internal node x, s(leftChild(x)) >= s(rightChild(x))

35 A Leftist Tree 0000 00 00 0 0 11 1 211 21 2

36 Leftist Trees--Property 1 In a leftist tree, the rightmost path is a shortest root to external node path and the length of this path is s(root).

37 A Leftist Tree 0000 00 00 0 0 11 1 211 21 2 Length of rightmost path is 2.

38 Leftist Trees—Property 2 The number of internal nodes is at least 2 s(root) - 1 –(note: because level 1 through level s(root) have no external nodes.) If there are n nodes in the leftist tree, then s(root)  log (n+1) –(because 2 s(root) – 1  n )

39 A Leftist Tree 0000 00 00 0 0 11 1 211 21 2 Levels 1 and 2 have no external nodes.

40 Leftist Trees—Property 3 Length of rightmost path is O(log n), where n is the number of nodes in a leftist tree. –Follows from Properties 1 and 2.

41 Leftist Trees As Priority Queues Min leftist tree … leftist tree that is a min tree. Used as a min priority queue. Max leftist tree … leftist tree that is a max tree. Used as a max priority queue.

42 A Min Leftist Tree 86 9 685 43 2

43 Some Min Leftist Tree Operations put() remove() meld() initialize() put() and remove() use meld().

44 Put Operation put(7) 86 9 685 43 2

45 Meld operation The meld strategy is the best described using recursion Let A and B be the two min HBLTs that are to be melded The root with smaller element becomes the root of the melded HBLT

46 Meld operation – cont. Suppose that A has the smaller root and that its left subtree is L. Let C be the min HBLT that results from melding the right subtree of A and the min HBLT B The result of melding A and B is the min HBLT has A as its root and L and C as its subtrees. If the s values of L is smaller than that of C, the C is the left subtree. Otherwise, L is.

47 Put Operation put(7) 86 9 685 43 2 Create a single node min leftist tree. 7

48 Put Operation put(7) 86 9 685 43 2 Create a single node min leftist tree. Meld the two min leftist trees. 7

49 Remove Min 86 9 685 43 2

50 86 9 685 43 2 Remove the root.

51 Remove Min 86 9 685 43 2 Remove the root. Meld the two subtrees.

52 Meld Two Min Leftist Trees 86 9 685 43 6 Traverse only the rightmost paths so as to get logarithmic performance.

53 Meld Two Min Leftist Trees 86 9 685 43 6 Meld right subtree of tree with smaller root and all of other tree.

54 Meld Two Min Leftist Trees 86 9 685 43 6 Meld right subtree of tree with smaller root and all of other tree.

55 Meld Two Min Leftist Trees 86 68 4 6 Meld right subtree of tree with smaller root and all of other tree.

56 Meld Two Min Leftist Trees 8 6 Meld right subtree of tree with smaller root and all of other tree. Right subtree of 6 is empty. So, result of melding right subtree of tree with smaller root and other tree is the other tree.

57 Meld Two Min Leftist Trees Swap left and right subtree if s(left) < s(right). Make melded subtree right subtree of smaller root. 8 66 8 6 8

58 Meld Two Min Leftist Trees 86 66 4 886 6 4 6 8 Make melded subtree right subtree of smaller root. Swap left and right subtree if s(left) < s(right).

59 Meld Two Min Leftist Trees 9 5 3 Swap left and right subtree if s(left) < s(right). Make melded subtree right subtree of smaller root. 86 66 4 8

60 Meld Two Min Leftist Trees 9 5 3 86 66 4 8

61 Initializing In O(n) Time create n single node min leftist trees and place them in a FIFO queue repeatedly remove two min leftist trees from the FIFO queue, meld them, and put the resulting min leftist tree into the FIFO queue the process terminates when only 1 min leftist tree remains in the FIFO queue analysis is the same as for heap initialization


Download ppt "Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 8 4 7 67 89 3 7 10 1 11 5 2."

Similar presentations


Ads by Google