Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Heap Is Efficiently Represented As An Array

Similar presentations


Presentation on theme: "A Heap Is Efficiently Represented As An Array"— Presentation transcript:

1 A Heap Is Efficiently Represented As An Array
9 8 6 7 2 5 1 9 8 7 6 7 2 6 5 1 1 2 3 4 5 6 7 8 9 10

2 Moving Up And Down A Heap
9 8 6 7 2 5 1 3 4

3 Inserting An Element Into A Max Heap
9 8 6 7 2 5 1 7 Complete binary tree with 10 nodes.

4 Inserting An Element Into A Max Heap
9 8 7 6 7 2 6 5 1 5 7 New element is 5.

5 Inserting An Element Into A Max Heap
9 8 7 6 7 2 6 5 1 7 7 New element is 20.

6 Inserting An Element Into A Max Heap
9 8 7 6 2 6 5 1 7 7 7 New element is 20.

7 Inserting An Element Into A Max Heap
9 7 6 8 2 6 5 1 7 7 7 New element is 20.

8 Inserting An Element Into A Max Heap
20 9 7 6 8 2 6 5 1 7 7 7 New element is 20.

9 Inserting An Element Into A Max Heap
20 9 7 6 8 2 6 5 1 7 7 7 Complete binary tree with 11 nodes.

10 Inserting An Element Into A Max Heap
20 9 7 6 8 2 6 5 1 7 7 7 New element is 15.

11 Inserting An Element Into A Max Heap
20 9 7 6 2 6 5 1 7 7 8 7 8 New element is 15.

12 Inserting An Element Into A Max Heap
20 15 7 6 9 2 6 5 1 7 7 8 7 8 New element is 15.

13 Complexity of Inserting
8 6 7 2 5 1 20 9 15 Complexity is O(log n), where n is heap size.

14 Deleting the Max Element
8 6 7 2 5 1 20 9 15 Max element is in the root.

15 Deleting the Max Element
15 7 6 9 2 6 5 1 7 7 8 7 8 After max element is removed.

16 Deleting the Max Element
15 7 6 9 2 6 5 1 7 7 8 7 8 Heap with 10 nodes. Reinsert 8 into the heap.

17 Deleting the Max Element
15 7 6 9 2 6 5 1 7 7 7 Reinsert 8 into the heap.

18 Deleting the Max Element
15 7 6 9 2 6 5 1 7 7 7 Reinsert 8 into the heap.

19 Deleting the Max Element
15 9 7 6 8 2 6 5 1 7 7 7 Reinsert 8 into the heap.

20 Deleting the Max Element
15 9 7 6 8 2 6 5 1 7 7 7 Max element is 15.

21 Deleting the Max Element
9 7 6 8 2 6 5 1 7 7 7 After max element is removed.

22 Deleting the Max Element
9 7 6 8 2 6 5 1 7 7 7 Heap with 9 nodes.

23 Deleting the Max Element
9 7 6 8 2 6 5 1 Reinsert 7.

24 Deleting the Max Element
9 7 6 8 2 6 5 1 Reinsert 7.

25 Deleting the Max Element
9 8 7 6 7 2 6 5 1 Reinsert 7.

26 Complexity of Remove Max Element
6 2 5 1 7 9 8 Complexity is O(log n).

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

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

29 Initializing a Max Heap
1 2 3 4 11 6 7 8 9 10 7 7 5 8 Move to next lower array position.

30 Initializing a Max Heap
1 2 3 4 11 6 7 8 9 7 7 5 10 8

31 Initializing a Max Heap
1 2 3 9 11 6 7 8 4 7 7 5 10 8

32 Initializing a Max Heap
1 2 3 9 11 6 7 8 4 7 7 5 10 8

33 Initializing a Max Heap
1 2 7 9 11 6 3 8 4 7 7 5 10 8

34 Initializing a Max Heap
1 2 7 9 11 6 3 8 4 7 7 5 10 8

35 Initializing a Max Heap
1 11 7 9 6 3 8 4 10 7 7 5 8 Find a home for 2.

36 Initializing a Max Heap
1 11 7 9 10 6 3 8 4 8 7 7 5 Find a home for 2.

37 Initializing a Max Heap
1 11 7 9 10 6 3 8 4 7 7 2 8 5 Done, move to next lower array position.

38 Initializing a Max Heap
1 11 7 9 10 6 3 8 4 7 7 2 5 8 Find home for 1.

39 Initializing a Max Heap
11 7 9 10 6 3 8 4 7 7 2 8 5 Find home for 1.

40 Initializing a Max Heap
11 10 7 9 6 3 8 4 7 7 2 5 8 Find home for 1.

41 Initializing a Max Heap
11 10 7 9 5 6 3 8 4 7 7 2 8 Find home for 1.

42 Initializing a Max Heap
11 10 7 9 5 6 3 8 4 7 7 2 1 8 Done.

43 Time Complexity Height of heap = h.
11 9 7 8 5 6 3 1 4 10 7 7 2 8 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).

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

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

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

47 A Binary Tree

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

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

50 s() Values Example

51 s() Values Example 2 2 1 2 1 1 1 1 1

52 Properties of s() If x is an external node, then s(x) = 0. Otherwise,
s(x) = min {s(leftChild(x)), s(rightChild(x))} + 1 s() values may be computed easily using a postorder traversal.

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

54 A Leftist Tree 2 2 1 2 1 1 1 1 1

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

56 A Leftist Tree 2 2 1 2 1 1 1 1 1 Length of rightmost path is 2.

57 Leftist Trees—Property 2
The number of internal nodes is at least 2s(root) - 1 Because levels 1 through s(root) have no external nodes. So, s(root) <= log(n+1)

58 A Leftist Tree Levels 1 and 2 have no external nodes. 2 2 1 2 1 1 1 1
1 1 1 Levels 1 and 2 have no external nodes.

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

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

61 A Min Leftist Tree 2 4 3 6 8 5 9 8 6

62 Some Min Leftist Tree Operations
insert() delete() meld() initialize() insert() and delete() use meld().

63 Insert Operation insert(7) 2 4 3 6 8 5 9 8 6

64 Insert Operation insert(7) Create a single node min leftist tree. 2 4
3 6 8 5 9 8 6 Create a single node min leftist tree. 7

65 Insert Operation insert(7) Create a single node min leftist tree.
2 4 3 6 8 5 9 8 6 Create a single node min leftist tree. Meld the two min leftist trees. 7

66 Delete Min 2 4 3 6 8 5 9 8 6

67 Delete Min 2 4 3 6 8 5 9 8 6 Delete the root.

68 Delete Min 2 4 3 6 8 5 9 8 6 Delete the root. Meld the two subtrees.

69 Meld Two Min Leftist Trees
8 6 9 5 4 3 Traverse only the rightmost paths so as to get logarithmic performance.

70 Meld Two Min Leftist Trees
4 3 6 8 5 6 9 8 6 Meld right subtree of tree with smaller root and all of other tree.

71 Meld Two Min Leftist Trees
4 3 6 8 5 6 9 8 6 Meld right subtree of tree with smaller root and all of other tree.

72 Meld Two Min Leftist Trees
6 4 6 8 8 6 Meld right subtree of tree with smaller root and all of other tree.

73 Meld Two Min Leftist Trees
6 8 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.

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

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

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

77 Meld Two Min Leftist Trees
3 4 5 6 6 9 8 6 8

78 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 queue Analysis is the same as for heap initialization


Download ppt "A Heap Is Efficiently Represented As An Array"

Similar presentations


Ads by Google