static void TestRBBSTree() { RBBSTNode tree = new RBBSTNode(50); var random = new Random(); Stopwatch watch = new Stopwatch(); watch.Restart(); var find = new RBBSTNode(random.Next(0, 10000)); tree = RBBSTNode.Add(tree, find); for (int i = 0; i < 10000; i++) { tree = RBBSTNode.Add(tree, new RBBSTNode(random.Next(0, 10000))); } Console.WriteLine("RBBST: " + watch.ElapsedMilliseconds); //for (int i = 0; i < 10; i++) //{ // tree = tree.Delete(new RBBSTNode(random.Next(0, 100))); //} //tree.MidOrderIterate(); Console.WriteLine("Find: " + find.Key + " in RBBST"); watch.Restart(); var result = tree.Get(find); Console.WriteLine("Find: " + result.Key + " in: " + watch.ElapsedTicks); }
public static RBBSTNode RightRotate(RBBSTNode source) { var other = source.Left; source.Left = other.Right; other.Right = source; other.IsRed = source.IsRed; source.IsRed = true; other.Size = source.Size; source.Size = 1 + SizeOf(source.Left) + SizeOf(source.Right); return(other); }
public static RBBSTNode Add(RBBSTNode source, RBBSTNode add) { if (source == null) { source = add; return(source); } if (add > source) { source.Right = Add(source.Right, add); } else if (add < source) { source.Left = Add(source.Left, add); } else { //to implement change contents. } if (GetIsRed(source.Right) && !GetIsRed(source.Left)) { source = LeftRotate(source); } if (GetIsRed(source.Left) && GetIsRed(source.Left.Left)) { source = RightRotate(source); } if (GetIsRed(source.Left) && GetIsRed(source.Right)) { FlipColor(source); } source.Size = 1 + SizeOf(source.Left) + SizeOf(source.Right); return(source); }
public RBBSTNode Delete(RBBSTNode p_node) { return((RBBSTNode)base.Delete(p_node)); }
public static void FlipColor(RBBSTNode source) { source.Left.IsRed = false; source.Right.IsRed = false; source.IsRed = true; }
public RBBSTNode Get(RBBSTNode p_node) { return((RBBSTNode)base.Get(p_node)); }
public static bool GetIsRed(RBBSTNode p_node) { return(p_node != null && p_node.IsRed); }