Heap
Heap
Trees and
Heaps
Presenter :- Anwar Misbah
Topics to be covered
Heap Implementation
The BST is one way to organize data in a tree structure. In each subtree
rooted at x, the following BST property holds: Items on the left subtree of
x are smaller than x and items on the right subtree of x are greater than
(or equal to) x.
In the worst case, only O(log n) operations are
required in a root-to-leaf scan . However, this only
holds if the BST is balanced.
3
We then insert the value 8. Since 8 is greater than 5, it
becomes the right child of the root:
3 8
Next, we insert the value 1. Since 1 is less than 5 and 3, it
becomes the left child of 3
3 8
1
We then insert the value 6. Since 6 is greater than 5 and
less than 8, it becomes the right child of 3
3 8
1 6
Finally, we insert the value 9. Since 9 is greater than 5
and 8, it becomes the right child of 8
3 8
1 6 9
To search for a value in the tree, we start at the
root and compare the value we are searching
for with the value of the current node.
The (Binary) Heap is also a binary tree like the BST, except that it must be a complete tree.
Complete binary trees can be stored efficiently in a compact 1-indexed array of size n + 1,
which is often preferred to an explicit tree representation.
A heap is a binary tree where each node is either greater than or equal to (max heap) or less than or
equal to (min heap) its children.
The root node of the heap is either the minimum or maximum element of the heap, depending on
whether it is a min heap or max heap.
There are two main types of heaps: max heaps and min heaps.
Max heap
In a max heap, the root node is the maximum element in the heap, and each
parent node is greater than or equal to its children.
We can begin by adding the first element to an empty heap in order to generate a
max heap. Then, in order to retain the maximum heap property, we put each
additional member into the heap and "bubble up" the element to its proper
location.
Insert 5: The heap is empty, so we insert 5 as the root node.
5
Then, Insert 8 We insert 8 as the left child of the root. Since 8 is greater
than 5, we swap the two elements.
5
Then Insert 3: We insert 3 as the left child of 8. Since 8 is still greater than 3,
we do not need to swap any elements. However, 3 is less than its parent, so
we need to bubble it up further.
5 3
Insert 10 We insert 10 as the right child of 8. Since 10 is greater than both its
parent and grandparent, we swap it with 8 and then with 5.
10
5 8
3 2
Insert 2: We insert 2 as the left child of 5. Since 5 is greater than 2, we do not
need to swap any elements. However, 2 is less than its parent, so we need
to bubble it up further.
10
5 8
3 2
7
Insert 7: We insert 7 as the right child of 2. Since 7 is greater than its parent,
we swap it with 2 and then with 5.
10
7 8
3 2
To create a min heap, we can follow a similar process as max heap, but
maintain the min heap property instead.
Insert 5: The heap is empty, so we insert 5 as the root node.
5
Then, Insert 8: We insert 8 as the left child of the root. Since 5 is less than 8,
we do not need to swap any elements.
8
Then Insert 3: We insert 3 as the left child of 8. Since 3 is less than both its
parent and grandparent, we swap it with 8 and then with 5.
8 5
Insert 10: We insert 10 as the right child of 8. Since 8 is less than 10, we do not
need to swap any elements.
8 5
10
Insert 2: We insert 2 as the left child of 8. Since 2 is less than both its parent
and grandparent, we swap it with 8 and then with 3.
8 5
10 3
Insert 7: We insert 7 as the right child of 3. Since 3 is less than 7, we do not
need to swap any elements.
8 5
10 3
Thank You
Questions ?