c++ - why are priority queues implemented as binary heap? -


why people stress heaps used implement priority queues because time complexity of peeking @ max/min value o(1) .
can't implented on bst , using pointer point @ right most/left node.

given fact propose priority queue based on bst, try explain why heap better bst.

a heap complete tree; balanced tree. height log_2(n+1).

a bst approach worthwhile if 1 balanced. best-known technique maintaining bst balanced avl tree. kind of tree has height bound of 1.44 log_2(n+2) - 0.33.

for consultation of minimum have cost of o(log(n)) bst versus o(1) heap. operation heap better.

for insertion , deletion costs asymptotically equivalent. bst tends more expensive because height tends higher balanced tree. in addition, avl tree consumes more constant time heap. in avl (and in other balancing approaches, red-black tree, treaps, splays, etc) perform rotations, while heap perform swaps, cheaper rotations.

deletion on bst complicated , expensive operation , take o(log(n)) rotations. heap o(log(n)) swaps, recall cheaper rotations.

eventually, in case of insertion, perform o(log(n)) swaps heap , @ 2 rotations avl. insertion avl need perform unsuccessful search, while heap can directly insert new key before starting swap. think insertion bst beat heap. however, take in account use priority queue consultations , deletions; so, if case, surely recover time lose when made insertions.

in addition, heap far easier implement bst if use array stores level traversal of complete tree. in case, not need pointers


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -