Skip to content

Commit 80d5bae

Browse files
author
Antesh Sharma
committed
heap max insert and heapify
1 parent e0d7ec8 commit 80d5bae

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

src/main/java/com/antesh/dsa/heap/README.md

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff 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
6363
This step is performed using recursion, and done until the inserted element finds its correct position in the heap.
6464

6565
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-
<p align="center">
68-
<img src="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+
<p align="center">
68+
<img src="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.
7272

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**.
7474

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.
7676

77-
So, let us understand how to **heapify** a binary tree.
77+
So, let us understand how to **heapify** a binary tree.
7878

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.
8181

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 –
8383

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
8585

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-
<p align="center">
88-
<img src="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+
<p align="center">
88+
<img src="max-heapify-step-1.jpg">
89+
<br/>
90+
</p>
9191

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.
9393

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 –
9595

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.
9797

98-
<p align="center">
99-
<img src="max-heapify-step-2.jpg">
100-
<br/>
101-
</p>
98+
<p align="center">
99+
<img src="max-heapify-step-2.jpg">
100+
<br/>
101+
</p>
102102

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.
104104

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.
106106

107-
<p align="center">
108-
<img src="max-heapify-step-3.jpg">
109-
<br/>
110-
</p>
107+
<p align="center">
108+
<img src="max-heapify-step-3.jpg">
109+
<br/>
110+
</p>
111111

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.
113113

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.
115115

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.
117117

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

Comments
 (0)