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

Converting a 2-3-4 tree into a red black tree

I'm trying to convert a 2-3-4 Tree into a Red-Black tree in java, but am having trouble figuring it out. I've written these two basic classes as follows, to make the problem straightforward, but can't figure out where to go from here. public class…
user3745602
  • 315
  • 1
  • 3
  • 12
11
votes
3 answers

Is kd-tree always balanced?

I have used kd-tree algoritham and make tree. But i found that tree is not balanced so my question is if we used kd-tree algoritham then that tree is always balanced if not then how can we make it balance ?. We can use another algoritham likes…
Logicbomb
  • 531
  • 1
  • 6
  • 20
11
votes
8 answers

Largest and smallest number of internal nodes in red-black tree?

The smallest number of internal nodes in a red-black tree with black height of k is 2k-1 which is one in the following image: The largest number of internal nodes with black height of k is 22k-1 which, if the black height is 2, should be 24 - 1 =…
HMdeveloper
  • 2,772
  • 9
  • 45
  • 74
10
votes
3 answers

Why is avl tree faster for searching than red black tree?

I have read it in a couple of places that avl tree search faster, but not able to understand. As I understand : max height of red-black tree = 2*log(N+1) height of AVL tree = 1.44*logo(N+1) Is it because AVL is shorter?
paseena
  • 4,207
  • 6
  • 32
  • 51
10
votes
4 answers

Computational complexity of TreeSet operations in Java?

I am trying to clear up some things regarding complexity in some of the operations of TreeSet. On the javadoc it says: "This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains)." So far so…
Andreas K.
  • 165
  • 1
  • 2
  • 7
10
votes
2 answers

Heap or Red-Black Tree?

I am willing to use a data structure as an overflow buffer of constant space. I want to have efficient insert but most importantly efficient removal of the min element. I was thinking of using a heap since I have O(log(n)) find_min() and log(n)…
nikosdi
  • 2,138
  • 5
  • 26
  • 35
10
votes
2 answers

Red Black Tree Top-Down Deletion Algorithm

I am implementing a Red Black Tree with insert, search and delete functions in O (log n) time. Insert and search are working fine. However I am stuck on delete. I found this ppt slide on the internet which shows the algorithm of RBT deletion:…
Bernice
  • 2,552
  • 11
  • 42
  • 74
9
votes
1 answer

Why Red Black trees preferred over AVL trees for memory management in Linux?

The vm_area_struct structure used to link various sections of a memory mapped executable file is stored as a red black tree. Now, as far as I know and the post here mentions too Difference between red-black trees and AVL trees AVL trees performs…
rango
  • 311
  • 1
  • 13
9
votes
3 answers

Guidelines to an Iterator Class

I have a Red Black tree implemented in c++. It supports the functionality of a STL map. Tree nodes contain keys and the values mapped. I want to write an iterator class for this, but I'm stuck with how to do it. Should I make it an inner class of…
Izza
  • 2,389
  • 8
  • 38
  • 60
9
votes
1 answer

Performance: SortedDictionary vs SortedSet

Should I maintain 1.) a SortedDictionary(double,struct) 2.) or just a plain Dictionary(double,struct) plus a SortedSet(double)? I just want fast insertions. I dont care about retrievals, as I'm not gonna do much lookups. I need sorted nature…
9
votes
4 answers

Implementation of Red-Black Tree in C#

I'm looking for an implementation of a Red-Black Tree in C#, with the following features: Search, Insert and Delete in O(log n). Members type should be generic. Support in Comparer(T), for sorting T by different fields in it. Searching in the tree…
Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288
9
votes
2 answers

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

We know that heaps and red-black tree both have these properties: worst-case cost for searching is lgN; 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…
Check King
  • 111
  • 1
  • 1
  • 3
9
votes
5 answers

How to save the memory when storing color information in Red-Black Trees?

I've bumped into this question at one of Coursera algorithms course and realized that I have no idea how to do that. But still, I have some thoughts about it. The first thing that comes into my mind was using optimized bit set (like Java's BitSet)…
9
votes
2 answers

Working of std::map::erase(iterator position)?

I read on cplusplus.com that the operation to delete an element in a std::map by passing the iterator as argument is constant time. If I am not wrong(and please correct me if I am) the iterators basically are pointers to the elements in the map with…
Rampal Chaudhary
  • 707
  • 1
  • 8
  • 18
8
votes
2 answers

Is a resultant red-black tree after insertion unique?

Suppose I have a binary search tree which, initially, satisfies all of the red-black conditions and contains one node for every integer s in some set S. Next, I want to a new node; say a (which is not in S). Is the result of this addition, after…
Ryan
  • 925
  • 2
  • 6
  • 25
1 2
3
39 40