Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between heap and red-black tree?

We know that heaps and red-black tree both have these properties:

  1. worst-case cost for searching is lgN;
  2. worst-case cost for insertion is lgN.

So, since the implementation and operation of red-black trees is difficult, why don't we just use heaps instead of red-black trees? I am confused.

like image 695
Check King Avatar asked May 14 '13 09:05

Check King


People also ask

What is the difference between heap and tree?

The Heap differs from a Binary Search Tree. The BST is an ordered data structure, however, the Heap is not. In computer memory, the heap is usually represented as an array of numbers. The heap can be either Min-Heap or Max-Heap.

What is difference between RB tree and AVL tree?

Red Black Trees has fewer lookups because they are not strictly balanced. AVL trees provide faster lookups than Red-Black Trees because they are more strictly balanced. In this, the color of the node is either Red or Black. In this, there is no color of the node.

What is the difference between red-black tree and binary search tree?

A red-black tree is a binary search tree with the following properties: Every node is colored with either red or black. All leaf (nil) nodes are colored with black; if a node's child is missing then we will assume that it has a nil child in that place and this nil child is always colored black.

What is common between AVL and red-black trees?

The complexity of tree operation in the red-black tree data structure is the same as the AVL tree. The red-black tree is a self-balancing binary search tree with the same complexity as the AVL tree.


1 Answers

You can't find an arbitrary element in a heap in O(log n). It takes O(n) to do this. You can find the first element (the smallest, say) in a heap in O(1) and extract it in O(log n). Red-black trees and heaps have quite different uses, internal orderings, and implementations: see below for more details.

Typical use

Red-black tree: storing dictionary where as well as lookup you want elements sorted by key, so that you can for example iterate through them in order. Insert and lookup are O(log n).

Heap: priority queue (and heap sort). Extraction of minimum and insertion are O(log n).

Consistency constraints imposed by structure

Red-black tree: total ordering: left child < parent < right child.

Heap: dominance: parent < children only.

(note that you can substitute a more general ordering than <)

Implementation / Memory overhead

Red-black tree: pointers used to represent structure of tree, so overhead per element. Typically uses a number of nodes allocated on free store (e.g. using new in C++), nodes point to other nodes. Kept balanced to ensure logarithmic lookup / insertion.

Heap: structure is implicit: root is at position 0, children of root at 1 and 2, etc, so no overhead per element. Typically just stored in a single array.

like image 138
TooTone Avatar answered Nov 14 '22 05:11

TooTone