You cannot find an arbitrary element on the heap in O(log n) . This requires O(n) . You can find the first element (the smallest, say) on the heap in O(1) and extract it in O(log n) . Red-black trees and heaps have completely different uses, internal ordering and implementation: see below for more details.
Typical use
Red-black tree: saving the dictionary, where you can also search for items sorted by key, so that you can, for example, iterate over them in order. Insert and search O(log n) .
Heap: priority queue (and heap sorting). Extract the minimum and insert O(log n) .
Structure Constraint Constraints
Red-black tree: general order: left child <parent <right child.
Heap: dominance: parent <children.
(note that you can replace a more general order than <)
Execution / memory loss
Red-black wood: pointers used to represent the structure of the tree, therefore the overhead of the element. Usually several nodes are used, distributed in free storage (for example, using new in C ++), the nodes point to other nodes. Balance is maintained to ensure a logarithmic search / insert.
Heap: the structure is implicit: the root is at position 0, children with a root at 1 and 2, etc., so there is no overhead for the element. Usually just stored in a single array.
source share