7.2Heap with Linked Nodes
A heap of nodes: parent and the last place
A heap is almost always written with an array, but the same structure can be built with linked nodes. Two problems come up: sift up has to reach the parent, and insertion and removal must know the last position of the tree. In an array both were a formula. In this lesson we add a parent pointer to the node and write the function that finds the last position by walking down from the root.
- Why a heap node also carries a
parentpointer - How to name nodes by position numbers starting at 1
- How to reach the node at position k in O(log n) by following k's binary digits
1Idea
The node holds a value and left and right pointers, as in a linked binary tree. A parent pointer is added; at the root, parent is NULL. Sift up climbs to the parent at every step; without parent it would have to search for the parent from the root each time. The heap itself keeps the root and the number of nodes: root and count.
parent pointer (dashed arrows). The root's parent field is NULL.A heap is always complete, so we can number its nodes in level order. In this lesson numbers start at 1: the root is 1, its children 2 and 3, theirs 4, 5, 6, 7. That is the array index plus one. Starting at 1 has a benefit: position k has its children at 2k and 2k+1 and its parent at k/2.
This numbering has a neat consequence. Write k in binary: the leading 1 stands for the root, and every bit after it is a step: 0 means go left, 1 means go right. For example, 6 is 110 in binary. Drop the leading 1 and 1 and 0 are left: first right, then left. Root 1, its right child 3, that node's left child 6. Indeed 6 = 2·3.
nodeAt does exactly this. First it finds the value of the leftmost 1 bit of k (4 for 6). Then it reads the bits to the right of it from left to right and goes down one level for each. Insertion will use this function to find the parent of position count + 1, and removal to find the last node at position count.
For an invalid position (k < 1 or k > count) the function returns NULL. k == count + 1 is a position that does not exist yet; insertion goes not to it but to its parent, (count + 1) / 2.
2Step by step
The heap in the drawing is 10, 20, 15, 40, 50, 30. The numbers under the nodes are position numbers. We call nodeAt(6): 6 is 110 in binary.
- The heap has 6 nodes;
countis 6. Every node knows its parent throughparent(not drawn here). k = 6is a valid position. The leftmost 1 bit of 6 is worth 4 (the leading 1 in 110).bitbecomes 4.nstarts at the root.bitdrops to 2: this bit of 6 is 1.- The bit is 1, so we go right:
nis now node 3, holding 15.bitdrops to 1: this bit of 6 is 0. - The bit is 0, so we go left:
nis node 6, holding 30.bitbecomes 0. - The loop ends; the function returns the node holding 30. Two steps were enough: as many as the height of the tree.
struct HeapNode { int data; struct HeapNode* left; struct HeapNode* right; struct HeapNode* parent; // NULL at the root}; struct Heap { struct HeapNode* root; int count;}; void create(struct Heap* h) { h->root = NULL; h->count = 0;} struct HeapNode* nodeAt(struct Heap* h, int k) { if (k < 1 || k > h->count) return NULL; int bit = 1; while (bit * 2 <= k) bit *= 2; // the leftmost 1 bit of k struct HeapNode* n = h->root; for (bit /= 2; bit > 0; bit /= 2) { if (k & bit) n = n->right; // 1: go right else n = n->left; // 0: go left } return n;}class HeapNode { int data; HeapNode left, right, parent; // parent is null at the root HeapNode(int data) { this.data = data; }} class LinkedHeap { HeapNode root; int count = 0; HeapNode nodeAt(int k) { if (k < 1 || k > count) return null; int bit = 1; while (bit * 2 <= k) bit *= 2; // the leftmost 1 bit of k HeapNode n = root; for (bit /= 2; bit > 0; bit /= 2) { if ((k & bit) != 0) n = n.right; // 1: go right else n = n.left; // 0: go left } return n; }}class HeapNode: def __init__(self, data): self.data = data self.left = None self.right = None self.parent = None # None at the root class LinkedHeap: def __init__(self): self.root = None self.count = 0 def node_at(self, k): if k < 1 or k > self.count: return None bit = 1 while bit * 2 <= k: bit *= 2 # the leftmost 1 bit of k n = self.root bit //= 2 while bit > 0: n = n.right if k & bit else n.left # 1: right, 0: left bit //= 2 return nThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Finding the leftmost bit and walking down from the root both take as many steps as k has bits. Since k ≤ n, that is at most log₂ n + 1 steps.
A few variables are enough.
In an array position k is simply items[k-1]: O(1). The linked structure walks down from the root on every access. This does not change the time of insertion and removal (they are O(log n) anyway), but it makes the constant factor larger.
4Common mistake
A common mistake is reading the bits from right to left, starting with the lowest bit:
while (k > 1) { n = (k % 2) ? n->right : n->left; // wrong: the path is read backwards k /= 2;}The lowest bit tells you the last step: whether the node is its parent's left or right child. Going down from the root, the first step is the bit just after the leading 1. For 6 the right path is right, left; read backwards it becomes left, right, which leads to node 5.
5Try it
How do you walk down from the root to position 11?
The answer is A. 11 is 1011 in binary. Drop the leading 1 and 0, 1, 1 remain: left, right, right. Check: 1, 2, 5, 11; each step is 2k or 2k+1.
Show the answer
The answer is A. 11 is 1011 in binary. Drop the leading 1 and 0, 1, 1 remain: left, right, right. Check: 1, 2, 5, 11; each step is 2k or 2k+1.
Why does a node in a linked heap carry a parent pointer?
The answer is A. Sift up climbs to the parent at every step. Without parent, the parent would be searched for from the root each time. Sift down uses only left and right.
Show the answer
The answer is A. Sift up climbs to the parent at every step. Without parent, the parent would be searched for from the root each time. Sift down uses only left and right.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.