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
7
votes
1 answer

Trie vs red-black tree: which is better in space and time?

Tries and red-black trees are very efficient for storing strings. Which has better time complexity? How about space complexity?
7
votes
2 answers

Deletion in Left Leaning Red Black Trees

I am learning about Left Leaning Red Black Trees. In the deletion algorithm peresented in the paper, if the key matches for a node and the right subtree is NULL for that node, then that node is deleted. But there may be a left subtree as well which…
7
votes
1 answer

why does qmap uses skiplist instead ob rb-tree?

I wounder why does QMap realised over skiplist data-structure and not rb-tree? There is very interesting SO thread about concurrency data-structs and skip-list benefits over rb-tree, pros and cons. It is indeed VERY interesing dialog with helpfull…
sohel
  • 377
  • 3
  • 13
6
votes
1 answer

How are red-black trees isomorphic to 2-3-4 trees?

I have a basic understanding of both red black trees and 2-3-4 trees and how they maintain the height balance to make sure that the worst case operations are O(n logn). But, I am not able to understand this text from Wikipedia 2-3-4 trees are an…
Lazer
  • 90,700
  • 113
  • 281
  • 364
6
votes
2 answers

Why Red Black Trees always having nil nodes as their leaf nodes and what is it implies?

I don't know why we need the NIL node as a leaf node in Red-Black Trees. Can anyone give an explanation about its purpose?
user9888628
6
votes
3 answers

Deleting a whole subtree of a red-black tree would keep its properties?

I'm currently implementing a red-black tree data structure to perform some optimizations for an application. In my application, at a given point I need to remove all elements less than or equal to a given value (you can assume that the elements are…
Phil
  • 3,375
  • 3
  • 30
  • 46
6
votes
1 answer

Trouble understanding libstdc++'s Red-black tree iterators

I'm trying to implement iterators for a Ternary Search Tree, and because TSTs are very similar to BSTs, I thought I'd look at libstdc++'s implementation for said iterators. The idea is to iterate through all tree nodes without modifying the tree or…
Ron
  • 1,989
  • 2
  • 17
  • 33
6
votes
1 answer

How to deal with duplicates in red-black trees?

So I've been(so far unsuccessfully) trying to make my red-black tree implementation work consistently with duplicates, but it seems to always be missing that small something, so here I am. I tried make the tree lean to one side, but It didn't seem…
jonas rudzionis
  • 115
  • 2
  • 8
6
votes
1 answer

std::map Known-Position Erase Amortized Complexity And Number of Red-Black Tree Recolorings

The complexity of std::map::erase(iterator) is amortized O(1) (see here, for example). While the standard library does not dictate implementations, this de-facto means that the number of rebalancing operations needed for a red-black tree is…
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
6
votes
3 answers

Is there something like mulitiSet in JavaScript?

I know that JavaScript now has sets, but I wonder if there is something to realize the function of multiSet, or if there is some framework that has the functions of multiset which I really need a lot. Or I have to code it by myself to do the…
hanzichi
  • 609
  • 2
  • 12
  • 22
6
votes
5 answers

Does inserting/erasing an element from a std::map modify the iteration sequence?

Say I have the following code: typedef std::map< int, std::string >::iterator Iterator; Iterator iter = myMap.begin(); while (iter != myMap.end()) { Iterator current = iter; ++iter; maybeDeleteElement( current ) // may call…
Dominic Gurto
  • 4,025
  • 2
  • 18
  • 16
6
votes
1 answer

Where can I find a simple red-black tree implementation?

It's not easy to find red-black tree implementations on the net, especially for learning. Where can I find a simple red-black tree implementation (C# preferred)?
user541686
  • 205,094
  • 128
  • 528
  • 886
5
votes
3 answers

Strange results when plotting (Cormen) Red-black tree insert

I implemented in Red-Black trees in Python according to the pseudo code in Cormen's Introduction to Algorithms. I wanted to see in my own eyes that my insert is really O(logn) so I plotted the time it takes to insert n=1, 10, 20, ..., 5000 nodes…
Zack
  • 2,021
  • 4
  • 18
  • 19
5
votes
1 answer

Which is easier to implement: 2-3-4 Tree or Red-Black Tree?

The textbook I'm learning from (Lafore) presents Red-Black Trees first, and does not include any pseudo-code, although the associated algorithms as presented seems fairly complex, with many unique cases. Next he presents 2-3-4 Trees which seem to me…
The111
  • 5,757
  • 4
  • 39
  • 55
5
votes
2 answers

Red Black Tree deletion algorithm

From "Introduction to Algorithms 2nd edition" I got this deletion algorithm: /* RB-DELETE(T, z) 1 if left[z] = nil[T] or right[z] = nil[T] 2 then y ← z 3 else y ← TREE-SUCCESSOR(z) 4 if left[y] ≠ nil[T] 5 then x ←…
smallB
  • 16,662
  • 33
  • 107
  • 151