Questions tagged [red-black-tree]

A red-black tree is a type of self-balancing binary search tree, a data structure used in computing science, typically used to implement associative arrays.

From Wikipedia, Red–black tree:

A red-black tree is a special type of binary tree, used in computer science to organize pieces of comparable data, such as text fragments or numbers.

The leaf nodes of red-black trees do not contain data. These leaves need not be explicit in computer memory — a null child pointer can encode the fact that this child is a leaf — but it simplifies some algorithms for operating on red-black trees if the leaves really are explicit nodes. To save memory, sometimes a single sentinel node performs the role of all leaf nodes; all references from internal nodes to leaf nodes then point to the sentinel node.

Red-black trees, like all binary search trees, allow efficient in-order traversal in the fashion, Left-Root-Right, of their elements. The search-time results from the traversal from root to leaf, and therefore a balanced tree, having the least possible tree height, results in O(log n) search time.

588 questions
18
votes
1 answer

Red black tree pseudocode redundancy

In introduction to Algorithms Third Edition they have a pseudocode implementation of red-black tree deletion. Here it is... RB-DELETE(T, z) y = z y-original-color = y.color if z.left == T.nil x = z.right RB-TRANSPLANT(T,…
confused
  • 213
  • 2
  • 6
18
votes
4 answers

What is the reason behind this huge Performance difference in .Net 4

I was just doing some research on RedBlack Tree. I knew that SortedSet class in .Net 4.0 uses RedBlack tree. So I took that part out as is using Reflector and created a RedBlackTree class. Now I am running some perf test on this RedBlackTree and…
Anindya Chatterjee
  • 5,824
  • 13
  • 58
  • 82
18
votes
1 answer

Scala range / interval map structure

I have almost the same question as mentioned in Data structures that can map a range of keys to a value, but for Scala. That is, I'd like to have a mutable system of non-overlapping 1D ranges [a[i], b[i]) that would map to some sort of value v[i]. A…
GreyCat
  • 16,622
  • 18
  • 74
  • 112
16
votes
1 answer

Interval tree algorithm that supports merging of intervals with no overlap

I'm looking for an interval tree algorithm similar to the red-black interval tree in CLR but that supports merging of intervals by default so that there are never any overlapping intervals. In other words if you had a tree containing two intervals…
Dave Griffiths
  • 1,962
  • 19
  • 22
16
votes
3 answers

Using STL's Internal Implementation of Red-Black Tree

I understand that my STL (that comes with g++ 4.x.x) uses red-black trees to implement containers such as the map. Is it possible to use the STL's internal red-black tree directly. If so, how? If not, why not - why does STL not expose the red-black…
Prasoon Tiwari
  • 840
  • 2
  • 12
  • 23
15
votes
7 answers

Is a tree with all black nodes a red black tree?

It seems the definition on wiki is not precise: http://en.wikipedia.org/wiki/Red-black_tree#Properties Is a tree with all black nodes a red black tree? UPDATE With the definition of rbtree not so strict,how do we decide whether to print the children…
cpuer
  • 7,413
  • 14
  • 35
  • 39
15
votes
2 answers

In red-black trees is top-down deletion faster and more space efficient than bottom-up deletion?

Per this page http://www.eternallyconfuzzled.com/tuts/datastructures/jsw_tut_rbtree.aspx "Top-down deletion" is an implementation of a red-black tree node removal that pro-actively balances a tree by pushing a red node down through the tree so that…
Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
15
votes
3 answers

Why red-black tree based implementation for java TreeMap?

The third paragraph of wikipedia's article on AVL trees says: "Because AVL trees are more rigidly balanced, they are faster than red-black trees for lookup-intensive applications." So, shouldn't TreeMap be implemented using AVL trees instead of…
Nikunj Banka
  • 11,117
  • 16
  • 74
  • 112
14
votes
2 answers

Why null key is not allowed in TreeMap?

I am trying to understand the concept behind Java Collection framework and came along to this question - Why null key is not allowed in TreeMap? Its giving NullPointerException if we try to add null key in TreeMap. Tried to google the Internal…
Amol Patil
  • 985
  • 2
  • 11
  • 43
14
votes
6 answers

Using red black trees for sorting

The worst-case running time of insertion on a red-black tree is O(lg n) and if I perform a in-order walk on the tree, I essentially visit each node, so the total worst-case runtime to print the sorted collection would be O(n lg n) I am curious, why…
Vaibhav Bajpai
  • 16,374
  • 13
  • 54
  • 85
14
votes
2 answers

Problems with Promote() using the red-black tree implementation from The Tomes of Delphi

I am using the Red-Black tree implementation written by Julian Bucknall in his well-known book, The Tomes Of Delphi. Source code can be downloaded here, and I am using the code as-is in Delphi 2010, with modifications to TdBasics.pas to let it…
David
  • 13,360
  • 7
  • 66
  • 130
13
votes
3 answers

Reason why CFS scheduler using red black tree?

CFS scheduler picks next process based on minimum virtual time and to get this value efficiently its using Red-Black tree(rbtree), using rbtree we will get minimum O(h) here h is height of rbtree. But, using min-heap we can get min virtual time…
noman pouigt
  • 906
  • 11
  • 25
13
votes
6 answers

Can every valid red-black tree exist?

Suppose you have a red-black tree that is a valid binary search tree and does not violate any of those rules: A node is either red or black. The root is black. All leaves (NIL) are black. Both children of every red node are black. Every simple path…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
12
votes
1 answer

Dictionary using Red-Black tree - deletion error

I'm trying to implement a Dictionary using a Red-Black tree. I've tested the insert method and it seems to work good, the RBtree seems to keep the correct shape and colors. The method that performs the binary tree node deletion seems to be correct,…
tommaso capelli
  • 920
  • 2
  • 8
  • 20
12
votes
2 answers

What is the standard binary search tree structure to use in Scala?

What is the standard balanced binary search tree implementation one should use in Scala 2.10.x? I am looking around and it seems that AVLTree was removed and RedBlack is deprecated with a message (Since version 2.10.0) use TreeMap or TreeSet…
jbx
  • 21,365
  • 18
  • 90
  • 144
1
2
3
39 40