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
+35-36Lines changed: 35 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -63,57 +63,56 @@ Consider we have an array with elements 10, 8, 5, 15, 6 in it. To build a max-he
63
63
This step is performed using recursion, and done until the inserted element finds its correct position in the heap.
64
64
65
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.
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
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**.
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
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.
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
76
77
-
So, let us understand how to **heapify** a binary tree.
77
+
So, let us understand how to **heapify** a binary tree.
78
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.
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
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 –
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
83
84
-
- Visualize all the elements of the list as a complete binary tree
84
+
- Visualize all the elements of the list as a complete binary tree
85
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>
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
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.
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
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 –
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
95
96
-
Here, we start comparing 8 with 15 and 6. Now, since 15 is greater than 8, we will swap their positions.
96
+
Here, we start comparing 8 with 15 and 6. Now, since 15 is greater than 8, we will swap their positions.
97
97
98
-
<palign="center">
99
-
<imgsrc="max-heapify-step-2.jpg">
100
-
<br/>
101
-
</p>
98
+
<palign="center">
99
+
<imgsrc="max-heapify-step-2.jpg">
100
+
<br/>
101
+
</p>
102
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.
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
104
105
-
Now that we have obtained a max-heap, we can stop this step.
105
+
Now that we have obtained a max-heap, we can stop this step.
106
106
107
-
<palign="center">
108
-
<imgsrc="max-heapify-step-3.jpg">
109
-
<br/>
110
-
</p>
107
+
<palign="center">
108
+
<imgsrc="max-heapify-step-3.jpg">
109
+
<br/>
110
+
</p>
111
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.
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
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.
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
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.
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
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.
119
-
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