public virtual TItem TestAddRange_RangeOfNewValues_Success(IEnumerable <TItem> collection, IComparer <TItem> comparer) { BinarySearchTree <TItem> tree = CreateTree(comparer); tree.AddRange(collection); return(tree.Root.data); }
public void Contains_TreeContainsInteger_ReturnTrue() { var tree = new BinarySearchTree <int>(); var array = new int[] { 1, 5, 7, 3, 10 }; tree.AddRange(array); Assert.IsTrue(tree.Contains(5)); }
public void PreOrder_DescIntegerComparer_ReturnItems() { var array = new int[] { 5, 6, 3, 2, 1 }; var actual = new int[array.Length]; var tree = new BinarySearchTree <int>(new DescComparer()); tree.AddRange(array); int i = 0; foreach (var item in tree.PreOrder()) { actual[i++] = item; } Assert.AreEqual(array, actual); }
public BinarySearchPage() { DataContext = this; Items = new ObservableCollection <BinarySearchTree <int, string> >(); _tree = new BinarySearchTree <int, string>(5, "Some value for key 5"); _tree.AddRange(new Dictionary <int, string> { { 4, "Some value for key 4" }, { 15, "Some value for key 15" }, { 1, "Some value for key 1" }, }); InitializeComponent(); UpdateItemsList(); }
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()); }
static void ShowMenuBST(BinarySearchTree <int> tree, TreeTypeConverter <int> converter) { WriteLine(); WriteLine("BST Menu:"); WriteLine("0 : Create new"); WriteLine("1 : Add value"); WriteLine("2 : Remove value"); WriteLine("3 : Traverse: Pre-Order"); WriteLine("4 : Traverse: In-Order"); WriteLine("5 : Traverse: Post-Order"); WriteLine("6 : Fill: FullTreeData"); WriteLine("7 : Fill: RandomTreeData"); WriteLine("<any other> : Exit from BST"); var read = ReadLine(); int value = default; bool exit = false; switch (read) { case "0": WriteLine("Enter value of a future Root node or press Enter to create without Root:"); read = ReadLine(); if (string.IsNullOrEmpty(read)) { tree = new BinarySearchTree <int>(); WriteLine("Tree created"); break; } if (!converter.TryParse(read, out value)) { WriteLine("Unable to convert input value. Tree will not be created"); break; } else { tree = new BinarySearchTree <int>(value); break; } case "1": WriteLine("Enter value to add:"); read = ReadLine(); if (!converter.TryParse(read, out value)) { WriteLine("Unable to convert input value"); break; } tree.Add(value); break; case "2": WriteLine("Enter value to remove:"); read = ReadLine(); if (!converter.TryParse(read, out value)) { WriteLine("Unable to convert input value"); break; } tree.Remove(value); break; case "3": WriteLine(string.Join(" | ", tree.TraversePreOrder())); break; case "4": WriteLine(string.Join(" | ", tree.TraverseInOrder())); break; case "5": WriteLine(string.Join(" | ", tree.TraversePostOrder())); break; case "6": tree = new BinarySearchTree <int>(); tree.AddRange(DataGenerator.FullTreeData()); break; case "7": WriteLine($"Enter number of nodes, max is {DataGenerator.MaxRandomItemsCount} :"); read = ReadLine(); if (!int.TryParse(read, out int numberOfRoots)) { WriteLine("Unable to convert input value"); break; } if (numberOfRoots < 1) { WriteLine("Number of nodes must be greater than 0"); break; } tree = new BinarySearchTree <int>(); tree.AddRange(DataGenerator.RandomTreeData(numberOfRoots)); break; default: exit = true; break; } if (exit) { return; } ShowMenuBST(tree, converter); }