Heap is Data Structure like a Binary Tree.
If it is stored in an array, then every node's parent, left child, right child could be calculated directly.
Parent(i): return i/2
Left(i): return 2*i
Right(i): return 2*i + 1
Max-Heap: A[parent(i)] >= A[i] ->use for heap-sort
Min-Heap: A[parent(i)] <= A[i] ->use for priority queue
Height of a node: # of edge on the longest path to a leaf.
Height of a heap: height of root. ->O(lgn)
No comments:
Post a Comment