red-black-tree

Red-black tree access by ordinal index

人盡茶涼 提交于 2020-01-23 11:52:53
问题 I have a red-black tree (binary tree, all the leaves are within 2 levels). I can navigate through nodes: to left, right or parent. I know the entire count of nodes. I have to find the N-th smallest element in a tree. Is there any way to do this faster than in O(n)? Any ideas of optimizing access by index? 回答1: In each node X you should store, how many nodes are in subtree with X as a root. count(LEAF) = 1 count(NODE) = count(NODE->LEFT) + count(NODE->RIGHT) + 1 During each insert/delete you

Is Red-Black tree balanced

99封情书 提交于 2020-01-13 05:56:50
问题 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 screenshot Everything was fine until I inserted number "6" into the tree. According to pseudo-code I get the following result As you can see all red-black tree requirements met, but I am confused because I know that red-black tree should be balanced on each

Computational complexity of TreeSet operations in Java?

≯℡__Kan透↙ 提交于 2019-12-30 06:15:20
问题 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 good. My question is what happens on addAll(), removeAll() etc. Here the javadoc for Set says: "If the specified collection is also a set, the addAll operation effectively modifies this set so that its value is the union of the two sets." Is it just

Red-black tree - How to find the node's parent?

≡放荡痞女 提交于 2019-12-22 11:23:44
问题 In red-black tree, when rotate, you need to know who is the parent of particular node. However, the node only has reference to either right or left child. I was thinking to give a node instance variable "parent" but just for this reason I don't think it is worth doing so and also it would be too complicated to change parent reference per rotation. public class Node { private left; private right; private isRed; private parent; //I don't think this is good idea } So, my solution is to write

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

坚强是说给别人听的谎言 提交于 2019-12-22 06:45:13
问题 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-2B trees outperform stl::set (and other red-black tree implementations) by a factor of 2 to 4 depending on the value of B. I'm curious if there is a compelling reason to use red-black trees when there appear to be faster data structures available. 回答1: Some folks over at Google actually built a B-tree based implementation of the C++ standard

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

耗尽温柔 提交于 2019-12-20 08:43:48
问题 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 SortedSet inserting 40000 sequential integral values (starting from 0 to 39999), I am astonished to see that there is huge perf difference as follows: RBTree took 9.27208 sec to insert 40000 values SortedSet took 0.0253097 sec to insert 40000 values

Red-Black Trees

拜拜、爱过 提交于 2019-12-17 17:24:44
问题 I've seen binary trees and binary searching mentioned in several books I've read lately, but as I'm still at the beginning of my studies in Computer Science, I've yet to take a class that's really dealt with algorithms and data structures in a serious way. I've checked around the typical sources (Wikipedia, Google) and most descriptions of the usefulness and implementation of (in particular) Red-Black trees have come off as dense and difficult to understand. I'm sure for someone with the

How to fix remove in RedBlackTree implementation?

烈酒焚心 提交于 2019-12-17 07:50:52
问题 Here is the implementation of RedBlackTree I am using (from Mark Allen Weiss, Data Structures public class RedBlackTree<AnyKey extends Comparable<? super AnyKey>, AnyValue extends Comparable<? super AnyValue>> implements MyTreeMap<AnyKey, AnyValue>{ private static final int BLACK = 1; private static final int RED = 0; // The psuedo(bogus) root, has a key value of negative infinity and a right link to the real root. private RedBlackNode<AnyKey, AnyValue> header; // Used in place of a null link

Red-Black Tree Height using Recursion

喜欢而已 提交于 2019-12-13 05:09:11
问题 I have these following methods to get the height of a red black tree and this works (I send the root). Now my question is, how is this working? I have drawn a tree and have tried following this step by step for each recursion call but I can't pull it off. I know the general idea of what the code is doing, which is going through all the leaves and comparing them but can anyone give a clear explanation on this? int RedBlackTree::heightHelper(Node * n) const{ if ( n == NULL ){ return -1; } else{

Red Black Trees Rebalancing

妖精的绣舞 提交于 2019-12-12 01:43:42
问题 I want to insert 7 items in my tree -3, -2, -1, 0, 1, 2 and 3. I get a well balanced tree of height 3 without doing rotations when I insert by this order: 0, -2, 2, -1, 1, -3, 3. But when I insert all items in ascending order, the right part of the root node does rebalancing, but the left part of the the root node doesn't. All rebalancing algorithms I have seen, do rebalancing from the inserted node up to the root node, and then they stop. Shouldn't they continue to the opposite part of the