public void DeleteTest_KeyNotFound() { List <int> elements = new List <int>(); var t = new TwoThreeTree <int, int>(); try { this.InsertNewNodeAndVerify(t, elements, 10); this.InsertNewNodeAndVerify(t, elements, 20); this.InsertNewNodeAndVerify(t, elements, 2); this.InsertNewNodeAndVerify(t, elements, 5); this.InsertNewNodeAndVerify(t, elements, 15); this.InsertNewNodeAndVerify(t, elements, 30); this.InsertNewNodeAndVerify(t, elements, 1); this.InsertNewNodeAndVerify(t, elements, 3); this.InsertNewNodeAndVerify(t, elements, 8); this.InsertNewNodeAndVerify(t, elements, 9); this.InsertNewNodeAndVerify(t, elements, 11); this.InsertNewNodeAndVerify(t, elements, 16); this.InsertNewNodeAndVerify(t, elements, 25); this.InsertNewNodeAndVerify(t, elements, 35); t.Delete(7); } catch (Exception e) { Assert.IsTrue(e is KeyNotFoundException); } finally { this.Validate(t, elements); } }
//O(N^3) OR O(L*D*D) WHERE D IS Height of the tree AND L IS Count(Leaves) private void AddFromList(ref ArrayList <String> V, ref TwoThreeTree <String> TREE) { StringBuilder sb = new StringBuilder(); Int32 l = 0; while (l < V.Count)//O(H*H) where H is the count of the V. { for (Int32 i = 0; i < V.Count; i++) { if (i + l < V.Count) { for (Int32 j = i, c = 0; j < V.Count && c <= l; j++, c++) { sb.Append(V[j]); } } else { continue; } //if(sb.Length != 0){ TREE.Add(sb.ToString()); //Console.WriteLine("{0} : {1}",sb.ToString(),TREE.Add(sb.ToString())); sb.Length = 0; //} } l++; } }
public void TestInsertSingle() { var tree = new TwoThreeTree <string>(); tree.Insert("13"); Assert.AreEqual("13 " + Environment.NewLine, tree.ToString()); }
public void TestInsertMany() { var tree = new TwoThreeTree <string>(); tree.Insert("A"); tree.Insert("B"); tree.Insert("C"); Assert.AreEqual("B " + Environment.NewLine + "A " + Environment.NewLine + "C " + Environment.NewLine, tree.ToString()); }
public void DeleteTest_R2L22_RemoveRoot() { List <int> elements = new List <int>(); var t = new TwoThreeTree <int, int>(); this.InsertNewNodeAndVerify(t, elements, 1); this.InsertNewNodeAndVerify(t, elements, 0); this.InsertNewNodeAndVerify(t, elements, 2); Assert.AreEqual(1, t.Delete(1)); elements.Remove(1); this.Validate(t, elements); }
private static long Tree23Get <T>(TwoThreeTree <Type, T> tree, Type key, int times) { T ignored = default(T); var treeWatch = Stopwatch.StartNew(); for (int i = 0; i < times; i++) { ignored = tree.GetValueOrDefault(key); } treeWatch.Stop(); GC.KeepAlive(ignored); GC.Collect(); return(treeWatch.ElapsedMilliseconds); }
public void InsertTest_Descending() { List <int> elements = new List <int>(); var t = new TwoThreeTree <int, int>(); this.InsertNewNodeAndVerify(t, elements, 9); this.InsertNewNodeAndVerify(t, elements, 8); this.InsertNewNodeAndVerify(t, elements, 7); this.InsertNewNodeAndVerify(t, elements, 6); this.InsertNewNodeAndVerify(t, elements, 5); this.InsertNewNodeAndVerify(t, elements, 4); this.InsertNewNodeAndVerify(t, elements, 3); this.InsertNewNodeAndVerify(t, elements, 2); this.InsertNewNodeAndVerify(t, elements, 1); this.InsertNewNodeAndVerify(t, elements, 0); }
private static void PreOrderTraverse <TKey, TValue>(TwoThreeTree <TKey, TValue> .Node node, Action <TKey, TValue> action) where TKey : IComparable <TKey> { if (node == null) { return; } PreOrderTraverse(node.Children[0], action); action(node.Keys[0], node.Values[0]); PreOrderTraverse(node.Children[1], action); if (node.KeyCount == 2) { action(node.Keys[1], node.Values[1]); PreOrderTraverse(node.Children[2], action); } }
private void Validate(TwoThreeTree <int, int> t, List <int> elements) { List <int> keys = new List <int>(); t.PreOrderTraverse((key, value) => { keys.Add(key); }); Assert.AreEqual(elements.Count, keys.Count); elements.Sort(); for (int i = 0; i < keys.Count; ++i) { Assert.AreEqual(elements[i], keys[i]); } }
private static long Tree23Add <V>(ref TwoThreeTree <Type, V> tree, Type[] keys, Type key, V value) { var ignored = default(V); var treeTime = Stopwatch.StartNew(); for (var i = 0; i < keys.Length; i++) { var k = keys[i]; Interlocked.Exchange(ref tree, tree.AddOrUpdate(k, ignored)); } Interlocked.Exchange(ref tree, tree.AddOrUpdate(key, value)); treeTime.Stop(); GC.KeepAlive(ignored); GC.Collect(); return(treeTime.ElapsedMilliseconds); }
public void Initialize() { this.tree = new TwoThreeTree <int, int>(); this.tree.Upsert(4, 4); this.tree.Upsert(1, 1); this.tree.Upsert(7, 7); this.tree.Upsert(9, 9); this.tree.Upsert(0, 0); this.tree.Upsert(2, 2); this.tree.Upsert(3, 3); this.tree.Upsert(5, 5); this.tree.Upsert(6, 6); this.tree.Upsert(8, 8); this.tree.Upsert(10, 10); this.treeKeys = new List <int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; }
public void TestInsert13Elements() { var tree = new TwoThreeTree <string>(); String[] arr = { "F", "C", "G", "A", "B", "D", "E", "K", "I", "G", "H", "J", "K" }; for (int i = 0; i < 13; i++) { tree.Insert(arr[i]); } Assert.AreEqual("D G" + Environment.NewLine + "B " + Environment.NewLine + "A " + Environment.NewLine + "C " + Environment.NewLine + "F " + Environment.NewLine + "E " + Environment.NewLine + "G " + Environment.NewLine + "I K" + Environment.NewLine + "H " + Environment.NewLine + "J " + Environment.NewLine + "K " + Environment.NewLine, tree.ToString()); }
public void DeleteTest_R3() { List <int> elements = new List <int>(); var t = new TwoThreeTree <int, int>(); this.InsertNewNodeAndVerify(t, elements, 10); this.InsertNewNodeAndVerify(t, elements, 20); this.InsertNewNodeAndVerify(t, elements, 2); this.InsertNewNodeAndVerify(t, elements, 5); this.InsertNewNodeAndVerify(t, elements, 15); this.InsertNewNodeAndVerify(t, elements, 30); this.InsertNewNodeAndVerify(t, elements, 1); this.InsertNewNodeAndVerify(t, elements, 3); this.InsertNewNodeAndVerify(t, elements, 8); this.InsertNewNodeAndVerify(t, elements, 9); this.InsertNewNodeAndVerify(t, elements, 11); this.InsertNewNodeAndVerify(t, elements, 16); this.InsertNewNodeAndVerify(t, elements, 25); this.InsertNewNodeAndVerify(t, elements, 35); Assert.AreEqual(10, t.Delete(10)); elements.Remove(10); this.Validate(t, elements); }
public void Execute() { SortedLinkedList <Int32> SET = new SortedLinkedList <Int32>(new Int32[10] { 4, 2, 7, 3, 8, 11, 2, 1, 13, 15 }); Console.WriteLine("\n ----SETS---- \n"); Console.WriteLine("A = " + SET.ToString()); SortedLinkedList <Int32> B = new SortedLinkedList <Int32>(new Int32[7] { 4, 2, 6, 88, 77, 44, 11 }); Console.WriteLine("B = " + B.ToString()); BinarySearchTree <Int32> F1 = new BinarySearchTree <Int32>(); BinarySearchTree <Int32> F2 = new BinarySearchTree <Int32>(); TwoThreeTree <Int32> BT = new TwoThreeTree <Int32>(); BT.AddRange(new Int32[10] { 4, 2, 7, 3, 8, 11, 2, 1, 13, 15 }); //NON-RECURSIVE!! Console.WriteLine("Count(2-3T) = " + BT.GetCount()); Console.WriteLine("2-3 TREE: " + BT); Console.WriteLine("FIND({0}): {1}", 4, BT.Contains(4)); Console.WriteLine("Height(TREE): {0}", BT.Height); BT.Delete(11); Console.WriteLine("2-3 TREE: " + BT); Console.WriteLine("Count(TREE) {0}", BT.Count); Console.WriteLine("Height(TREE) {0}", BT.Height); Console.WriteLine("Min(TREE) {0}", BT.Min()); Console.WriteLine("Max(TREE) {0}", BT.Max()); BT.DeleteMin();//Remove 1. Console.WriteLine(BT); Console.WriteLine(""); //2,4,7 (4,7) // F1.AddRange(new Int32[10] { 4, 2, 7, 3, 8, 11, 2, 1, 13, 15 }); //4 27 F2.AddRange(new Int32[7] { 4, 2, 6, 88, 77, 44, 11 }); //F2: 2->4<-6<-88 11->44->77->88 Console.WriteLine("F1 = " + F1.ToString()); Console.WriteLine("F2 = " + F2.ToString()); SortedLinkedList <Int32> FFF = F1.ToSortedList(); Console.WriteLine("F1(S) = " + FFF.ToString()); BinarySearchTree <Int32> F3 = F1.Intersection(F2); Console.WriteLine("F1 AND F2 = " + F3.ToString()); Console.WriteLine(""); Console.WriteLine("F1 OR F2 = " + F1.Union(F2).ToString()); Console.WriteLine(""); Console.WriteLine("F1\\F2 = " + F1.Difference(F2).ToString()); Console.WriteLine("Min(F1), Max(F1) :: {0}, {1}", F1.Min().ToString(), F1.Max().ToString()); Console.WriteLine(""); while (F1.GetCount() > 0) { Console.Write(F1.DeleteMin().ToString() + " "); } Console.WriteLine(""); SortedLinkedList <Int32> C; C = SET.Intersection(B); Console.WriteLine("A AND B = " + C.ToString()); Console.WriteLine("Min(C) = " + C.Min().ToString()); Console.WriteLine("Max(C) = " + C.Max().ToString()); Console.WriteLine("Count(C) = " + C.Count); Console.WriteLine("A OR B = " + SET.Union(B).ToString()); Console.WriteLine("A\\B = " + SET.Difference(B).ToString()); Console.WriteLine("B\\A = " + B.Difference(SET).ToString()); Console.WriteLine("A\\B OR B\\A = " + SET.SymmetricDifference(B).ToString()); Console.WriteLine("Member(88) = " + SET.SymmetricDifference(B).Contains(88)); SortedLinkedList <Int32> E = new SortedLinkedList <Int32>(); Console.WriteLine("Empty set(E) = " + E.ToString()); }
private void InsertNewNodeAndVerify(TwoThreeTree <int, int> t, List <int> elements, int newElement) { t.Upsert(newElement, newElement); elements.Add(newElement); this.Validate(t, elements); }
public void TestInsertZero() { var tree = new TwoThreeTree <string>(); Assert.AreEqual("", tree.ToString()); }
public static void PreOrderTraverse <TKey, TValue>(this TwoThreeTree <TKey, TValue> tree, Action <TKey, TValue> action) where TKey : IComparable <TKey> { PreOrderTraverse(tree.Root, action); }
public void Execute() { CultureInfo cul = new CultureInfo("en-GB"); StringComparer comp = StringComparer.Create(cul, true); TwoThreeTree <String> T1 = new TwoThreeTree <String>(comp); //2-3 TREE ArrayList <String> V = new ArrayList <String>(); // ARRAY LIST AS VECTOR. SortedLinkedList <String> SET; //SET. ArrayTree <String> TS = new ArrayTree <String>(); //SOURCE TREE. TS.Root().Value = "a"; //ROOT_VALUE TS.Add("b"); //DEPTH = 1, PARENT = ROOT. TS.Add("c"); TS.Add(2, 0, "d"); //DEPTH = 2, PARENT = FIRST_CHILD(ROOT) TS.Add(2, 0, "e"); /* * Int32 h = TS.MinHeight + 1;//MAX_DEPTH OF THE NODES * Console.WriteLine(h); * IVisitor<String> v = new NRVisitor<String>(); * ArrayList<ArrayList<String>> VS = new ArrayList<ArrayList<String>>(); * * * //PROCESS PATHS * v.PreOrder(TS, (n) => __AddAllPathsFromRootToLeaves(n, ref V)); * PrintPathsFromVector(ref V, VS, h); * for(Int32 i = 0; i < VS.Count; i++){ * ArrayList<String> VS_i = VS[i]; * AddFromList(ref VS_i, ref T1); * } * * SET = (SortedLinkedList<String>)T1; * Console.WriteLine(SET);*/ /* * TS.Clear(); * T1.Clear(); * TS.Root().Value = "a";//ROOT_VALUE * TS.Add("b");//DEPTH = 1, PARENT = ROOT. * TS.Add("c"); * /* * TS.Add(2,0,"d");//DEPTH = 2, PARENT = FIRST_CHILD(ROOT) * TS.Add(2,0,"e"); * TS.Add(3,1,"g");//TO e * TS.Add(4,0,"i"); * h = TS.MinHeight + 1; * Console.WriteLine(h); * VS.Clear(); * V.Clear(); * //PROCESS PATHS * /* * v.PreOrder(TS, (n) => __AddAllPathsFromRootToLeaves(n, ref V)); * PrintPathsFromVector(ref V, VS, h); * for(Int32 i = 0; i < VS.Count; i++){ * ArrayList<String> VS_i = VS[i]; * AddFromList(ref VS_i, ref T1); * } * * SortedLinkedList<String> SET2 = (SortedLinkedList<String>)T1; * Console.WriteLine(SET2); * Console.WriteLine(SET.Intersection(SET2));*/ }