AEP CS2 Heap
AEP CS2 Heap
Heaps
Tree
A tree is a finite set of one or more nodes such that:
2
Terminology
• The degree of a node is the number of subtrees of the node
• The node with degree 0 is a leaf or terminal node.
• A node that has subtrees is the parent of the roots of the
subtrees.
• The roots of these subtrees are the children of the node.
• Children of the same parent are siblings.
• The ancestors of a node are all the nodes along the path
from the root to the node.
3
Example
Level
• node (13)
• degree of a node
A 1
• leaf (terminal) 3 1
• nonterminal
• parent B C D 2
2 2 1 2 3 2
• children
• sibling 2
E F
30 30
G
3
H
1 30 30
I J
3 3
• degree of a tree (3)
• ancestor K L M 4
0 40 4 0 4
• level of a node
• height of a tree (4)
4
Binary Tree
A binary tree is a finite set of nodes that is
either empty or consists of a root and two
disjoint binary trees called the left subtree
and the right subtree.
Any tree can be transformed into binary tree.
– by left child-right sibling representation
The left subtree and the right subtree are
distinguished.
5
Example of Binary Tree
A
E C
F G D
K
H
L
M I
J 6
Number of Nodes in BT
The maximum number of nodes on level i of a
binary tree is 2i-1, i>=1.
The maximum number of nodes in a binary tree
of depth k is 2k-1, k>=1.
Prove by induction.
k
2 i 1
2 k
1
i 1
7
Full BT vs. Complete BT
A full binary tree is a tree in which every node other
than the leaves has two children.
A complete binary tree is a binary tree in which
every level, except possible the last, is completely
filled, and all nodes are as far left as possible.
A
A
B C
B C
D E F G
D E F G
H I J K L M N O
H I
Complete binary tree Full binary tree of depth 4
8
Heaps Definition
A heap is a
certain kind of
complete
binary tree.
Heaps
Root
A heap is a
certain kind of
complete
binary tree.
When a complete
binary tree is built,
its first node must be
the root.
Heaps
Left child
Complete of the
root
binary tree.
Right child
Complete of the
root
binary tree.
Complete
binary tree.
Complete
binary tree.
Complete
binary tree.
Complete
binary tree.
Complete
binary tree.
Heaps
45
A heap is a
certain kind of 35 23
complete
binary tree. 27 21 22 4
19
Each node in a heap
contains a key that
can be compared to
other nodes' keys.
Heaps
45
A heap is a
certain kind of 35 23
complete
binary tree. 27 21 22 4
19
The "heap property"
requires that each
node's key is >= the
keys of its children
Application : Priority Queues
• A priority queue is a container class that
allows entries to be retrieved according to
some specific priority levels
– The highest priority entry is removed first
– If there are several entries with equally high
priorities, then the priority queue’s
implementation determines which will come out
first (e.g. FIFO)
• Heap is suitable for a priority queue
The Priority Queue ADT with Heaps
• The entry with the highest priority is always at the
root node
• Focus on two priority queue operations
– adding a new entry
– remove the entry with the highest priority
• In both cases, we must ensure the tree structure
remains to be a heap
– we are going to work on a conceptual heap without
worrying about the precise implementation
– later I am going to show you how to implement...
Adding a Node to a Heap
45
node reaches an
acceptable location. 19 42
Adding a Node to a Heap
45
node reaches an
acceptable location. 19 27
Adding a Node to a Heap
45
node reaches an
acceptable location. 19 27
Adding a Node to a Heap
45
35 21 22 4
19 27
Removing the Top of a Heap
27
35 21 22 4
19
Removing the Top of a Heap
27
node reaches an
acceptable location. 19 45*
Adding a Node : same priority
45
node reaches an
acceptable location. 19 27
Adding a Node : same priority
45
node reaches an
acceptable location. 19 27
Adding a Node : same priority
45
An array of data
Implementing a Heap
42
42
An array of data
Implementing a Heap
42
42 35 23
An array of data
Implementing a Heap
42
42 35 23 27 21
An array of data
Implementing a Heap
42
42 35 23 27 21
An array of data
We don't care what's in
this part of the array.
Important Points about the
Implementation
42
42 35 23 27 21
An array of data
Important Points about the
Implementation
42
42 35 23 27 21