You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/com/antesh/dsa/heap/README.md
+94-4Lines changed: 94 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Heap
2
2
## Complete Binary Tree Definition
3
-
- A binary tree is called complete binary tree when all the level of binary tree is completely filled except possibly the last level, which is filled from left side to right.
3
+
- A binary tree is called complete binary tree when all the level of binary tree is completely filled except possibly the last level, which is always filled from left to right.
4
4
- Difference between a complete binary tree and a full binary tree is that, in a complete binary tree all leaf nodes should lean towards left and the last nodes need not to have a right sibling.
5
5
6
6
@@ -21,9 +21,99 @@ There are two kinds of heaps: **Max Heap** and **Min Heap**
21
21
**Max Heap:** Each node in the Heap has a value no less than its child nodes. Therefore, the top element (root node) has the largest value in the Heap.
22
22
23
23
**Min Heap:** Each node in the Heap has a value no larger than its child nodes. Therefore, the top element (root node) has the smallest value in the Heap.
Consider we have an array with elements 10, 8, 5, 15, 6 in it. To build a max-heap using the given elements, Follow the under-given steps –
31
+
- Add the first element, 10 in the tree. It is the root element in the tree.
32
+
<palign="center">
33
+
<imgsrc="max-heap-insertion-step-1.jpg">
34
+
<br/>
35
+
</p>
36
+
- Add the next element in the tree. Compare it with the parent element. If it is greater than its parent element, swap their positions. It is done to ensure that the tree follows heap conditions, and a max-heap is maintained each time an element is added. Here, we should add the second element, 8 as the left child of the root element because a heap is filled from left to right. No swapping occurs here because 10 is greater than 8.
37
+
<palign="center">
38
+
<imgsrc="max-heap-insertion-step-2.jpg">
39
+
<br/>
40
+
</p>
41
+
- Repeat the above-given step with the next element, 5.
42
+
<palign="center">
43
+
<imgsrc="max-heap-insertion-step-3.jpg">
44
+
<br/>
45
+
</p>
46
+
- Add the next element, 15 in the tree.
47
+
<palign="center">
48
+
<imgsrc="max-heap-insertion-step-4.jpg">
49
+
<br/>
50
+
</p>
51
+
Now since 15 is greater than its parent element 8, this tree is not a max-heap anymore. To make it a heap again, we will swap the positions of 8 and 15.
52
+
<palign="center">
53
+
<imgsrc="max-heap-insertion-step-4a.jpg">
54
+
<br/>
55
+
</p>
56
+
57
+
Again the obtained tree is not a max-heap since 15 is greater than its parent element, 10. We will again swap the positions of 10 & 15.
58
+
<palign="center">
59
+
<imgsrc="max-heap-insertion-step-4b.jpg">
60
+
<br/>
61
+
</p>
62
+
63
+
This step is performed using recursion, and done until the inserted element finds its correct position in the heap.
64
+
65
+
Notice that 15 was first added at the bottom of the tree and then moved up to its correct position. This moving up of elements is known as **bubbling up**.
66
+
- Add the last element, 6 in the heap by comparing it with the parent.
67
+
<palign="center">
68
+
<imgsrc="max-heap-insertion-step-5.jpg">
69
+
<br/>
70
+
</p>
71
+
With this, we have added all the elements of the given array into a heap.
72
+
73
+
One thing to note here is that a comparison is done each time an element is added. The number of comparisons also depends on the height of the tree. In the above case, a total of 5 comparisons were made. This results in a time complexity of **O(nlogn)** since the height of a binary tree is **logn**.
74
+
75
+
But we can reduce the number of comparisons by using a method called **heapify** where elements are first added into the tree and then arranged in a bottom-up fashion. It helps in reducing the number of comparisons, and thus the time complexity of the overall algorithm.
76
+
77
+
So, let us understand how to **heapify** a binary tree.
78
+
79
+
## How to Heapify a Binary Tree?
80
+
**Heapify** is the process of rearranging the elements to form a tree that maintains the properties of the heap data structure.
81
+
82
+
Recall the list/array that had the elements – 10, 8, 5, 15, 6 in it. To heapify these elements, and form a max-heap, let us follow the under-given steps –
83
+
84
+
- Visualize all the elements of the list as a complete binary tree
85
+
86
+
Treat the elements of the given array as the nodes of a tree. To visualize an array as a binary tree, refer to the part where we have discussed the array representation of the binary tree.
87
+
<palign="center">
88
+
<imgsrc="max-heapify-step-1.jpg">
89
+
<br/>
90
+
</p>
91
+
92
+
Notice how the above-given binary tree is a complete binary tree but does not satisfy the properties of a max-heap since element 8 has an element greater than itself as its child.
93
+
94
+
- Start from comparing the values of children nodes with that of the parent. If the value of the parent is smaller than the values of the children, swap it. Swapping is done with a larger of two children. This process is repeated until every node satisfy the properties of a max-heap –
95
+
96
+
Here, we start comparing 8 with 15 and 6. Now, since 15 is greater than 8, we will swap their positions.
97
+
98
+
<palign="center">
99
+
<imgsrc="max-heapify-step-2.jpg">
100
+
<br/>
101
+
</p>
102
+
103
+
Again, the property of max-heap is not satisfied since 15 is greater than 10. Therefore, we will once again perform the above step.
104
+
105
+
Now that we have obtained a max-heap, we can stop this step.
106
+
107
+
<palign="center">
108
+
<imgsrc="max-heapify-step-3.jpg">
109
+
<br/>
110
+
</p>
111
+
112
+
One interesting thing to note here is that a node can be heapified iff all the children nodes are already heapified. This is the reason why we start from the bottom-most sub-tree.
113
+
114
+
This step is also performed using recursion. We create a function called heapify() that works by dividing the tree into smaller sub-trees and then comparing the values of parents with that of children in each sub-tree.
115
+
116
+
Notice that the number of comparisons are reduced slightly. This way, we can significantly reduce the comparisons if there are many elements to be added.
117
+
118
+
Here, the time complexity would be equal to the height of tree O(logn). This is because a node will be compared only to its parent, and thus, will be swapped at most O(logn) times.
0 commit comments