1 Heaps. 2 Background: Priority Queues. zQueues are a First-In, First-Out data structure; zPriority...
-
date post
21-Dec-2015 -
Category
Documents
-
view
213 -
download
0
Transcript of 1 Heaps. 2 Background: Priority Queues. zQueues are a First-In, First-Out data structure; zPriority...
2
Background: Priority Queues.
Queues are a First-In, First-Out data structure;
Priority Queues are similar, except those of highest priority must be removed first.
How should we implement this?
3
Priority Queue Implementation
Arrays Inserts: O(logn) to find spot; O(n) to move
all others down. Deletes: O(1) to remove; O(n) to move
others.Linked Lists :
Inserts: O(n) to find spot; O(1) to do insert. Deletes: O(1) to remove.
4
Priority Queue Implementation
AVL-Trees Inserts: O(logn) to find spot; O(logn) to do
rotation(s). Deletes: O(logn) to remove; O(logn) to do
rotations.Heaps :
Inserts: O(1) to find spot; O(logn) to do insert. Deletes: O(1) to find; O(logn) to delete/rotate.
5
Heaps Vs. AVL-Trees
AVL-Trees have a lot of overhead to balance.
AVL-Trees store the values in order.
Heaps only provide access to the least/greatest.
This requires less overhead and is easier to implement.
6
What is a Heap?BINARY MIN HEAP is a complete binary
tree in which each node is empty or has a key value that is less than both of its children. Both children are Binary Min Heaps
A COMPLETE BINARY TREE is a binary tree where the root is either empty OR TL is a perfect tree of height h-1 and TR is a complete tree of height h-1 OR TL is a complete tree of height h-1 and TR is a
perfect tree of height h-2
7
More Definitions
A PERFECT TREE is one where the root is either empty or has HL – HR = 0 and both sub trees are also perfect trees.
i.e. the last level (and all levels) are completely filled.
8
Heap as a Tree
As a tree, inserts would fill a level entirely, left to right before going on to the next level:
A
B C
D E F G
9
Heap Insert Algorithm
The previous slide discussed where the new node would go, but how to maintain the order of the heap?
Algorithm: Compare new node to parent; if the new
node is greater than the parent, stop. Else, swap new node and parent. Repeat
algorithm at parent (percolate up).
10
Heap Example
Now let’s do an extended example, inserting a series of priority values into a heap.
We will be inserting 90, 50, 25, 10, 15, 20, 75.
Let’s start with 90:
20
Heap Example
Swap again?No. 15 is where it needs to be. Now 20:
10
15
Left to insert:20, 75
50
90 25
24
Deleting from a Heap
The minimum value is at the root of the tree; thus, it can be found easily, but how do we rebalance?
Algorithm: Remove Root’s value as minimum value Replace root with last node; delete last. If root is less than both its children, stop. Else, swap root and min child; repeat from
min child node (percolate down).
26
Heap Delete Example
Is 75 less than both of its children?No. Swap with minimum (15):
75
15 20
90 25 50
27
Heap Delete Example
Is 75 less than both of its children?No. Swap with minimum (25):
15
75 20
90 25 50
29
Implementation Problem
The problem with the tree/pointer representation is how to maintain the “next” pointer.
I think this can be done by keeping a counter and manipulating it to decide left or right from the root.
But there is another way:
30
Array Implementation
Any binary tree may be represented in an array.
The only questions really are Where are the two children of a node? Where is a node’s parent?
Answer (for a node at position i): The two children are at 2*i and 2*i+1 The parent is at i div 2.
31
Array Example:
For this tree:
Here’s the array:
10
15 20
90 25 50 75
15
2
10
1
25
5
90
4
20
3
75
7
50
6