Algorithm and data structure
  • Algorithm and data structure
  • Break Away: Programming And Coding Interviews & Cracking coding interview
    • System design and scalability
    • Performance and Complexity
    • Big O Notation
    • Sorting trade-off
    • Selection sort
    • Bubble sort
    • Insertion sort
    • Shell sort
    • Merge sort
    • Quick sort
    • Binary search
    • Recursion
    • Binary tree
    • Breadth First Traversal
    • Depth First Pre-Order
    • Binary search tree
    • Stack
    • Queue
    • Linked list
    • Doubly linked list
    • String
    • Pointer and array
    • Bit manipulation
    • General programming problems
    • Priority queue
    • Balanced binary search tree
    • Binary heap
    • Heap sort
    • Graphs
    • Topological sort
  • Leetcode
    • 1. Two Sum
    • 2. Add Two Numbers
    • 3. Longest Substring Without Repeating Characters
    • 4. Median of Two Sorted Arrays
    • 5. Longest Palindromic Substring
    • 6.ZigZag Conversion
    • 7. Reverse Integer
    • 8. String to Integer (atoi)
    • 9. Palindrome Number
    • 11. Container With Most Water
    • 12. Integer to Roman
    • 13. Roman to Integer
    • 14. Longest Common Prefix
    • 15. 3Sum
    • 16. 3Sum Closest
    • 17. Letter Combinations of a Phone Number
    • 18. 4Sum
    • 19. Remove Nth Node From End of List
    • 20. Valid Parentheses
    • 21. Merge Two Sorted Lists
    • 22. Generate Parentheses
    • 23. Merge k Sorted Lists
    • 24. Swap Nodes in Pairs
    • 25. Reverse Nodes in k-Group
    • 26. Remove Duplicates from Sorted Array
    • 27. Remove Element
    • 28. Implement strStr()
    • 29. Divide Two Integers
    • 30. Substring with Concatenation of All Words
    • 31. Next Permutation
    • 32. Longest Valid Parentheses
    • 33. Search in Rotated Sorted Array
    • 34. Find First and Last Position of Element in Sorted Array
    • 35. Search Insert Position (Easy)
    • 36. Valid Sudoku
    • 37. Sudoku Solver
    • 38. Count and Say
    • 39. Combination Sum
    • 40. Combination Sum II
    • 41. First Missing Positive
    • 43. Multiply Strings
    • 45. Jump Game II
    • 46. Permutations (Medium)
    • 47. Permutations II (Medium)
    • 48. Rotate Image
    • 49. Group Anagrams
    • 50. Pow(x, n)
    • 51. N-Queens
    • 52. N-Queens II
    • 53. Maximum Subarray
    • 54. Spiral Matrix
    • 55. Jump Game
    • 56. Merge Intervals
    • 57. Insert Interval
    • 58. Length of Last Word
    • 59. Spiral Matrix II
    • 61. Rotate List
    • 62. Unique Paths
    • 63. Unique Paths II
    • 64. Minimum Path Sum
    • 66. Plus One
    • 67. Add Binary
    • 69. Sqrt(x)
    • 70. Climbing Stairs
    • 73. Set Matrix Zeroes
    • 74. Search a 2D Matrix
    • 75. Sort Colors
    • 76. Minimum Window Substring
    • 77. Combinations
    • 78. Subsets
    • 79. Word Search
    • 80. Remove Duplicates from Sorted Array II
    • 81. Search in Rotated Sorted Array II
    • 82. Remove Duplicates from Sorted List II
    • 83. Remove Duplicates from Sorted List
    • 84. Largest Rectangle in Histogram
    • 86. Partition List
    • 88. Merge Sorted Array
    • 89. Gray Code
    • 90. Subsets II
    • 92. Reverse Linked List II
    • 94. Binary Tree Inorder Traversal (Medium)
    • 95. Unique Binary Search Trees II
    • 96. Unique Binary Search Trees
    • 98. Validate Binary Search Tree
    • 100. Same Tree (Easy)
    • 101. Symmetric Tree
    • 102. Binary Tree Level Order Traversal
    • 103. Binary Tree Zigzag Level Order Traversal
    • 104. Maximum Depth of Binary Tree
    • 105. Construct Binary Tree from Preorder and Inorder Traversal
    • 106. Construct Binary Tree from Inorder and Postorder Traversal
    • 107. Binary Tree Level Order Traversal II (Easy)
    • 108. Convert Sorted Array to Binary Search Tree
    • 109. Convert Sorted List to Binary Search Tree
    • 110. Balanced Binary Tree
    • 111. Minimum Depth of Binary Tree
    • 112. Path Sum
    • 113. Path Sum II
    • 114. Flatten Binary Tree to Linked List
    • 116. Populating Next Right Pointers in Each Node
    • 117. Populating Next Right Pointers in Each Node IIㄟˋ大
    • 118. Pascal's Triangle
    • 119. Pascal's Triangle II
    • 120. Triangle
    • 121. Best Time to Buy and Sell Stock
    • 122. Best Time to Buy and Sell Stock II
    • 123. Best Time to Buy and Sell Stock III
    • 125. Valid Palindrome
    • 654. Maximum Binary Tree
    • 127. Word Ladder
    • 129. Sum Root to Leaf Numbers
    • 130. Surrounded Regions (Medium)
    • 131. Palindrome Partitioning
    • 133. Clone Graph
    • 134. Gas Station
    • 136. Single Number
    • 137. Single Number II
    • 138. Copy List with Random Pointer
    • 139. Word Break
    • 141. Linked List Cycle
    • 143. Reorder List
    • 144. Binary Tree Preorder Traversal
    • 145. Binary Tree Postorder Traversal
    • 147. Insertion Sort List
    • 148. Sort List
    • 151. Reverse Words in a String
    • 152. Maximum Product Subarray
    • 153. Find Minimum in Rotated Sorted Array
    • 154. Find Minimum in Rotated Sorted Array II
    • 155. Min Stack
    • 160. Intersection of Two Linked Lists
    • 164. Maximum Gap
    • 169. Majority Element (Easy)
    • 173. Binary Search Tree Iterator
    • 174. Dungeon Game (Hard)
    • 189. Rotate Array
    • 198. House Robber (Easy)
    • 199. Binary Tree Right Side View (Medium)
    • 203. Remove Linked List Elements
    • 206. Reverse Linked List
    • 213. House Robber II (Medium)
    • 215. Kth Largest Element in an Array (Medium)
    • 222. Count Complete Tree Nodes
    • 226. Invert Binary Tree
    • 230. Kth Smallest Element in a BST
    • 232. Implement Queue using Stacks
    • 234. Palindrome Linked List (Easy)
    • 235. Lowest Common Ancestor of a Binary Search Tree
    • 236. Lowest Common Ancestor of a Binary Tree
    • 237. Delete Node in a Linked List
    • 240. Search a 2D Matrix II
    • 242. Valid Anagram
    • 257. Binary Tree Paths
    • 283. Move Zeroes
    • 337. House Robber III (Medium)
    • 347. Top K Frequent Elements
    • 349. Intersection of Two Arrays
    • 409. Longest Palindrome (Easy)
    • 437. Path Sum III
    • 442. Find All Duplicates in an Array
    • 449. Serialize and Deserialize BST
    • 450. Delete Node in a BST
    • 543. Diameter of Binary Tree
    • 572. Subtree of Another Tree
    • 653. Two Sum IV - Input is a BST
    • 654. Maximum Binary Tree
    • 700. Search in a Binary Search Tree
    • 701. Insert into a Binary Search Tree
    • 783. Minimum Distance Between BST Nodes
    • 876.Middle of the Linked List
    • 942. DI String Match
  • Notes of algorithms
    • Binary Tree traversal
    • 廣度優先搜尋 (Breadth-first Search)
    • Divide and Conquer
    • Linked list: Insert Node
    • Dynamic programming
    • 深度優先搜尋 (Depth-first Search)
    • Lowest Common Ancestor (LCA)
    • Asymptotic notation
    • Binary search tree
    • AVL Tree (Height Balanced BST)
    • Linked list: Split the list
    • Linked list: Traverse the list
    • Linked list: Delete node
    • Heap sort
    • Cartesian tree
  • C++
Powered by GitBook
On this page
  • 1.Introduction
  • 2. Logic
  • 3.Implementation
  • 4.Heapify
  • Sift down
  • Sift up
  • 5.Insert
  • 6.Remove
  • 7.Complexity
  • 8.Find the maximum element in a minimum heap
  • 9.Find the K largest elements in a stream
  • 10.Merge K sorted arrays using heaps
  • 11.Find the median elements in a stream
  • Tips
  • Implementation
  1. Break Away: Programming And Coding Interviews & Cracking coding interview

Binary heap

Binary heap use an implicit relationship specified by the indices

1.Introduction

  • A heap is just a tree with a special property or constraints on the values of its nodes

  • The tree with special properties or constraints on the value of its nodes these constraint called the heap property the constraint

  • Heap property

    • 1.Minimum heap

      • Every node value should be <= value of it's children

      • The node with the smallest value should be the root of the tree

    • 2.Maximum heap

      • Every node value should be >= value of it's children

      • The node with the largest value should be the root of the tree

  • Shape property

    • If H is the height of the tree - The leaf nodes should only be at level H or H - 1

      • Only the last level is incomplete in the heap

      • All nodes at level H - 1 have to be filled before moving on to level H

    • The heap should form a complete binary tree - all levels except the last should be filled

2. Logic

  • The operations typically performed on a heap requires us to

    • 1.Traverse downwards from the root towards.

    • 2.The leaf node Traverse upwards from the leaf nodes towards the root

  • On the heap we want to be able to

    • 1.Get left child

    • 2.Get right child

    • 3.Get parent

  • Every level of the binary tree in a heap is filled except perhaps the last

  • Implement by array:

    • Node at index i:

      • has a left child at index: 2i + 1

      • has a right child at index: 2i + 2

      • has parent at index: (i - 1)/2

3.Implementation

  • Base class

    • A generic heap can hold data of any type

    • The generic type has to extend comparable - This how we check the highest priority

public abstract class Heap<T extends Comparable> {
    private static int MAX_SIZE = 40;
    private T[] array;
    private int count = 0;
    
    public Heap(Class<T> clazz) {
        this(clazz, MAX_SIZE);
    }
    
    public Heap(Class<T> clazz, int size) {
        array = (T[]) Array.newInstance(clazz, size);
    }
    
    public int getLeftChild(int index) {
        int leftChildIndex = 2 * index + 1;
        if (leftChildIndex >= count) {
            return -1;
        }
    
        return leftChildIndex;
    }
    
    public int getRightChild(int index) {
        int rightChildIndex = 2 * index + 2;
        if (rightChildIndex >= count) {
            return -1;
        }
    
        return rightChildIndex;
    }
    
    public int getParentIndex(int index) {
        if (ijndex < 0 || index > count) {
            return -1;
        }
    
        return (index - 1)/2;
    }
    
    protected void swap(int index1, int index2) {
        T tempValue = array[index1];
        array[index1] = array[index2];
        array[index1] = tempValue;
    }
}

4.Heapify

  • While inserting or removing an element into the heap how do we know which is the right position for the element to occupy?

Sift down

  • An element is in the wrong position with respect to other elements bellow it in the heap

  • It has to be moved downwards in the heap towards the leaf nodes to find it's right position

protected void siftDown(int index) {
    int leftIndex = getLeftChildIndex(index);
    int rightIndex = getRightChildIndex(index);
    
    int smallerIndex = -1;
    if (leftIndex != -1 && rightIndex != -1) {
    smallerIndex = getElementAtIndex(leftIndex).compareTo(getElementAtIndex(rightIndex)) < 0
                        ? leftIndex : rightIndex;
    } else if (leftIndex != -1) {
       smallerIndex = leftIndex;
    } else if (rightIndex != -1) {
       smallerIndex = rightIndex;
    }
    
    if (smallerIndex == -1) {
        return;
    }
    
    if (getElementAtIndex(smallerIndex).compareTo(getElementAtIndex(index)) < 0) {
        swap(smallerIndex, index);
        siftDown(smallerIndex);
    }
}

Sift up

  • An element is in the wrong position with respect to other elements above it in the heap

  • It has to be moved upwards in the heap towards the leaf nodes to find it's right position

public void siftUp(int index){
    int parentIndex = getParentIndex(index);
    if (parentIndex != -1 &&
        getElementAtIndex(index).compareTo(getElementAtIndex(parentIndex)) < 0) {
        swap(parentIndex, index);
        siftUp(parentIndex);
    }
}

5.Insert

  • 插在Array的最後面, 然後Sift up上來

public void insert(T value) throws HeapFullExcepton {
    if (count >= array.length) {
        throw HeapFullExcepton);
    }
    
    account[count] = value;
    siftUp(count);
    
    count++;
}

6.Remove

  1. 移除最小值 (array[0])

  2. 將最大值移到root

  3. Sift down root

public T removeHighestPriority()(T value) throws HeapEmptyExcepton {
    T min = getighestPriority();
    array[0] = array[count - 1];
    if (count >= array.length) {
        throw HeapFullExcepton);
    }
    
    account[count] = value;
    count--;
    siftDown(0);
    return min;
}

public T getighestPriority() throws HeapEmptyExcepton {
    if (count == 0) {
        throw HeapEmptyExcepton);
    }
    
    return array[0];
}

7.Complexity

  • Insert: O(log n)

  • Access: O(1)

  • Remove: O(log n)

8.Find the maximum element in a minimum heap

  • 先找出最後一個node

  • 比較所有leaf node的大小 (最大值一定是其中一個leaf)

public static int getMaximum(MinHeap<Integer> minHeap) {
    int lastIndex = minHeap.getCount() - 1;
    int lastParentIndex = minHeap.getParentIndex();
    int firstChildIndex = lastParentIndex + 1;
    int maxElement = minHeap.getParentIndex(firstChildIndex);
    for (int i = firstChildIndex; i <= lastIndex; i++) {
        if (maxElement < minHeap.getElementAtIndex(i)) {
            maxElement = minHeap.getElementAtIndex(i);
        }
    }
    
    return maxElement;
}

9.Find the K largest elements in a stream

  • Use a minimum heap with size K to store elements as they come in

public static void printMaximumElements(int k) throws MinHeap.HeapEmptyExcepton, 
 MinHeap.HeapFullExcepton{
    MinHeap<Integer> minHeap = new MinHeap<>(Integer.class, k);
    
    for (int number : randomNumber) {
       if (minHeap.isEmpty()) {
           minHeap.insert(number);
       } else if (!minHeap.isFull || minHeap.getHighestPriority() < number) {
           if (minHeap.isFull()) {
               minHeap.removeHighestPriority();
           }
           minHeap.insert(number);
       }
    }
    minHeap.printArray();
}

10.Merge K sorted arrays using heaps

  • Remove the smallest element from each list and add it to the minimum heap - there will be K elements in the heap

public static void mergeKSortedLists(int totalElements, List<Integer... lists>) 
   throws MinHeap.HeapEmptyExcepton, MinHeap.HeapFullExcepton{
    MinHeap<Element> minHeap = new MinHeap<>(Element.class, lists.length);
    
    List<Integer> sorted = new ArrayList<>();
    for (int i = 0; i < lists.length; i++) {
        List<Integer> list = lists[i];
        if (!list.isEmpty()) {
            minHeap.insert(new Element(i, list.remove(0)));
        }
    }
    
    while (sortedList.size() < totalElements) {
        Element element = minHeap.removeHighestPriority();
        sortedList.add(element.getValue());
        
        List<Integer> list = lists[element.getListIndex()];
        if (!list.isEmpty()) {
            minHeap.insert(new Element(element.getListIndex(), list.remove(0)));
        }
    }
    printList(sortedList);
}

11.Find the median elements in a stream

Tips

  • The stream implies that we have no idea which elements are going in to come in text - we have to keep track of the median as the elements stream in

  • Use a max heap to store the smaller (first) half of elements seen in the stream

  • Use a min heap to store the larger (second) half of elements seen in the stream

  • 原則上, min heap及max heap的root都是stream的median elements

  • min heap的node值都小於平均值, max heap的node值都大於平均值

Implementation

  1. 第一個數字放到max heap

  2. 接下來的數字依序跟max heap比較, 如果比max heap的root大則放到min heap, 如果比max heap的root小則放到max heap

  3. 如果max heap及min heap個數差異超過1, 則必須rebalance: 從min heap中取出最小值並且加入max heap

  4. 如果兩個heap的數量相等, 則平均值為兩個heap root的平均值

  5. 否則平均值為數量多的heap的root值

public static double getStreamingMedian(int number) 
    throw MinHeap.HeapEmptyException, MinHeap.HeapFullException{
    
    if (!maxHeap.isEmpty() & number > maxHeap.getHighestPriority()) {
        minHeap.insert(number);
    } else {
        maxHeap.insert(number);
    }
    
    //rebalance
    if (minHeap.getCount() > maxHeap.getCount() + 1) {
        maxHeap.insert(minHeap.removeHighestPriority());
    } else if (maxHeap.getCount() > minHeap.getCount() + 1){
        minHeap.insert(maxHeap.removeHighestPriority());
    }
    
    if (maxHeap.getCount() == minHeap.getCount()) {
        return (maxHeap.getHighestPriority() + minHeap.getHighestPriority()) * 0.5;
    }
    
    return minHeap.getCount() > maxHeap.getCount() 
             ? minHeap.getHighestPriority()
             : maxHeap.getHighestPriority();
}

PreviousBalanced binary search treeNextHeap sort

Last updated 6 years ago