In: Computer Science
Imagine a set Implementation which uses heaps, instead of binary search trees. How would performance of such a data structure differ from a set Implementation using Balanced Trees(AVL or Red/Black Trees)? Performance meaning RunTime(Time complexity) of the methods in the data structure ex. add()
If Balanced Tree is used to implement a set, then time complexity of finding an element will be O(log n) and same time complexity holds for insertion and deletion in Balanced trees. So insertion, deletion and search, all these operations take O(log n) time in balanced tree.
However if a set is implemented using heap, then insertion of new element into heap requires O(log n) time, since heap always have depth O(log n), however heap requires O(n) time to find an element with a particular value. Hence heap is not a good data structure for searching an element of arbitrary value. Since time complexity of searching is O(n), so time complexity of removal of any arbitrary element will also take O(n).
So heap is not very good data structure to implement a set if we are interested in finding any arbitrary element , however it is good data structure to implement priority queue such that high priority element is always closer to root.
Please comment for any clarification.