Example #1
0
        public void Simple()
        {
            var binaryTree = new BinaryTree<int>(5);

            var child1 = new BinaryTree<int>(2);
            var child2 = new BinaryTree<int>(3);

            binaryTree.Add(child1);
            binaryTree.Add(child2);

            var child4 = new BinaryTree<int>(9);
            var child5 = new BinaryTree<int>(12);
            var child6 = new BinaryTree<int>(13);

            child1.Add(child4);
            child1.Add(child5);
            child2.Add(child6);

            Assert.AreEqual(binaryTree.FindNode(target => target == 2), child1);

            Assert.AreEqual(binaryTree.FindNode(target => target == 9), child4);

            Assert.AreEqual(binaryTree.FindNode(target => target == 13), child6);

            Assert.AreEqual(binaryTree.FindNode(target => target == 57), null);
        }
Example #2
0
        public void Interface()
        {
            var rootBinaryTree = new BinaryTree<int>(5);

            var child1 = new BinaryTree<int>(2);
            var child2 = new BinaryTree<int>(3);

            rootBinaryTree.Add(child1);
            rootBinaryTree.Add(child2);

            var child4 = new BinaryTree<int>(9);
            var child5 = new BinaryTree<int>(12);
            var child6 = new BinaryTree<int>(13);

            child1.Add(child4);
            child1.Add(child5);
            child2.Add(child6);

            ITree<int> tree = rootBinaryTree;
            Assert.AreEqual(tree.FindNode(target => target == 2), child1);

            Assert.AreEqual(tree.FindNode(target => target == 9), child4);

            Assert.AreEqual(tree.FindNode(target => target == 13), child6
                );

            Assert.AreEqual(tree.FindNode(target => target == 57), null);
        }
 public void DeleteTest()
 {
     var tree = new BinaryTree<float>();
     tree.Add(4.21f);
     tree.Add(421);
     tree.Add(42);
     tree.Add(1);
     tree.Add(1000.500f);
     tree.Delete(4.21f);
     Assert.IsFalse(tree.Contains(4.21f));
     Assert.IsTrue(tree.Contains(421));
     Assert.IsTrue(tree.Contains(42));
     Assert.IsTrue(tree.Contains(1));
     tree.Delete(421);
     Assert.IsFalse(tree.Contains(421));
     Assert.IsTrue(tree.Contains(1));
     tree.Delete(1);
     Assert.IsFalse(tree.Contains(1));
     Assert.IsFalse(tree.Contains(421));
     Assert.IsTrue(tree.Contains(42));
     tree.Delete(42);
     Assert.IsFalse(tree.Contains(42));
     Assert.IsTrue(tree.Contains(1000.500f));
     tree.Delete(1000.500f);
     Assert.IsFalse(tree.Contains(1000.500f));
     Assert.IsTrue(tree.IsEmpty());
 }
Example #4
0
        public void Simple()
        {
            var binaryTree = new BinaryTree<int>(5);

            Assert.AreEqual(binaryTree.Count, 0);
            Assert.AreEqual(binaryTree.Degree, 0);
            Assert.AreEqual(binaryTree.Data, 5);

            binaryTree.Add(3);

            Assert.AreEqual(binaryTree.Count, 1);
            Assert.AreEqual(binaryTree.Degree, 1);
            Assert.AreEqual(binaryTree.Data, 5);

            Assert.AreEqual(binaryTree.Left.Data, 3);
            Assert.IsNull(binaryTree.Right);

            binaryTree.Add(4);

            Assert.AreEqual(binaryTree.Count, 2);
            Assert.AreEqual(binaryTree.Degree, 2);
            Assert.AreEqual(binaryTree.Data, 5);

            Assert.AreEqual(binaryTree.Left.Data, 3);
            Assert.AreEqual(binaryTree.Right.Data, 4);
        }
        public void DuplicateKeysNotAllowed()
        {
            BinaryTree<int, string> tree = new BinaryTree<int, string>();

            tree.Add(3, "a");
            tree.Add(3, "a");
        }
Example #6
0
 public void BeginTestMethod()
 {
     target = new BinaryTree<String>();
     target.Add(3, "three");
     target.Add(2, "two");
     target.Add(7, "seven");
 }
 public void Add_ToFullIntTree_ExpandsTree()
 {
     BinaryTree<int> tree = new BinaryTree<int>(new int[] { 54, 25, 316, 4, 55, 6, });
     tree.Add(11);
     tree.Add(41);
     tree.Add(111);
     Assert.AreEqual(9, tree.Count);
 }
 public void CountTest()
 {
     var tree = new BinaryTree<int>();
     tree.Add(new BinaryTreeNode<int>(1));
     Assert.AreEqual(1, tree.Count);
     tree.Add(new BinaryTreeNode<int>(2));
     Assert.AreEqual(2, tree.Count);
     tree.Add(new BinaryTreeNode<int>(0));
     Assert.AreEqual(3, tree.Count);
 }
 public void IndexTest()
 {
     var d = new BinaryTree<int, bool>();
     d.Add(10, true);
     d.Add(20, true);
     d.Add(30, true);
     d.Add(40, true);
     d.Add(25, true);
     Assert.AreEqual(2, d.IndexOfKey(25));
 }
Example #10
0
 public void BinaryTreeAddLeaf()
 {
     var tree = new BinaryTree<int>();
     tree.Add(4);
     tree.Add(1);
     tree.Add(10);
     tree.ShouldContain(1);
     tree.ShouldContain(4);
     tree.ShouldContain(10);
 }
 public void Creation()
 {
     BinaryTree<int> Tree = new BinaryTree<int>();
     Tree.Add(1);
     Tree.Add(2);
     Tree.Add(0);
     Tree.Add(-1);
     Assert.Equal(-1, Tree.MinValue);
     Assert.Equal(2, Tree.MaxValue);
 }
Example #12
0
 /// <summary>
 /// Main program method
 /// </summary>
 /// <param name="args"></param>
 private static void Main(string[] args)
 {
     var tree = new BinaryTree<int>();
     tree.Add(5);
     tree.Add(3);
     tree.Add(6);
     tree.Print();
     tree.Delete(6);
     tree.Print();
     Console.WriteLine(tree.Contains(6));
 }
        public void BinaryTree_Test()
        {
            BinaryTree<int> tree = new BinaryTree<int>();
            tree.Add(5);
            tree.Add(10);
            tree.Add(80);
            tree.Add(5);
            tree.Add(2);

            Assert.AreEqual(4, tree.Count);
        }
 public void DeleteTest2()
 {
     var tree = new BinaryTree<int>();
     tree.Add(4);
     tree.Add(1);
     tree.Add(3);
     tree.Add(2);
     tree.Add(5);
     tree.Delete(4);
     Assert.IsTrue(tree.Contains(3));
 }
        public void FindParentTest()
        {
            var tree = new BinaryTree<int>();
            var node0 = new BinaryTreeNode<int>(0);
            var node1 = new BinaryTreeNode<int>(1);
            var node2 = new BinaryTreeNode<int>(2);
            var node3 = new BinaryTreeNode<int>(3);
            var node4 = new BinaryTreeNode<int>(4);
            var node5 = new BinaryTreeNode<int>(5);
            var node6 = new BinaryTreeNode<int>(6);
            var node7 = new BinaryTreeNode<int>(7);
            var node8 = new BinaryTreeNode<int>(8);
            var node9 = new BinaryTreeNode<int>(9);
            var node10 = new BinaryTreeNode<int>(10);
            var node11 = new BinaryTreeNode<int>(11);
            var node12 = new BinaryTreeNode<int>(12);
            var node13 = new BinaryTreeNode<int>(13);
            var node14 = new BinaryTreeNode<int>(14);
            var node15 = new BinaryTreeNode<int>(15);
            var node16 = new BinaryTreeNode<int>(16);

            tree.Add(node8);
            tree.Add(node4);
            tree.Add(node12);
            tree.Add(node2);
            tree.Add(node6);
            tree.Add(node10);
            tree.Add(node14);
            tree.Add(node1);
            tree.Add(node3);
            tree.Add(node5);
            tree.Add(node7);
            tree.Add(node9);
            tree.Add(node11);
            tree.Add(node13);
            tree.Add(node15);
            tree.Add(node0);
            tree.Add(node16);

            // Structure:
            //                8
            //        4               12
            //    2       6      10        14
            //  1   3   5   7  9    11  13    15
            //0                                  16

            Assert.AreEqual(node15, tree.FindParent(tree.Top, 16));
            Assert.AreEqual(node1, tree.FindParent(tree.Top, 0));
            Assert.AreEqual(node2, tree.FindParent(tree.Top, 1));
            Assert.AreEqual(node2, tree.FindParent(tree.Top, 3));
            Assert.AreEqual(node8, tree.FindParent(tree.Top, 4));

        }
        public void BinaryTreeTest_Add()
        {
            BinaryTree<int> tree = new BinaryTree<int>();

            tree.Add(6);
            tree.Add(3);
            tree.Add(9);
            tree.Add(1);
            tree.Add(4);

            int i = 0;
        }
Example #17
0
        public void Simple()
        {
            ITree<int> binaryTree = new BinaryTree<int>(5);

            var child1 = new BinaryTree<int>(4);
            var child2 = new BinaryTree<int>(6);

            binaryTree.Add(child1);
            binaryTree.Add(child2);
            Assert.AreEqual(binaryTree.Degree, 2);
            Assert.AreEqual(binaryTree.GetChild(0), child1);
            Assert.AreEqual(binaryTree.GetChild(1), child2);
        }
 public void Add_ToFullIntTree_AddsItemsRight()
 {
     BinaryTree<int> tree = new BinaryTree<int>(new int[] { 54, 25, 316, 4, 55, 6, });
     tree.Add(11);
     tree.Add(41);
     tree.Add(111);
     int[] actual = new int[9];
     int i = 0;
     foreach (var item in tree.InOrderTraversal)
         actual[i++] = item;
     IStructuralEquatable expected = new int[] { 4, 6, 11, 25, 41, 54, 55, 111, 316 };
     Assert.IsTrue(expected.Equals(actual, StructuralComparisons.StructuralEqualityComparer));
 }
 public void AddTest()
 {
     var tree = new BinaryTree<int>();
     Assert.IsTrue(tree.IsEmpty());
     tree.Add(3);
     Assert.IsFalse(tree.IsEmpty());
     tree.Add(1);
     tree.Add(4);
     Assert.IsTrue(tree.Contains(3));
     Assert.IsTrue(tree.Contains(1));
     Assert.IsTrue(tree.Contains(4));
     Assert.IsFalse(tree.Contains(2));
     Assert.IsFalse(tree.IsEmpty());
 }
Example #20
0
        private int Solve()
        {
            Square current = new Square(new int[] {0, 0}, 1, 0);
            BinaryTree<Square> store = new BinaryTree<Square>();
            store.Add(current);

            double targetSize = 0;

            while(!store.Empty()) {
                current = store.PopMax();

                int[] curentCoordinates = current.GetCoordinates();
                if(curentCoordinates[0] == coordinates[0] && curentCoordinates[1] == coordinates[1]) {
                    targetSize = current.GetSize();
                }

                Square[] children = current.GetChildren();
                foreach (Square child in children) {
                    int[] childCoordinates = child.GetCoordinates();
                    if(childCoordinates[0] <= coordinates[0] && childCoordinates[1] <= coordinates[1]) {
                        store.Add(child);
                    }
                }
            }

            int count = 0;
            int result = -1;

            current = new Square(new int[] { 0, 0 }, 1, 0);
            store.Add(current);

            while (!store.Empty()) {
                count++;
                current = store.PopMax();

                int[] curentCoordinates = current.GetCoordinates();
                if (curentCoordinates[0] == coordinates[0] && curentCoordinates[1] == coordinates[1]) {
                    result = count;
                }

                Square[] children = current.GetChildren();
                for (int i = 0; i < children.Length; i++) {
                    if (targetSize <= children[i].GetSize()) {
                        store.Add(children[i]);
                    }
                }
            }

            return result;
        }
        public void BuildTree()
        {
            BinaryTree<int, string> tree = new BinaryTree<int, string>();

            var a = tree.Add(3, "a");
            var b = tree.Add(2, "b");
            var c = tree.Add(1, "c");
            var d = tree.Add(4, "d");

            Assert.IsTrue(a.Parent == null);
            Assert.IsTrue(a == tree.Root);
            Assert.IsTrue(a.Left == b);
            Assert.IsTrue(b.Left == c);
            Assert.IsTrue(a.Right == d);
        }
Example #22
0
        public void Book_PostOrderTraversal_DefaultComparerTest()
        {
            List<Book> booksList = new List<Book>()
            {
                new Book(){Author = "Mark Twen", Title = "Oliver Twist",
                    Year = 1935, Publisher = "WilliamsPublish.", PagesQuantity = 350},
                new Book(){Author = "Andew Tanenbaum", Title = "Computer Networks",
                    Year = 2010, Publisher = "ClassicOfComputerScience.", PagesQuantity = 750},
                new Book(){Author = "Gang Of Fours", Title = "Design Patterns",
                    Year = 1995, Publisher = "O'Realy.", PagesQuantity = 520}
            };
            BinaryTree<Book> bt = new BinaryTree<Book>();
            foreach (var item in booksList)
            {
                bt.Add(item);
            }
            List<Book> expectedList = new List<Book>()
            {

                new Book(){Author = "Gang Of Fours", Title = "Design Patterns",
                    Year = 1995, Publisher = "O'Realy.", PagesQuantity = 520},
                new Book(){Author = "Andew Tanenbaum", Title = "Computer Networks",
                    Year = 2010, Publisher = "ClassicOfComputerScience.", PagesQuantity = 750},
                new Book(){Author = "Mark Twen", Title = "Oliver Twist",
                    Year = 1935, Publisher = "WilliamsPublish.", PagesQuantity = 350},
            };
            List<Book> actualList = new List<Book>();
            foreach (var item in bt.Postorder())
            {
                actualList.Add(item);
            }
            CollectionAssert.AreEqual(expectedList,actualList);
        }
Example #23
0
 public static void AddElements(BinaryTree<int> binaryTree, int count)
 {
     for (int i = 1; i <= count; i++)
     {
         binaryTree.Add(i);
     }
 }
        public void BinaryTree_Foreach_Test()
        {
            BinaryTree<int> tree = new BinaryTree<int>();
            tree.Add(5);
            tree.Add(10);
            tree.Add(80);
            tree.Add(5);
            tree.Add(2);

            int count = 0;
            foreach (var item in tree)
            {
                count++;
            }
            Assert.AreEqual(4, count);
        }
Example #25
0
 public void Point2D_InOrderTraversal_CustomComparerTest()
 {
     BinaryTree<Point2D> bt = new BinaryTree<Point2D>();
     Point2D[] elemToAdd =
     {
         new Point2D{X = 1, Y = 2},
         new Point2D{X = 10, Y = 2},
         new Point2D{X = 2, Y = 1},
         new Point2D{X = 4, Y = 4}
     };
     bt.Comparer = new CustomComparer<Point2D>((p1,p2) => (p1.X + p1.Y) - (p2.X+p2.Y));
     foreach (var item in elemToAdd)
         bt.Add(item);
     List<Point2D> actual = new List<Point2D>();
     foreach (var item in bt.Inorder())
     {
         actual.Add(item);
     }
     Point2D[] expected =
     {
         new Point2D{X = 2, Y = 1},
         new Point2D{X = 1, Y = 2},
         new Point2D{X = 4, Y = 4},
         new Point2D{X = 10, Y = 2}
     };
     CollectionAssert.AreEqual(expected, actual);
 }
 public static void AddNumber(BinaryTree<int> tree, int number)
 {
     tree.Add(number);
     Console.WriteLine("Added " + number);
     Traverse(tree.Root, "");
     Console.WriteLine("----------------------");
 }
        public void TestMethod_remove()
        {
            BinaryTree<int> bt = new BinaryTree<int>();
            bt.Add(10);

            Assert.AreEqual(true, bt.Remove(10));
        }
Example #28
0
        public void Interface()
        {
            ITree<int> binaryTree = new BinaryTree<int>(5);

            var child1 = new BinaryTree<int>(4);
            var child2 = new BinaryTree<int>(6);
            var child3 = new BinaryTree<int>(7);

            binaryTree.Add(child1);
            binaryTree.Add(child2);
            Assert.AreEqual(binaryTree.Degree, 2);
            Assert.IsTrue(binaryTree.Remove(child1));
            Assert.AreEqual(binaryTree.Degree, 1);
            Assert.IsFalse(binaryTree.Remove(child3));
            Assert.AreEqual(binaryTree.Degree, 1);
            Assert.IsTrue(binaryTree.Remove(child2));
            Assert.AreEqual(binaryTree.Degree, 0);
        }
Example #29
0
        public void TestAddInt()
        {
            BinaryTree <int> tree = new BinaryTree <int>();

            tree.Add(6);
            var node = tree.Search(6);

            Assert.AreEqual(node.Data, 6);
        }
Example #30
0
        public void TestRemove()
        {
            Student st = new Student("JoJo", "WitchCraft", DateTime.Now, 10);
            BinaryTree <Student> tree = InitTree();

            tree.Add(st);
            tree.Remove(st);
            Assert.AreEqual(null, tree.Search(st));
        }
Example #31
0
        public void TestFindOFUnavailableEl()
        {
            BinaryTree <string> tree = new BinaryTree <string>();

            tree.Add(5, "Иванов");
            BinaryTree <string> t2 = tree.Find(7);

            Assert.AreEqual(null, t2);
        }
Example #32
0
        public void Add_AddValueTwentyToTestTree_TreeContainsTwenty()
        {
            TestTree.Add(20);
            bool expected = true;

            bool actual = TestTree.Contains(20);

            Assert.AreEqual(expected, actual, "Tree dont have added value");
        }
Example #33
0
        public void Interface()
        {
            ITree<int> binaryTree = new BinaryTree<int>(5);

            var child = new BinaryTree<int>(4);
            binaryTree.Add(child);
            Assert.AreEqual(binaryTree.Degree, 1);
            Assert.AreEqual(binaryTree.GetChild(0), child);
        }
Example #34
0
    // Start is called before the first frame update
    void Start()
    {
        binaryTree = gameObject.AddComponent <BinaryTree>();

        for (int i = 0; i < binaryValues.Count; i++)
        {
            binaryTree.Add(binaryValues[i]);
        }
    }
        public void CanFindNodeInBinaryTree()
        {
            Node       datRoot = new Node(3);
            BinaryTree datBT   = new BinaryTree(datRoot);

            datBT.Add(datRoot, 5);
            datBT.Add(datRoot, 7);
            datBT.Add(datRoot, 10);
            datBT.Add(datRoot, 13);
            datBT.Add(datRoot, 15);

            Assert.Equal(10, datBT.Search(datBT.Root, 10).Value);
            Assert.Equal(5, datBT.Search(datBT.Root, 5).Value);
            Assert.Equal(7, datBT.Search(datBT.Root, 7).Value);
            Assert.Null(datBT.Search(datBT.Root, 555));
            Assert.Null(datBT.Search(datBT.Root, 42));
            Assert.Null(datBT.Search(datBT.Root, 1337));
        }
Example #36
0
        public void ReturnsRootValueIfNoChildrenForFindMaxValueMethod()
        {
            BinaryTree <int> Bt = new BinaryTree <int>();

            Bt.Add(13);
            int actual = Bt.FindMaximumValue(Bt.Root);

            Assert.Equal(13, actual);
        }
Example #37
0
        public void Add_KeyNull_ThrowsException()
        {
            //Arrange
            const string data = "DummyData";
            var          tree = new BinaryTree <string, string>();

            //Act & Assert
            Assert.Throws <ArgumentNullException>(() => tree.Add(null, data));
        }
Example #38
0
        public void Interface()
        {
            ITree <int> binaryTree = new BinaryTree <int>(5);

            var child1 = new BinaryTree <int>(4);
            var child2 = new BinaryTree <int>(6);
            var child3 = new BinaryTree <int>(7);

            binaryTree.Add(child1);
            binaryTree.Add(child2);
            Assert.AreEqual(binaryTree.Degree, 2);
            Assert.IsTrue(binaryTree.Remove(child1));
            Assert.AreEqual(binaryTree.Degree, 1);
            Assert.IsFalse(binaryTree.Remove(child3));
            Assert.AreEqual(binaryTree.Degree, 1);
            Assert.IsTrue(binaryTree.Remove(child2));
            Assert.AreEqual(binaryTree.Degree, 0);
        }
        public void TreeAdd500AssertCountOneHeadValue500()
        {
            var bst = new BinaryTree <int>();

            bst.Add(500);

            Assert.AreEqual(1, bst.Count);
            Assert.AreEqual(500, bst.Head.Value);
        }
Example #40
0
        public void CanAddNodeToRoot()
        {
            BinaryTree <int> Bt = new BinaryTree <int>();

            Bt.Add(12);
            int actual = Bt.Root.Value;

            Assert.Equal(12, actual);
        }
        public void FindMaxValueHandlesNegativeNumbers()
        {
            //Arrange
            BinaryTree bt   = new BinaryTree();
            Node       root = new Node(-21);

            bt.Add(root, new Node(-5));
            bt.Add(root, new Node(-3));
            bt.Add(root, new Node(-15));
            bt.Add(root, new Node(-2));
            bt.Add(root, new Node(-7));

            //Act
            int response = bt.FindMaximumValue(root);

            //Assert
            Assert.Equal(-2, response);
        }
Example #42
0
        public void GivenTreesWithoutIntersection_ReturnsNull()
        {
            // arrange
            BinaryTree <int> binaryTree1 = new BinaryTree <int>();
            BinaryTree <int> binaryTree2 = new BinaryTree <int>();
            List <int>       expectedEmptyIntersection = new List <int>();

            // act
            binaryTree1.Add(30);
            binaryTree1.Add(20);
            binaryTree2.Add(1);
            binaryTree2.Add(2);
            // assert
            List <int> actualResult = Program.TreeIntersection(binaryTree1, binaryTree2);

            // assert
            Assert.Equal(expectedEmptyIntersection, actualResult);
        }
Example #43
0
        public void TestInterfaceRemove()
        {
            ITree <int> tree = new BinaryTree <int>(5);

            BinaryTree <int> child1 = new BinaryTree <int>(4);
            BinaryTree <int> child2 = new BinaryTree <int>(6);
            BinaryTree <int> child3 = new BinaryTree <int>(7);

            tree.Add(child1);
            tree.Add(child2);
            Assert.AreEqual(tree.Degree, 2);
            Assert.AreEqual(tree.Remove(child1), true);
            Assert.AreEqual(tree.Degree, 1);
            Assert.AreEqual(tree.Remove(child3), false);
            Assert.AreEqual(tree.Degree, 1);
            Assert.AreEqual(tree.Remove(child2), true);
            Assert.AreEqual(tree.Degree, 0);
        }
Example #44
0
        public void Can_Del_To_1()
        {
            var tree = new BinaryTree();

            tree.Add(7);
            tree.Del(7);
            Assert.AreEqual(0, tree.Size);
            Assert.IsFalse(tree.Contains(7));
        }
        public void BinaryTreeTest_PostOrder()
        {
            BinaryTree<int> tree = new BinaryTree<int>();
            String order = String.Empty;

            tree.Add(6);
            tree.Add(3);
            tree.Add(9);
            tree.Add(1);
            tree.Add(4);

            foreach (var item in tree.Order(TreeOrder.Postorder))
            {
                order += item;
            }

            Assert.AreEqual("96431", order);
        }
        // this main class is heavily based off of Eric Singleton's
        // using Eric's as a reference for the invokcation
        static void Main(string[] args)
        {
            BinaryTree binaryTree = new BinaryTree(new Node(2));

            binaryTree.Add(new Node(3), binaryTree.Root);
            binaryTree.Add(new Node(5), binaryTree.Root);
            binaryTree.Add(new Node(7), binaryTree.Root);
            binaryTree.Add(new Node(11), binaryTree.Root);

            BinarySearchTree bst = new BinarySearchTree(new Node(577));

            bst.Add(new Node(587), bst.Root);
            bst.Add(new Node(593), bst.Root);
            bst.Add(new Node(599), bst.Root);
            bst.Add(new Node(601), bst.Root);

            Console.WriteLine("BreadthFirst:");
            binaryTree.BreadthFirst(binaryTree.Root);
            Console.WriteLine("------");
            bst.BreadthFirst(bst.Root);
            Console.ReadLine();
            Console.Clear();

            Console.WriteLine("PreOrder:");
            binaryTree.PreOrder(binaryTree.Root);
            Console.WriteLine("------");
            bst.PreOrder(bst.Root);
            Console.ReadLine();
            Console.Clear();

            Console.WriteLine("InOrder:");
            binaryTree.InOrder(binaryTree.Root);
            Console.WriteLine("------");
            bst.InOrder(bst.Root);
            Console.ReadLine();
            Console.Clear();

            Console.WriteLine("PostOrder:");
            binaryTree.PostOrder(binaryTree.Root);
            Console.WriteLine("------");
            bst.PostOrder(bst.Root);
            Console.ReadLine();
            Console.Clear();
        }
Example #47
0
        public void TestSearch()
        {
            Student st = new Student("JoJo", "WitchCraft", DateTime.Now, 10);
            BinaryTree <Student> tree = InitTree();

            tree.Add(st);
            Student st1 = tree.Search(st).Data;

            Assert.AreEqual(st.CompareTo(st1), 0);
        }
        public void VerifyRangeSingleElement(TreeType type)
        {
            BinaryTree <long, string> testTree = TreeFactory(type);

            testTree.Add(4, "4");

            string a = BuildTestString(testTree.Range(4, 4));

            Assert.Equal("4", a);
        }
        public void NewNodeInLeftChild()
        {
            //Arrange
            Node       node1   = new Node(10);
            Node       node2   = new Node(2);
            Node       node3   = new Node(13);
            BinaryTree twoTree = new BinaryTree(node1);

            twoTree.Add(node2.Data);
            twoTree.Add(node3.Data);

            //Act
            var check1 = twoTree.Root.LChild.Data;
            var check2 = twoTree.Root.RChild.Data;

            //Assert
            Assert.Equal(2, check1);
            Assert.Equal(13, check2);
        }
        public void addTest()
        {
            BinaryTree <int> target = new BinaryTree <int>(); // TODO: Initialize to an appropriate value

            int[] data = { 1, 2, 4, 7, 8, 10 };               // TODO: Initialize to an appropriate value
            for (int i = 0; i < data.Length; i++)
            {
                target.Add(i, data[i]);
            }
        }
        public void Add_AddedNodeIsNull_ArgumentNullException()
        {
            //arrange
            var bt = new BinaryTree <int>();
            //act
            Action act = () => { bt.Add(null); };

            //assert is handled by ExpectedException
            Assert.Throws <ArgumentNullException>(act);
        }
        public void Add_AddTestInfoFirstElementToTheTree_AddedElementIsARoot()
        {
            //arrange
            BinaryTree <TestInfo> bt = new BinaryTree <TestInfo>();

            //act
            bt.Add(new TestInfo("Test0", 1));
            //assert
            Assert.Equal(new TestInfo("Test0", 1), bt.RootNode.Data);
        }
Example #53
0
        private void ReadToTree()
        {
            _tree = new BinaryTree <T>();
            var list = _storage.ReadAll();

            foreach (var item in list)
            {
                _tree.Add(item);
            }
        }
Example #54
0
        public void In3_out1()
        {
            //arrange
            int expected = 23;

            BinaryTree <int> tree = new BinaryTree <int>(BinaryTree <int> .CompareInt);

            tree.Add(20);
            tree.Add(29);
            tree.Add(23);

            Node <int> node = tree.DFSearching(23);

            //act
            int actual = node.Value;

            //assert
            Assert.AreEqual(expected, actual);
        }
Example #55
0
        private BinaryTree <int, int> GetFilledTree(int count)
        {
            var tree = new BinaryTree <int, int>();

            foreach (var key in Enumerable.Range(0, count))
            {
                tree.Add(key, default);
            }
            return(tree);
        }
        public void Add_Student_Method_Test(Student student)
        {
            //arrange
            BinaryTree <Student> tree = new BinaryTree <Student>();
            //act
            bool actual = tree.Add(student);

            //assert
            Assert.IsTrue(actual);
        }
Example #57
0
        public void BinaryTreeCloneTestMethod()
        {
            BinaryTree <Worker> binaryTree = new BinaryTree <Worker>();

            binaryTree.Add(new Worker("Da", "Net", 5, "Fire"));
            BinaryTree <Worker> binaryTreeClone = new BinaryTree <Worker>();

            binaryTreeClone = (BinaryTree <Worker>)binaryTree.Clone();
            Assert.IsNotNull(binaryTreeClone);
        }
Example #58
0
        public void Interface()
        {
            ITree <int> binaryTree = new BinaryTree <int>(5);

            var child = new BinaryTree <int>(4);

            binaryTree.Add(child);
            Assert.AreEqual(binaryTree.Degree, 1);
            Assert.AreEqual(binaryTree.GetChild(0), child);
        }
        private void AddElement(int value)
        {
            if (_tree.Contains(value))
            {
                return;
            }

            _tree.Add(value);
            RebuildTree(true);
        }
Example #60
0
        // Binary Tree not working yet on Mono
        // see bug: http://bugzilla.ximian.com/show_bug.cgi?id=78502
#if !MONO
        /// <summary>
        /// Indexes a DBF column in a binary tree [NOT COMPLETE]
        /// </summary>
        /// <typeparam name="T">datatype to be indexed</typeparam>
        /// <param name="columnId">Column to index</param>
        /// <returns></returns>
        public BinaryTree <T, UInt32> CreateDbfIndex <T>(int columnId) where T : IComparable <T>
        {
            var tree = new BinaryTree <T, uint>();

            for (uint i = 0; i < ((_numberOfRecords > 10000) ? 10000 : _numberOfRecords); i++)
            {
                tree.Add(new BinaryTree <T, uint> .ItemValue((T)GetValue(i, columnId), i));
            }
            return(tree);
        }