You are on page 1of 14

1

Extra Notes on Heaps


By
Teo Kim Heng
2


Definition: A binary tree is either an empty tree or it
contains a node called the root together with two
subtrees, called the left subtree and right subtree of the
root.
Definition: A binary search tree (BST) is a binary tree that
is either empty or in which each node contains a key that
satisfies the following BST properties:
1. Any key in the left subtree is smaller than the key
in the root;
2. Any key in the right subtree is greater than the key
in the root;
3. The left subtree and right subtree are again binary
search trees.

X
T1 T2
A Non Empty Binary
Tree with root node X,
left subtree T1 and
right subtree T2
2
1
9
5
3
7
A 5-node BST
3


Definition: Let N be a node in a binary tree. If N has
a nonempty left subtree, then the root of the left
subtree of N is called the left child of N. Similarly,
the root of the right subtree of N, if exists, is called
the right child of N.
Definition: If M is a child (either left child or right
child) of N, then N is called the parent of M. A pair
of nodes with the parent-child relationship is called
a branch or edge.
Definition: Node M is an ancestor of a node N iff
(i) M = N or;
(ii) M is the parent of Z and Z is an ancestor of N.

Definition: The number of children of a node N is
called the (out) degree of N. A node with degree
zero is called a leaf. A node with degree greater
than zero is called an internal/interior node.

4


Definition: The size of a tree T, denoted as size(T), is the
number of nodes in T. Formally, it can be defined as
follows:
size(T) = 0, if T is empty;
size(T) = size(L) + size(R)) + 1,otherwise,
where L/R are the left/right subtrees of the root.
Definition: The height of a binary tree T, denoted as
height(T), is defined as follows:
height(T) = -1, if T is empty;
height(T) = max(height(L), height(R)) + 1, otherwise,
where L/R are the left/right subtrees of the root.

X
A Non Empty
Binary Tree T, with
size(T)=5 and
height(T)=2
Y
W Z
U
5


The complexity of most binary tree operations is of
O(height(T)). What then are the maximum and
minimum heights of binary trees?
We can maximize the height of an n-node binary
tree simply by giving each node exactly one child
(why?) which results in a tree of height n-1. Such
trees resemble a linear list. Two particular such
trees are called left and right skewed binary tree,
respectively.



A Left Skewed
binary tree







A Right Skewed
binary tree
6


Minimum Height Of A Binary Tree
Intuitively, to minimize the height of a binary tree given n
nodes, we must fill each level of the tree as completely as
possible. Such trees are called complete binary trees
which are defined below:
Definition: A full binary tree of height h has all its leaves
at level h and all internal nodes are of degree two.
Definition: A complete binary tree of height h is full down
to level h-1, with level h filled in from left to right.



A 7-node Full binary
tree with height = 2






A 9-node complete binary
tree with height = 3






7


Array representation of complete binary trees
The data in a complete binary tree can be stored on
an array using any of its preorder, post-order, in-
order or row-order sequence. In the following
discussion, we shall use row-order sequence.
7
11
21
1
31
1
9
13
55
88 67
0 1 2 3 4 5 6 7 8
7 11 9 21 31 13 55 88 67

Observation:
The root item is at position 0;
The left child of node i, if exists, is at 2*i+1;
The right child of node i, if exists, is at 2*i+2;
The parent of node i, if exists, is at (i-1) div 2.

0
1
3
4
2
5
6
7 8
8


Definition: A Minimum Heap is a complete binary
tree satisfying the following property.
Minimum Heap Property: The value stored at each
node is not larger than the values of its children (if
they exist)

The following figure shows a 9-node minimum heap
and its array representation
7
11
21
1
31
1
9
13
55
88 67
0 1 2 3 4 5 6 7 8
7 11 9 21 31 13 55 88 67

0
1
3
4
2
5
6
7 8
9


Definition: A Maximum Heap is a complete binary
tree satisfying the following property.
Maximum Heap Property: The value stored at each
node is not smaller than the values of its children (if
they exist)

The following figure shows a 9-node maximum heap
and its array representation
88
67
55
7
31
13
11
21 9
0 1 2 3 4 5 6 7 8
88 67 31 55 7 13 11 21 9

0
1
3
4
2
5
6
7 8
10


public class MaxHeap {
private Data [] a; // array representation of the heap
private int capacity; // maximum size of the heap
private int n = 0; // size of the heap
public MaxHeap (int capacity) { //O(1)
a = new Data [capacity];
this.capacity = capacity;
}
public boolean isFull () { return n==capacity;} // O(1)
public boolean isEmpty () { return n==0;} // O(1)

public Data getMax() // O(1)
// pre: the heap is not empty
{
return a[0];
}

}

11


public class MaxHeap {
public void insert(Data d) // O(log(n))
// pre: the heap is not full
{
a[n]=d;
n=n+1;
trickeUp (n-1);
}

private void Data trickeUp (int i) { // O(log(n))
if (i == 0) { return; } // done if at root
int parent = i/2; // has parent
if (a[parent].key > a[i].key) { return; } // done if is in order
swap(a[parent], a[i]);
trickeUp (parent);
}
}

12


For examples of insertion
operation, see slide 28 to 53
13


public class MaxHeap {
public Data deleteMax() // O(log(n))
// pre: the heap is not empty
{ Data data = a[0];
a[0]=a[n-1]; n=n-1;
trickeDown (0); return data;
}
private void Data trickeDown (int i) { // O(log(n))
if (2*i + 1 >= n) { return; } // done if no children
int largestChild = 2*i+1; // has left child
if (2*i + 2 < n) { has right child
if (a[2*i+1].key < a[2*i + 2].key) // right child is larger
largestChild = 2*i + 2;
}
if (a[i] >= a[largestChild] { return; } done if in order
swap(a[i], a[largestChild]);
trickeDown (largestChild);
}
}
14


public void heapSort (Data [] array, int n)
// pre: array contains a set of n data
// post: array contains the same set of data in increasing order
{
MaxHeap heap = new MaxHeap (n);
int i;

// Step 1: Heapify -- O(nlog(n))
for (i = 0; i < n; i++) {
heap.insert (array[i]);
}

// Step 2: Extract Element -- O(nlog(n))
for (i = n-1; i >= 0; i--) {
array[i] = heap.deleteMax();
}
}

Note: The heapify operation can be done in O(n) time

You might also like