public TraverseTests() { _tree = new Tree <int>(10); _tree.Insert(5); _tree.Insert(3); _tree.Insert(9); _tree.Insert(20); _tree.Insert(25); // _tree.Insert(15); }
public void ShouldReturnTrue_ForABinarySearchTree() { _tree.Insert(10); _tree.Insert(5); _tree.Insert(20); _tree.Insert(15); _tree.Insert(25); _tree.Insert(3); _tree.Insert(9); var isBst = _tree.CheckBst(); Assert.True(isBst); }
public void ShouldSetRootRightNode_WhenDataIsGreaterThanRootData() { const int data = 15; var tree = new Tree <int>(10); tree.Insert(data); Assert.False(tree.IsEmpty); Assert.NotNull(tree.Root.Right); Assert.Equal(data, tree.Root.Right.Data); }
public void ShouldSetRootLeftNode_WhenDataIsLessThanRootData() { const int data = 5; var tree = new Tree <int>(10); tree.Insert(data); Assert.False(tree.IsEmpty); Assert.NotNull(tree.Root.Left); Assert.Equal(data, tree.Root.Left.Data); }
public void ShouldSetRootNode_WhenTreeIsEmpty() { const int data = 5; var tree = new Tree <int>(); tree.Insert(data); Assert.False(tree.IsEmpty); Assert.NotNull(tree.Root); Assert.Equal(data, tree.Root.Data); }
static void Main(string[] args) { var tree1 = new Tree(7); tree1.Insert(4); tree1.Insert(9); tree1.Insert(1); tree1.Insert(6); tree1.Insert(8); tree1.Insert(10); var tree2 = new Tree(7); tree2.Insert(4); tree2.Insert(9); tree2.Insert(1); tree2.Insert(6); tree2.Insert(8); Console.WriteLine(tree1.IsEqual(tree2)); }
public void PrintTest() { _tree.Insert(10); _tree.Insert(5); _tree.Insert(20); _tree.Insert(15); _tree.Insert(25); _tree.Insert(3); _tree.Insert(9); _tree.Print(); }
static void Main(string[] args) { Tree tree = new Tree(); tree.Insert(7); tree.Insert(4); tree.Insert(9); tree.Insert(1); tree.Insert(6); tree.Insert(8); tree.Insert(10); foreach (var item in tree.FindAncestors(10)) { Console.WriteLine(item); } //tree.Traverse(); //tree.PrintNodesInKthDistance(1); }
static void Main(string[] args) { Tree <int> tree = new Tree <int>(); string input; int value; while (true) { Console.WriteLine("I = insert, D = delete, m = minimum, M = maximum, S = search, PO = pre-order, IO = in-order, pO = post-order"); input = Console.ReadLine(); if (input == "I") { Console.WriteLine("What doth thee wanteth to ins'rt?"); value = int.Parse(Console.ReadLine()); tree.Insert(value); Console.WriteLine("OK"); } if (input == "D") { Console.WriteLine("What doth thee wanteth to fordid?"); value = int.Parse(Console.ReadLine()); tree.Delete(value); Console.WriteLine("OK"); } if (input == "m") { Console.WriteLine("Of what sub-tree?"); value = int.Parse(Console.ReadLine()); Console.WriteLine(tree.Minimum(value).ToString()); } if (input == "M") { Console.WriteLine("Of what sub-tree?"); value = int.Parse(Console.ReadLine()); Console.WriteLine(tree.Maximum(value).ToString()); } if (input == "S") { Console.WriteLine("What doth thee wanteth to searcheth f'r?"); value = int.Parse(Console.ReadLine()); if (tree.Search(value)) { Console.WriteLine(""); Console.WriteLine("This value is somewh're in the tree. If 't be true thee wanteth to knoweth its exact location, prithee payeth me $5."); } else { Console.WriteLine(""); Console.WriteLine("Err'r numb'r 666."); Console.WriteLine("Eith'r this value is not in the tree, 'r thee oweth me wage."); Console.WriteLine("Whatev'r the case, i suggesteth yond thee starteth running."); } } if (input == "PO") { Console.WriteLine(""); int[] array = new int[tree.PreOrder().Length]; for (int i = 0; i < array.Length; i++) { array[i] = tree.PreOrder()[i]; } for (int i = 0; i < array.Length; i++) { Console.Write(array[i] + ", "); } } if (input == "IO") { Console.WriteLine(""); int[] array = new int[tree.InOrder().Length]; for (int i = 0; i < array.Length; i++) { array[i] = tree.InOrder()[i]; } for (int i = 0; i < array.Length; i++) { Console.Write(array[i] + ", "); } } if (input == "pO") { Console.WriteLine(""); int[] array = new int[tree.PostOrder().Length]; for (int i = 0; i < array.Length; i++) { array[i] = tree.PostOrder()[i]; } for (int i = 0; i < array.Length; i++) { Console.Write(array[i] + ", "); } } if (input == "z") { tree.Insert(20); tree.Insert(10); tree.Insert(30); tree.Insert(5); tree.Insert(15); tree.Insert(25); tree.Insert(35); } for (int i = 0; i < 5; i++) { Console.WriteLine(""); } } }
public void ShouldReturnOne_ForATreeWithOnlyOneNode() { _tree.Insert(1); Assert.Equal(1, _tree.Height()); }