You are on page 1of 19

Heap Trees

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page

Heaps
A particular kind of binary tree called min-Heap or just Heap has the following properties: The value of each node is less than the values stored in each of its children The tree is perfectly balanced and leaves in the last level are all in the leftmost position Similarly, in a max Heap tree the value of any node is greater than the values of its children. Therefore, the root has the highest value in the tree.

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page

An example of a Min-Heap
A B D H I E C

A
0 1

B
2

C
3

D
4

E
5

F
6

G
7

H
8

I
9

J
10 11 12 13

Note that for every element i, its left child is located at 2*i and its right child is located at 2*i+1 and its parent is at location i/2

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page

Inserting into a Max-Heap Tree


To add an element, the element is added at the end of the heap as the last leaf Restoring the heap property in the case of inserting a new node is achieved by moving from the last node (the node that was just added) to the root. This is called percolating up the tree Thus to insert a new node, node P, to the heap Put P at the end of the heap While P is not the root and value of P > value of parent (p) Swap P with its parent

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page

Example: Insert 15 to the following heap tree


20 20 18 7 5 6 13 14 2 8 5 6 10 7 15 13 P 18 14

10 8 2

Insert 15 (call it node P): 15 is added as the last leaf. Now we need to swap P with its parent because Ps value is greater
20 10 18 7 5 6 15 P
Page 5

8 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh

13

14

National University

P is still greater than its parent. Thus we need to do another swap


20 10
P 18 15 5 6 13 14

8
2

P is not greater than its parent anymore. So we are done


20 P 15 8 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh

18 10

13
7

14

National University

Page

Another way of looking at it (array form) Lets consider the Min-Heap this time
To insert an element P into a heap, Create a hole in the next available location If P can be place in the hole without violating heap order, then we do so and we are done; Otherwise, we slide the element that is in the holes parent into the hole, thus bubbling the hole up toward the root We continue until P can be placed into the hole

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page

Trying to insert 14 into the following heap


13 21 24 31 26 32 19 16 68 65 24 26 32 21 19 13 16 68

65

31

14 < 31 move parent to the hole

13 13 14 24 65 21 19 31 65 14 > 13 move 14 to the hole


A.R. Hadaegh Dr. Ahmad R. Hadaegh National University

16 16 68 24 21

19
31

68

26

32

26

32

14 < 21 move parent to the hole


Page 8

Insert Algorithm
void BinaryHeap::insert(const int& x) { if (isfull()) Compares the value throw Overflow(); with its parent // percolate up int hole = ++currentsize; // creating a new hole (position) for (; hole > 1 && x < array[hole/2]; hole=hole/2) array[hole] = array [hole/2]; // bubbling the hole up array[hole] = x; // Placing the value into the hole }
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 9

Algorithm for Deleting From a Max-Heap Tree


Locate the node to be deleted and call it node P Locate the last node in the heap Swap the values of P with the last node Delete the last node While P is not a leaf, and P < any of its children Swap P with the larger child

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page

10

Example of deleting an element from a max-heap tree


20

Suppose we want to delete 20


8

10 7 5 6 13

15 14

We swap the last element with 20 first


6 10 15 7 5 20
National University Page 11

8 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh

13

14

P 6

Now we delete 20
8 2 5

10 7 20 13

15 14

Swap P with its larger child if the value of P is lower than the larger child P P
6 10 8 2 5
National University

15 15 10 14 2 8 5
Page 12

6 7 13 14

13

A.R. Hadaegh Dr. Ahmad R. Hadaegh

Swap P with its larger child if the value of P is lower than the larger child
15
P

10
8 2 5 7 13

6 14

15 10 8 2 5 7 13

14
P

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page

13

Other operations include:


DescreaseKey(p, delta): Lowers the values of the item at position p by the positive amount delta This may violate the heap. We may need to percolate up to restore the heap in a min-heap IncreaseKey(p, delta): Increases the values of the item at position p by the positive amount delta This may violate the heap. We may need to percolate down to restore the heap in a min-heap Remove(p) To remove an item in position p, we can Apply Decreasekep(p, infinity) to move it up to the root Apply deleteMin to remove it from the min-heap
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 14

Trying to delete 13 from the heap


13 14 19 65 26 21 16

19
31

68

32

31

14 19 65
26 21 32 19

16 68

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page

15

14 31 19 65 21 16

19

68

26

32

Both children, 14 and 16 are smaller than 31 we move 14 to the hole

14 19 31 65 26 21 32 19 16 68

Both children, 19 and 21 are smaller than 31 we move 19 to the hole


A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 16

14
19 26 65 31 21 32 19 16 68

One child, 26, is smaller than 31 we move 26 to the hole

14 19 26 65 31 21 32 19 16 68

This is the final tree after 13 is deleted and heap is restored


A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 17

deleteMin Algorithm
void BinaryHeap::deleteMin (int& minItem) { if (isEmpthy()) throw Underflow(); minItem = array[1]; array[1] = array [currentsize - -]; // moves the value of the last node into the first node percolateDown(1); } //--------------------------------------------------------------------------------------------------------------------void BinaryHeap ::precolateDown(int hole) { int child; int tmp = array[hole]; // stores the value of the last element in the heap into a temp variable for (; hole*2 <=currentSize; hole=child) { child = hole*2; // sets the child to the left child if (child != currentSize && array[child+1] < array[child]) // compares left with right child array[hole] = array[child]; // bubbling the hole down the tree else break; } array[hole] = tmp; // hole is found and the last item is placed there }
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 18

Another way of looking at it (array form)


Lets again consider the Min-Heap For the deleteMin operation Since the minimum is at the root, removing the minimum creates a hole at the root of the tree. Let P be the last element in the heap If P can be place in the hole without violating heap order, then we do so and we are done; (This is unlikely) Otherwise, compare the children of the hole and move the smaller one to the hole bubbling the hole down toward the bottom We continue until P can be placed into the hole
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 19

You might also like