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
8
votes
2 answers

Red-black Tree Rotation: When I have y = x.right; x.right = y.left. Is it the same to write y.left.p = x as x.right.p = x

In CLRS, the authors introduce the rotation operation in red-black tree by following pseudocode: LEFT-ROTATE(T, x) y = x.right # Line 1 x.right = y.left # Line 2 if y.left ≠ T.nil # Line 3 y.left.p = x # Line 4 …
cindy50633
  • 144
  • 2
  • 11
8
votes
5 answers

Javascript: Need a decent red black tree implementation

Where can I find one ready for use? Or for that matter, a good collection of "standard" data structures, if you know of any?
Hamster
  • 2,962
  • 7
  • 27
  • 38
8
votes
1 answer

Red Black Trees: Kahrs version

I'm currently trying to understand the Red-Black tree implementation as given by Okasaki and delete methods by Kahrs (the untyped version). In the delete implementation a function app is used, which seems to "merging" the children of the node being…
user3169543
  • 1,499
  • 1
  • 10
  • 16
8
votes
2 answers

How tree map uses red black tree algorithm

I have read many articles on Red black tree where it take O(log n) time for operations .I am not very clear how it works and how actually tree map uses red black tree algorithm to balance the tree compared to binary search tree. Ref…
coder25
  • 2,363
  • 12
  • 57
  • 104
8
votes
1 answer

Java 8 hashmap implementation using TreeNode instead of linkedlist

According to this post: http://coding-geek.com/how-does-a-hashmap-work-in-java/ java 8 hashmaps use a treenode instead of a linkedlist (as in java 7) as elements of the array. TreeNodes have the special property of acting as a linkedlist if the…
Anik Rahman
  • 89
  • 1
  • 2
8
votes
2 answers

Does any std::set implementation not use a red-black tree?

Has anyone seen an implementation of the STL where stl::set is not implemented as a red-black tree? The reason I ask is that, in my experiments, B-trees outperform std::set (and other red-black tree implementations) by a factor of 2 to 4 depending…
Pat Morin
  • 1,688
  • 1
  • 12
  • 12
8
votes
3 answers

Finding C++ interval tree algorithm implementation

I'm trying to find an efficient C++ interval tree implementation (mostly likely based on red black trees) without a viral or restrictive license. Any pointers to a clean lightweight standalone implementation? For the use case I have in mind, the set…
Christophe Lambert
8
votes
2 answers

Red Black Tree inserting: why make nodes red when inserted?

In Red Black Tree Properties doesn't contain any rule for insert node color, how ever always we are inserting red color nodes. If we insert black color node does it violate any Properties( any rule out of 4)?
8
votes
2 answers

Order of insertion for worst case black height of a red black tree

Lets say we are dealing with the keys 1-15. To get the worst case performance of a regular BST, you would insert the keys in ascending or descending order as follows: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 Then the BST would essentially…
Jordan
  • 4,928
  • 4
  • 26
  • 39
7
votes
3 answers

Time complexity of TreeMap<> operations: get() and subMap()

Based on this post, Time complexity of TreeMap operations- subMap, headMap, tailMap subMap() itself is O(1), and O(n) comes from iterating the sub map. So, why use get(key) then? We can use subMap(key, true, key, true) instead, which is O(1) and…
Leonard
  • 183
  • 1
  • 11
7
votes
4 answers

Building Red-Black Tree from sorted array in linear time

i know how to build it with n insertions ( each with O(log(n)) efficiency ) (n*log(n)) overall ,i also know that the equivalent structure of 2-3-4 tree can also be build with linear time from sorted array. can anyone please provide a simple…
Michael G
  • 1,190
  • 8
  • 12
7
votes
5 answers

CompareTo may return 0, alternative to TreeSet/TreeMap

I need a sorted set of objects and am currently using the TreeSet. My problem is that the compareTo of the objects will often return 0, meaning the order of those two objects is to be left unchanged. TreeMap (used by TreeSet by default) will then…
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
7
votes
2 answers

Is Red-Black tree balanced

I am studying red-black trees and I am reading the Cormen's "Introduction to Algorithms" book. Now I am trying to create red-black tree with numbers 1-10 by using the pseudo-code described in the book - RB-INSERT-FIXUP(T, z). Here is the…
Ashot Khachatryan
  • 2,156
  • 2
  • 14
  • 30
7
votes
2 answers

What's an idiomatic way to implement a red-black tree in Go?

I'm new to Go and have implemented a binary search tree. The tree can store any value (specifically, anything that implements interface{}). I'd like to build upon this implementation to create a self-balancing red-black tree. In an object-oriented…
Brian Gesiak
  • 6,648
  • 4
  • 35
  • 50
7
votes
2 answers

Why does the Red-Black Tree in the Clojure Cookbook miss the recoloring case

After playing around with the Red-Black Tree implementation example in the Clojure Cookbook I noticed that the balancing function does not contain the recoloring case. (Case 1 in most of the Red-Black Tree literature, or also known as the case…
Kungi
  • 1,477
  • 1
  • 18
  • 34