Skip to content

Commit 01735be

Browse files
author
freemanzhang
committed
add heapify
1 parent 5d56c84 commit 01735be

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

README.md

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# My whiteboard coding interview operating system !!!
1+
# Fight towards 1000 commits
22
<!-- MarkdownTOC -->
33

44
- [Typical whiteboard coding workflow](#typical-whiteboard-coding-workflow)
@@ -80,9 +80,10 @@
8080
- [Parentheses](#parentheses)
8181
- [Queue](#queue)
8282
- [Interfaces](#interfaces)
83-
- [PriorityQueue](#priorityqueue)
83+
- [Heap](#heap)
8484
- [Heapify](#heapify)
8585
- [Sift up/down](#sift-updown)
86+
- [PriorityQueue](#priorityqueue)
8687
- [Improve built-in remove O\(n\)](#improve-built-in-remove-on)
8788
- [Lambda expression as comparator](#lambda-expression-as-comparator)
8889
- [Top K problems](#top-k-problems)
@@ -1555,9 +1556,61 @@ boolean isValid( String s )
15551556
| examine | E element() | E peek() |
15561557

15571558

1558-
#### PriorityQueue
1559+
#### Heap
1560+
* Heap is a data structure, which usually have three methods: push, pop and top. where "push" add a new element the heap, "pop" delete the minimum/maximum element in the heap, "top" return the minimum/maximum element.
1561+
15591562
##### Heapify
1563+
* Def: Convert an unordered integer array into a heap array. If it is min-heap, for each element A[i], we will get A[i * 2 + 1] >= A[i] and A[i * 2 + 2] >= A[i].
1564+
* Heapify is an O(n) operation. The reason is as follows:
1565+
- At the bottommost level, there are 2^(h)nodes, but we do not call heapify on any of these, so the work is 0. At the next to level there are 2^(h − 1) nodes, and each might move down by 1 level. At the 3rd level from the bottom, there are 2^(h − 2) nodes, and each might move down by 2 levels.
1566+
15601567
##### Sift up/down
1568+
1569+
```java
1570+
private void siftdown( int[] A, int k )
1571+
{
1572+
while ( k < A.length )
1573+
{
1574+
int smallest = k;
1575+
if ( k * 2 + 1 < A.length && A[k * 2 + 1] < A[smallest] )
1576+
{
1577+
smallest = k * 2 + 1;
1578+
}
1579+
if ( k * 2 + 2 < A.length && A[k * 2 + 2] < A[smallest] )
1580+
{
1581+
smallest = k * 2 + 2;
1582+
}
1583+
1584+
if ( smallest == k )
1585+
{
1586+
break;
1587+
}
1588+
else
1589+
{
1590+
swap( A, smallest, k );
1591+
k = smallest;
1592+
}
1593+
}
1594+
}
1595+
1596+
private void swap( int[] A, int pos1, int pos2 )
1597+
{
1598+
if ( pos1 != pos2 )
1599+
{
1600+
int temp = A[smallest];
1601+
A[smallest] = A[k];
1602+
A[k] = temp;
1603+
}
1604+
}
1605+
1606+
public void heapify(int[] A) {
1607+
for (int i = A.length / 2; i >= 0; i--) {
1608+
siftdown(A, i);
1609+
}
1610+
}
1611+
```
1612+
1613+
#### PriorityQueue
15611614
##### Improve built-in remove O(n)
15621615
* Built-in implementation remove() method for priorityqueue has O(n) time complexity.
15631616
- O(n) time is spent on looping through entire queue to find the element to be removed. O(logn) is used to remove the element

0 commit comments

Comments
 (0)