A Heap Is Efficiently Represented As An Array - PowerPoint PPT Presentation

About This Presentation
Title:

A Heap Is Efficiently Represented As An Array

Description:

A Heap Is Efficiently Represented As An Array 9 8 7 6 7 2 6 5 1 9 8 7 6 7 2 6 5 1 1 2 3 4 5 6 7 8 9 10 0 * – PowerPoint PPT presentation

Number of Views:114
Avg rating:3.0/5.0
Slides: 79
Provided by: Preferr564
Category:

less

Transcript and Presenter's Notes

Title: A Heap Is Efficiently Represented As An Array


1
A Heap Is Efficiently Represented As An Array
9
8
7
6
7
2
6
5
1
1
2
3
4
5
6
7
8
9
10
0
2
Moving Up And Down A Heap
3
Inserting An Element Into A Max Heap
  • Complete binary tree with 10 nodes.

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

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

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

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

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

9
Inserting An Element Into A Max Heap
20
9
7
8
6
2
6

7
5
1
7
7
  • Complete binary tree with 11 nodes.

10
Inserting An Element Into A Max Heap
20
9
7
8
6
2
6

7
5
1
7
7
  • New element is 15.

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

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

13
Complexity of Inserting
  • Complexity is O(log n), where n is heap size.

14
Deleting the Max Element
  • Max element is in the root.

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

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

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

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

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

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

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

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

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

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

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

26
Complexity of Remove Max Element
  • Complexity is O(log n).

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

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

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

30
Initializing a Max Heap
1
3
2
4
6
7
11
5
8
7
8
9
7
10
31
Initializing a Max Heap
1
3
2
9
6
7
11
5
8
7
8
4
7
10
32
Initializing a Max Heap
1
3
2
9
6
7
11
5
8
7
8
4
7
10
33
Initializing a Max Heap
1
7
2
9
6
3
11
5
8
7
8
4
7
10
34
Initializing a Max Heap
1
7
2
9
6
3
11
5
8
7
8
4
7
10
35
Initializing a Max Heap
1
7
11
9
6
3
5
8
7
8
4
7
10
Find a home for 2.
36
Initializing a Max Heap
1
7
11
9
6
3
10
8
7
8
4
7
5
Find a home for 2.
37
Initializing a Max Heap
1
7
11
9
6
3
10
8
7
8
4
7
5
2
Done, move to next lower array position.
38
Initializing a Max Heap
1
7
11
9
6
3
10
2
8
7
8
4
7
5
Find home for 1.
39
Initializing a Max Heap
11
7
9
6
3
10
2
8
7
8
4
7
5
Find home for 1.
40
Initializing a Max Heap
11
7
10
9
6
3
2
8
7
8
4
7
5
Find home for 1.
41
Initializing a Max Heap
11
7
10
9
6
3
5
2
8
7
8
4
7
Find home for 1.
42
Initializing a Max Heap
11
7
10
9
6
3
5
2
8
7
8
4
7
1
Done.
43
Time Complexity
11
7
9
6
3
5
8
8
7
4
7
2
1
10
Height of heap h. Number of subtrees with root
at level j is lt 2 j-1. Time for each subtree is
O(h-j1).
44
Complexity
Time for level j subtrees is lt 2j-1(h-j1)
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 n1
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
0
1
0
0
1
1
0
0
0
0
0
0
0
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

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

54
A Leftist Tree
2
2
1
2
1
1
0
1
0
0
1
1
0
0
0
0
0
0
0
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
0
1
0
0
1
1
0
0
0
0
0
0
0
Length of rightmost path is 2.
57
Leftist TreesProperty 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) lt log(n1)

58
A Leftist Tree
2
2
1
2
1
1
0
1
0
0
1
1
0
0
0
0
0
0
0
Levels 1 and 2 have no external nodes.
59
Leftist TreesProperty 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)

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

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
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
Make melded subtree right subtree of smaller root.
Swap left and right subtree if s(left) lt s(right).
75
Meld Two Min Leftist Trees
Make melded subtree right subtree of smaller root.
Swap left and right subtree if s(left) lt s(right).
76
Meld Two Min Leftist Trees
Make melded subtree right subtree of smaller root.
Swap left and right subtree if s(left) lt 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
Write a Comment
User Comments (0)
About PowerShow.com