Professional Documents
Culture Documents
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.
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
National University
Page
National University
Page
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
8
2
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
National University
Page
65
31
16 16 68 24 21
19
31
68
26
32
26
32
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
National University
Page
10
10 7 5 6 13
15 14
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
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
National University
Page
13
19
31
68
32
31
14 19 65
26 21 32 19
16 68
National University
Page
15
14 31 19 65 21 16
19
68
26
32
14 19 31 65 26 21 32 19 16 68
14
19 26 65 31 21 32 19 16 68
14 19 26 65 31 21 32 19 16 68
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