cs20l-lecture set 11 - binary heap
TRANSCRIPT
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
1/14
Binary HeapA special kind of binary tree. It has two
properties that are not generally true for other
trees:
Completeness
The tree is complete, which means that nodes areadded from top to bottom, left to right, withoutleaving any spaces. A binary tree is completely full ifit is of height, h, and has 2h+1-1 nodes.
Heapness
The item in the tree with the highest priority is at thetop of the tree, and the same is true for everysubtree.
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
2/14
Binary HeapBinary tree of height, h, is complete iff
it is empty
or
its left subtree is complete of height h-1and its right subtree is completely full ofheight h-2
or
its left subtree is completely full of heighth-1 and its right subtree is complete of
height h-1.
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
3/14
Binary Heap
In simple terms:
A heap is a binary tree in which every
parent is greater than its child(ren).
A Reverse Heap is one in which the rule
is every parent is less than thechild(ren).
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
4/14
Binary HeapTo build a heap, data is placed in the tree as it arrives.
Algorithm:
1. add a new node at the next leaf position2. use this new node as the current position
3. While new data is greater than that in the
parent of the current node
move the parent down to the current node
make the parent (now vacant) the current node
Place data in current node
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
5/14
Binary HeapNew nodes are always added on the deepest levelin an orderly way.
The tree never becomes unbalanced.
There is no particular relationship among the data items
in the nodes on any given level, even the ones that have
the same parent
A heap is not a sorted structure. Can be regarded aspartially ordered.
A given set of data can be formed into many different
heaps (depends on the order in which the data arrives.)
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
6/14
Binary Heap
Example:
Data arrives to be heaped in the order:
54, 87, 27, 67, 19, 31, 29, 18, 32, 56, 7, 12,
31
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
7/14
Binary Heap
54, 87, 27, 67, 19, 31, 29, 18, 32, 56, 7, 12, 31
54 54
87
87
54
87
54 27
87
54 27
67
87
67 27
54
87
67 27
54 19
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
8/14
Binary Heap
54, 87, 27, 67, 19, 31, 29, 18, 32, 56, 7, 12, 31
27
87
67
54 19
27
87
67
54 19 31
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
9/14
Binary Heap
54, 87, 27, 67, 19, 31, 29, 18, 32, 56, 7, 12, 31
29
31
87
67
54 19 27 29
31
87
67
54 19 27 29
18
etc
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
10/14
Binary HeapTo delete an element from the heap:
Algorithm:
If node is a leaf, delete it
Else if node has a right child, locate the
deepest leaf of right subtree and
replace node with that leaf. Then re-
heapify
Else replace node with the left child.
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
11/14
Binary Heap
Example: Deleting the root node, T
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
12/14
Binary Heap
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
13/14
Binary Heap
-
8/7/2019 CS20L-Lecture Set 11 - Binary Heap
14/14
Binary Heap