0% found this document useful (0 votes)
31 views48 pages

AEP CS2 Heap

The document discusses heaps and their properties as a complete binary tree data structure. It defines what a heap is, how nodes are added by pushing them upward, and how the top node is removed by pushing the last node downward.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views48 pages

AEP CS2 Heap

The document discusses heaps and their properties as a complete binary tree data structure. It defines what a heap is, how nodes are added by pushing them upward, and how the top node is removed by pushing the last node downward.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

CS 2133

Heaps
Tree
A tree is a finite set of one or more nodes such that:

• There is a specially designated node called the root.

• The remaining nodes are partitioned into n>=0 disjoint

sets T1, ..., Tn, where each of these sets is a tree.

• We call T1, ..., Tn the subtrees of the root.

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.

The second node is


always the left child
of the root.
Heaps

Right child
Complete of the
root
binary tree.

The third node is


always the right child
of the root.
Heaps

Complete
binary tree.

The next nodes


always fill the next
level from left-to-right.
Heaps

Complete
binary tree.

The next nodes


always fill the next
level from left-to-right.
Heaps

Complete
binary tree.

The next nodes


always fill the next
level from left-to-right.
Heaps

Complete
binary tree.

The next nodes


always fill the next
level from left-to-right.
Heaps

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

Put the new node in the


next available spot.
35 23
Push the new node
upward, swapping with
its parent until the new 27 21 22 4

node reaches an
acceptable location. 19 42
Adding a Node to a Heap
45

Put the new node in the


next available spot.
35 23
Push the new node
upward, swapping with
its parent until the new 42 21 22 4

node reaches an
acceptable location. 19 27
Adding a Node to a Heap
45

Put the new node in the


next available spot.
42 23
Push the new node
upward, swapping with
its parent until the new 35 21 22 4

node reaches an
acceptable location. 19 27
Adding a Node to a Heap
45

 The parent has a key that


is >= new node, or
 The node reaches the root. 42 23
 The process of pushing the
new node upward is
35 21 22 4
called
reheapification
upward.
19 27

Note: Note: we need to easily go from child to parent as


well as parent to child.
Removing the Top of a Heap
45

Move the last node onto


the root.
42 23

35 21 22 4

19 27
Removing the Top of a Heap
27

Move the last node onto


the root.
42 23

35 21 22 4

19
Removing the Top of a Heap
27

Move the last node onto


the root.
42 23
Push the out-of-place
node downward,
swapping with its larger 35 21 22 4

child until the new node


reaches an acceptable 19
location.
Removing the Top of a Heap
42

Move the last node onto


the root.
27 23
Push the out-of-place
node downward,
swapping with its larger 35 21 22 4

child until the new node


reaches an acceptable 19
location.
Removing the Top of a Heap
42

Move the last node onto


the root.
35 23
Push the out-of-place
node downward,
swapping with its larger 27 21 22 4

child until the new node


reaches an acceptable 19
location.
Removing the Top of a Heap
42

The children all have


keys <= the out-of-place
node, or 35 23

The node reaches the


leaf. 27 21 22 4
 The process of pushing
the new node
downward is called 19
reheapification
downward.
Priority Queues Revisited
• 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
Adding a Node: same priority
45

Put the new node in the


next available spot.
35 23
Push the new node
upward, swapping with
its parent until the new 27 21 22 4

node reaches an
acceptable location. 19 45*
Adding a Node : same priority
45

Put the new node in the


next available spot.
35 23
Push the new node
upward, swapping with
its parent until the new 45* 21 22 4

node reaches an
acceptable location. 19 27
Adding a Node : same priority
45

Put the new node in the


next available spot.
45* 23
Push the new node
upward, swapping with
its parent until the new 35 21 22 4

node reaches an
acceptable location. 19 27
Adding a Node : same priority
45

 The parent has a key that


is >= new node, or
 The node reaches the root. 45* 23
 The process of pushing the
new node upward is
35 21 22 4
called
reheapification
upward.
19 27

Note: Implementation determines which 45 will be in


the root, and will come out first when popping.
Removing the Top of a Heap
45*
 The children all have keys
<= the out-of-place node,
or 35 23
 The node reaches the leaf.
 The process of pushing the
new node downward is 27 21 22 4
called
reheapification
downward.
19

Note: Implementation determines which 45 will be in


the root, and will come out first when popping.
Heap Implementation
• Use tree structure
– node implementation is for a general binary tree
– need to have doubly linked node
• Use arrays
– A heap is a complete binary tree
– which can be implemented more easily with an
array than with the node class
– and do two-way links
Formulas for location children and
parents in an array representation
• Root at location [0]
• Parent of the node in [i] is at [(i-1)/2]
• Children of the node in [i] (if exist) is at [2i+1]
and [2i+2]
• Test:
– complete tree of 10, 000 nodes
– parent of 4999 is at (4999-1)/2 = 2499
– children of 4999 is at 9999 (V) and 10,000 (X)
Implementing a Heap
42

• We will store the


data from the 35 23
nodes in a
partially-filled 27 21
array.

An array of data
Implementing a Heap
42

• Data from the root


goes in the first 35 23
location of
the array. 27 21

42

An array of data
Implementing a Heap
42

• Data from the next


row goes in the 35 23
next two array
locations. 27 21

42 35 23

An array of data
Implementing a Heap
42

• Data from the next


row goes in the 35 23
next two array
locations. 27 21

42 35 23 27 21

An array of data
Implementing a Heap
42

• Data from the next


row goes in the 35 23
next two array
locations. 27 21

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

• The links between the tree's


nodes are not actually stored
35 23
as pointers, or in any other way.
• The only way we "know" that
"the array is a tree" is from the 27 21

way we manipulate the data.

42 35 23 27 21

An array of data
Important Points about the
Implementation
42

• If you know the index of a node,


then it is easy to figure out the
35 23
indexes of that node's parent
and children.
27 21

42 35 23 27 21

[0] [1] [2] [3] [4]


Formulas for location children and
parents in an array representation
• Root at location [0]
• Parent of the node in [i] is at [(i-1)/2]
• Children of the node in [i] (if exist) is at [2i+1]
and [2i+2]
• Test:
– complete tree of 10, 000 nodes
– parent of 4999 is at (4999-1)/2 = 2499
– children of 4999 is at 9999 (V) and 10,000 (X)
Summary
• A heap is a complete binary tree, where the entry at
each node is greater than or equal to the entries in
its children.
• To add an entry to a heap, place the new entry at the
next available spot, and perform a reheapification
upward.
• To remove the biggest entry, move the last node
onto the root, and perform a reheapification
downward.

You might also like