Sunday, March 5, 2017

CLRS Exercise 12.1-2

12.1-2
What is the difference between the binary-search-tree property and the min-heap property (see page 153)? Can the min-heap property be used to print out the keys of an n-node tree in sorted order in $O(n)$ time? Show how, or explain why not.

In the Min-Heap, both child nodes are less than or equal to their parents, while in the binary-search-tree, left child node cannot be greater than its parent and right child node cannot be less than. Thus, in-order traversal for BST gives nodes in sorted order with $\Theta(n)$ time complexity, but in the Min-Heap we need $n$ times of EXTRACT-MIN followed by HEAPIFY operations to get the same result, which takes $O(n\log{}n)$ time.

No comments: