Exemplo n.º 1
0
        static void Main(string[] args)
        {
            BinaryTree bt = new BinaryTree();

            //bt.AddNode("18", 18);
            //bt.AddNode("7", 7);
            //bt.AddNode("6", 6);
            //bt.AddNode("9", 9);
            //bt.AddNode("2", 2);
            //bt.AddNode("2", 2);
            //bt.AddNode("18", 18);

            var rand = new Random(DateTime.UtcNow.Millisecond);
            for (int i = 0; i < 100; i++)
            {

                //int randInt = (int)(rand.NextDouble() * 1000000);

                //bt.AddNode(randInt.ToString(), randInt);

                bt.AddNode("1", 1);
            }

            int a = 5;

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

            //        4
            //       / \
            //      2   5
            //     / \   \
            //    1   3   7
            //           / \
            //          6   8

            tree.Add(4);
            tree.Add(5);
            tree.Add(2);
            tree.Add(7);
            tree.Add(3);
            tree.Add(6);
            tree.Add(1);
            tree.Add(8);

            int[] expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            int index = 0;

            foreach (int actual in tree)
            {
                Assert.AreEqual(expected[index++], actual, "The item enumerated in the wrong order");
            }
        }
        public void InOrder_Delegate()
        {
            BinaryTree<int> tree = new BinaryTree<int>();

            //        4
            //       / \
            //      2   5
            //     / \   \
            //    1   3   7
            //           / \
            //          6   8

            tree.Add(4);
            tree.Add(5);
            tree.Add(2);
            tree.Add(7);
            tree.Add(3);
            tree.Add(6);
            tree.Add(1);
            tree.Add(8);

            int[] expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            int index = 0;

            tree.InOrderTraversal(item => Assert.AreEqual(expected[index++], item, "The item enumerated in the wrong order"));
        }
        static void Main(string[] args)
        {
            BinaryTree<string> tree = new BinaryTree<string>();

            string input = string.Empty;

            while (!input.Equals("quit", StringComparison.CurrentCultureIgnoreCase))
            {
                // read the line from the user
                Console.Write("> ");
                input = Console.ReadLine();

                // split the line into words (on space)
                string[] words = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                // add each word to the tree
                foreach (string word in words)
                {
                    tree.Add(word);
                }

                // print the number of words
                Console.WriteLine("{0} words", tree.Count);

                // and print each word using the default (in-order) enumerator
                foreach (string word in tree)
                {
                    Console.Write("{0} ", word);
                }

                Console.WriteLine();

                tree.Clear();
            }
        }
Exemplo n.º 5
0
        static void Main()
        {
            var binaryTree = new BinaryTree<int> { 90, 50, 150, 20, 75, 5, 66, 80, 68 };

            Console.WriteLine("Created tree:");
            PrintTree(binaryTree);

            var nodeToRemove = binaryTree[(new Random()).Next(0, binaryTree.Count)];
            binaryTree.Remove(nodeToRemove);

            Console.WriteLine("\nTree with removed node [ {0} ]:", nodeToRemove);
            PrintTree(binaryTree);

            Console.WriteLine("\n5th element is: {0}", binaryTree[4]);

            // creating more interesting tree
            var tree = new BinaryTree<int> { 90, 50, 150, 20, 75, 95, 175, 5, 25, 66, 80, 92, 111, 166, 200 };

            Console.WriteLine("\n\nPreorder traverse:");
            tree.Traverse(node => Console.Write("[{1}]:{0} ", node, node.Level), "Preorder");

            Console.WriteLine("\n\nInorder Traverse:");
            tree.Traverse(node => Console.Write("[{1}]:{0} ", node, node.Level), "Inorder");

            Console.WriteLine("\n\nPostorder Traverse:");
            tree.Traverse(node => Console.Write("[{1}]:{0} ", node, node.Level), "Postorder");

            Console.WriteLine("\n\nWidth Traverse (foreach use this method):");
            tree.Traverse(node => Console.Write("[{1}]:{0} ", node, node.Level), "Width");

            Console.WriteLine();

            Console.ReadKey();
        }
Exemplo n.º 6
0
        static void BinaryTreeTest()
        {
            Console.WriteLine("Binary Search Tree test");

            var tree = new BinaryTree.BinaryTree();
            foreach (int v in arr)
            {
                tree.Insert(new Node(v));
            }

            //tree.Print();
            Console.WriteLine("Inorder walk of binary search tree");
            tree.PrintSorted();

            Console.WriteLine("Print successor of each node");
            foreach (int v in arr)
            {
                Node node = tree.Find(v);
                node.Print();
                Node successor = tree.FindSuccessor(node);
                if (successor != null) successor.Print();
                Console.WriteLine();
            }

            Console.WriteLine("Delete each of the nodes and print resulting tree");
            foreach (int v in arr)
            {
                tree.Delete(v);
                tree.PrintSorted();
                Console.WriteLine();
            }
        }
Exemplo n.º 7
0
        private void btnCreate_Click(object sender, EventArgs e)
        {
            _tree = new BinaryTree();
            //lblEvents.Text = @"new binary tree";
            Node root = new Node(25);
            _tree.Add(root);

            _tree.Add(new Node(20));

            _tree.Add(new Node(30));

            _tree.Add(new Node(10));
            _tree.Add(new Node(23));

            _tree.Add(new Node(35));

            _tree.Add(new Node(5));
            _tree.Add(new Node(4));
            _tree.Add(new Node(6));

            _tree.Add(new Node(21));
            _tree.Add(new Node(19));
            _tree.Add(new Node(24));

            _tree.Add(new Node(32));
            _tree.Add(new Node(50));
            _tree.Add(new Node(33));

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

            //     3
            //    /
            //   2
            //  /
            // 1


            tree.Add(3);
            tree.Add(2);
            tree.Add(1);

            tree.Remove(3);

            //   2
            //  /
            // 1

            int[] expected = new[] { 1, 2 };

            int index = 0;

            tree.PostOrderTraversal(item => Assert.AreEqual(expected[index++], item, "The item enumerated in the wrong order"));
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            TreeNode root = CreateTree();
            BinaryTree tree = new BinaryTree();

            tree.BFSTreeWithLevel(root);
            Console.ReadLine();
        }
Exemplo n.º 10
0
        public void Test_ContainsInTree()
        {
            var tree = new BinaryTree<int>() { 1, 5, 3, 4, 9 };

            Assert.AreEqual(tree.Contains(new BinaryTreeNode<int>(5)), true);
            Assert.AreEqual(tree.Contains(new BinaryTreeNode<int>(77)), false);

            Assert.AreEqual(tree.Contains(5), true);
            Assert.AreEqual(tree.Contains(77), false);
        }
Exemplo n.º 11
0
 public void BreadthFirstElementDoesNotExist()
 {
     BinaryTree<int> tree = new BinaryTree<int>();
     tree.Root = new BinaryTreeNode<int>(1);
     tree.Root.Left = new BinaryTreeNode<int>(2);
     tree.Root.Right = new BinaryTreeNode<int>(3);
     tree.Root.Left.Left = new BinaryTreeNode<int>(4);
     tree.Root.Left.Right = new BinaryTreeNode<int>(5);
     tree.BreadthFirstSearch(8);
 }
Exemplo n.º 12
0
 public void BreadthFirstFind()
 {
     BinaryTree<int> tree = new BinaryTree<int>();
     tree.Root = new BinaryTreeNode<int>(1);
     tree.Root.Left = new BinaryTreeNode<int>(2);
     tree.Root.Right = new BinaryTreeNode<int>(3);
     tree.Root.Left.Left = new BinaryTreeNode<int>(4);
     tree.Root.Left.Right = new BinaryTreeNode<int>(5);
     int expected = 3;
     Assert.AreEqual(expected, tree.BreadthFirstSearch(3));
 }
Exemplo n.º 13
0
        public void Test_Enumerator_OfEmptyTree()
        {
            var tree = new BinaryTree<int>();
            INode<int> nullNode = null;

            foreach (var node in tree)
            {
                nullNode = node;
            }

            Assert.IsNull(nullNode);
        }
Exemplo n.º 14
0
        public void Test_ClearingTree()
        {
            var tree = new BinaryTree<int>() {1, 5, 3, 4, 9};
            Assert.AreEqual(tree.Count, 5);

            tree.Clear();
            Assert.AreEqual(tree.Count, 0);

            var count = tree.Count();

            Assert.AreEqual(count, 0);
        }
Exemplo n.º 15
0
        public void Test_Enumerator()
        {
            var tree = new BinaryTree<int>() { new BinaryTreeNode<int>(5) };
            INode<int> test = null;
            foreach (INode<int> node in ((IEnumerable)tree))
            {
                test = node;
            }

            Assert.AreNotEqual(null, test);
            Assert.AreEqual(5, test.Data);
        }
Exemplo n.º 16
0
        static void PrintTree(BinaryTree<int> bt)
        {
            foreach (var node in bt)
            {
                var sb = new StringBuilder(".");
                for (var i = 0UL; i < node.Level; i++)
                {
                    sb.Append(".");
                }

                Console.WriteLine(sb.ToString() + node.Data);
            }
        }
Exemplo n.º 17
0
 public void AddNodesToTree()
 {
     BinaryTree<int> tree = new BinaryTree<int>();
     tree.Root = new BinaryTreeNode<int>(1);
     tree.Root.Left = new BinaryTreeNode<int>(2);
     tree.Root.Right = new BinaryTreeNode<int>(3);
     tree.Root.Left.Left = new BinaryTreeNode<int>(4);
     tree.Root.Left.Right = new BinaryTreeNode<int>(5);
     BinaryTreeNode<int>[] expected = { tree.Root.Left, tree.Root.Right };
     BinaryTreeNode<int>[] expected2 = { tree.Root.Left.Left, tree.Root.Left.Right };
     CollectionAssert.AreEqual(expected, tree.Root.Children);
     CollectionAssert.AreEqual(expected2, tree.Root.Left.Children);
 }
Exemplo n.º 18
0
 public void BreadthFirstFindBiggerTree()
 {
     BinaryTree<int> tree = new BinaryTree<int>();
     tree.Root = new BinaryTreeNode<int>(1);
     tree.Root.Left = new BinaryTreeNode<int>(2);
     tree.Root.Right = new BinaryTreeNode<int>(3);
     tree.Root.Left.Left = new BinaryTreeNode<int>(4);
     tree.Root.Left.Right = new BinaryTreeNode<int>(5);
     tree.Root.Left.Left.Left = new BinaryTreeNode<int>(6);
     tree.Root.Left.Left.Right = new BinaryTreeNode<int>(7);
     tree.Root.Left.Right.Left = new BinaryTreeNode<int>(8);
     tree.Root.Left.Right.Right = new BinaryTreeNode<int>(9);
     int expected = 9;
     Assert.AreEqual(expected, tree.BreadthFirstSearch(9));
 }
Exemplo n.º 19
0
        public void BeforEachTest()
        {
            System.Diagnostics.Debug.WriteLine("Before {0}", TestContext.CurrentContext.Test.Name);
            tree = new BinaryTree<int>();

            tree.Head = new BinaryTreeNode<int>(101, 2);
            tree.Head.Left = new BinaryTreeNode<int>(102, 7);
            tree.Head.Left.Left = new BinaryTreeNode<int>(103, 2);
            tree.Head.Left.Right = new BinaryTreeNode<int>(104, 6);
            tree.Head.Left.Right.Left = new BinaryTreeNode<int>(105, 5);
            tree.Head.Left.Right.Right = new BinaryTreeNode<int>(106, 11);

            tree.Head.Right = new BinaryTreeNode<int>(107, 5);
            tree.Head.Right.Right = new BinaryTreeNode<int>(108, 9);
            tree.Head.Right.Right.Left = new BinaryTreeNode<int>(109, 4);


        }
        internal static void Main()
        {
            var random = new Random();
            BinaryTree<int, string> tree = new BinaryTree<int, string>();

            // Включва 10 върха с произволни ключове
            for (int i = 0; i < 10; i++)
            {
                int key = (random.Next() % 20) + 1;
                Console.WriteLine("Вмъква се елемент с ключ {0}", key);
                tree.Add(key, "someinfo");
            }

            Console.WriteLine("Дърво: ");
            tree.Print();
            Console.WriteLine();

            // Претърсва за елемента с ключ 5
            TreeNode<int, string> result = tree.Search(5);
            if (result != null)
            {
                Console.WriteLine("Намерен е: {0}", result.Value);
            }

            // Изтрива произволни 10 върха от дървото
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    int key = (random.Next() % 20) + 1;
                    Console.WriteLine("Изтрива се елемента с ключ {0}", key);
                    tree.Remove(key);
                }
                catch (InvalidOperationException ioe)
                {
                    Console.WriteLine(ioe.Message);
                }
            }

            Console.WriteLine("Дърво: ");
            tree.Print();
            Console.WriteLine();
        }
Exemplo n.º 21
0
 public void AddTest(BinaryTree.iTree tr)
 {
     tr.Add(12);
     tr.Add(45);
     tr.Add(10);
     tr.Add(25);
     tr.Add(75);
     tr.Add(39);
     tr.Add(11);
     tr.Add(2);
     tr.Add(5);
     tr.Add(1);
     tr.Add(22);
     tr.Add(42);
     tr.Add(40);
     tr.Add(43);
     tr.Add(3);
     tr.Add(6);
     int expected = 16;
     NUnit.Framework.Assert.AreEqual(expected, tr.Size());
 }
Exemplo n.º 22
0
        private void btnRnd_Click(object sender, EventArgs e)
        {
            var val = int.Parse(textBox1.Text);
            var val2 = int.Parse(textBox2.Text);

            Node n1 = new Node(val);
            Node n2 = new Node(val2);

              BinaryTree i = new BinaryTree();

              Node root = i.RootNode;

             if (_tree.Exists(val) && _tree.Exists(val2))
             {

              MessageBox.Show("The parent node is " + i.LCA(root, n1, n2).Value, "Please Hire Me!");
             }
             else
             {
                MessageBox.Show("Node values are invalid please try again","Error");
             }
        }
Exemplo n.º 23
0
        public void Creation_Of_Single()
        {
            BinaryTree<int> tree = new BinaryTree<int>();

            foreach (int item in tree)
            {
                Assert.Fail("An empty tree should not enumerate any values");
            }

            Assert.IsFalse(tree.Contains(10), "Tree should not contain 10 yet");

            tree.Head = new BinaryTreeNode<int>(10);

            Assert.IsTrue(tree.Contains(10), "Tree should contain 10");

            int count = 0;
            foreach (int item in tree)
            {
                count++;
                Assert.AreEqual(1, count, "There should be exactly one item");
                Assert.AreEqual(item, 10, "The item value should be 10");
            }
        }
        public void Remove_Head()
        {
            BinaryTree<int> tree = new BinaryTree<int>();

            //        4
            //       / \
            //      2   5
            //     / \   \
            //    1   3   7
            //           / \
            //          6   8

            tree.Add(4);
            tree.Add(5);
            tree.Add(2);
            tree.Add(7);
            tree.Add(3);
            tree.Add(6);
            tree.Add(1);
            tree.Add(8);

            tree.Remove(4);

            //        5
            //       /   \
            //      2      7
            //     / \    / \
            //    1   3  6  8


            int[] expected = new[] { 1, 3, 2, 6, 8, 7, 5, };

            int index = 0;

            tree.PostOrderTraversal(item => Assert.AreEqual(expected[index++], item, "The item enumerated in the wrong order"));
        }
Exemplo n.º 25
0
        public void Test_CopyTo()
        {
            var tree = new BinaryTree<int>() { 90, 50,150, 20, 75 };
            var arr = new INode<int>[5];
            tree.CopyTo(arr,0);

            INode<int>[] expected = {
                new BinaryTreeNode<int>(90),
                new BinaryTreeNode<int>(50),
                new BinaryTreeNode<int>(150),
                new BinaryTreeNode<int>(20),
                new BinaryTreeNode<int>(75),
            };

            for (var i = 0; i < arr.Length; i++)
            {
                Assert.AreEqual(arr[i].Data, expected[i].Data);
            }

            var arr2 = new INode<int>[1];
            tree.CopyTo(arr2,0);
            Assert.AreEqual(arr2[0].Data, 90);
            Assert.AreEqual(arr2.Length, 1);
        }
Exemplo n.º 26
0
        static void Main(string[] args)
        {
            bool endConsole = false;
            int  insertNumber;
            int  deleteNumber;

            Console.WriteLine("Please enter one value at a time, whenever you are done type 'Done'");
            BinaryTree tree = new BinaryTree();

            while (true)
            {
                string values = Console.ReadLine();
                if (values == "Done")
                {
                    break;
                }
                int newValues = Convert.ToInt32(values);
                tree.insert(newValues);
                Console.WriteLine("Inorder traversal ");
                tree.inOrderTraversal(tree.returnRoot());
                tree.printCounter();
            }
            while (endConsole == false)
            {
                Console.WriteLine("Would you like to edit the tree? Reply Y or N");
                string answertwo = Console.ReadLine();
                if (answertwo == "Y")
                {
                    Console.WriteLine("Insert or Delete a value? Type either Insert or Delete depending on what you would like");
                    string answerthree = Console.ReadLine();
                    if (answerthree == "Insert")
                    {
                        Console.WriteLine("Please enter the number you would like to insert:");
                        insertNumber = Convert.ToInt32(Console.ReadLine());
                        tree.insert(insertNumber);
                        Console.WriteLine("Inorder traversal ");
                        tree.inOrderTraversal(tree.returnRoot());
                    }
                    else if (answerthree == "Delete")
                    {
                        Console.WriteLine("Please enter the number you would like to delete");
                        deleteNumber = Convert.ToInt32(Console.ReadLine());
                        tree.delete(deleteNumber);
                        Console.WriteLine("Inorder traversal");
                        tree.inOrderTraversal(tree.returnRoot());
                    }
                    else
                    {
                        Console.WriteLine("Sorry! I didn't recognise your input. Please try again!");
                        Console.WriteLine("---------------");
                        break;
                    }
                }
                else if (answertwo == "N")
                {
                    Console.WriteLine("Alright then, if you change your mind you can go again!");
                    Console.WriteLine("---------------");
                    break;
                }
                else
                {
                    Console.WriteLine("Sorry! I didn't recognise your input. Please try again!");
                    Console.WriteLine("---------------");
                    break;
                }
            }
        }
Exemplo n.º 27
0
        public void InOrder_Enumerator_String()
        {


            //         "a"
            //       /   \
            //      "c"    " d"
            //     / \    
            //    "g"   "b"
            //      



            BinaryTree<string> tree3 = new BinaryTree<string>();

            tree3.Head = new BinaryTreeNode<string>("a");
            tree3.Head.Left = new BinaryTreeNode<string>("c");
            tree3.Head.Left.Left = new BinaryTreeNode<string>("g");
            tree3.Head.Left.Right = new BinaryTreeNode<string>("b");

            tree3.Head.Right = new BinaryTreeNode<string>("d");

            string[] expected = new[] { "g", "c", "b", "a", "d" };

            int index = 0;

            foreach (string actual in tree3)
            {
                Assert.AreEqual(expected[index++], actual, "The item enumerated in the wrong order");
                System.Diagnostics.Debug.WriteLine(actual);
            }
        }
Exemplo n.º 28
0
        public void InOrder_Enumerator_Char()
        {


            //         a
            //       /   \
            //      c     d
            //     / \    
            //    g   b
            //      



            BinaryTree<char> tree2 = new BinaryTree<char>();

            tree2.Head = new BinaryTreeNode<char>('a');
            tree2.Head.Left = new BinaryTreeNode<char>('c');
            tree2.Head.Left.Left = new BinaryTreeNode<char>('g');
            tree2.Head.Left.Right = new BinaryTreeNode<char>('b');

            tree2.Head.Right = new BinaryTreeNode<char>('d');

            char[] expected = new[] { 'g', 'c', 'b', 'a', 'd' };

            int index = 0;

            foreach (int actual in tree2)
            {
                Assert.AreEqual(expected[index++], actual, "The item enumerated in the wrong order");
                System.Diagnostics.Debug.WriteLine(actual);
            }
        }
Exemplo n.º 29
0
 public BinaryTree(T value, BinaryTree <T> leftChild = null, BinaryTree <T> rightChild = null)
 {
     this.Value      = value;
     this.LeftChild  = leftChild;
     this.RightChild = rightChild;
 }
Exemplo n.º 30
0
        public void Enumerator_Of_Single()
        {
            //         2
            //       /   \
            //      
            tree = new BinaryTree<int>();

            foreach (int item in tree)
            {
                Assert.Fail("An empty tree should not enumerate any values");
            }

            Assert.IsFalse(tree.Contains(2), "Tree should not contain 2 yet");

            tree.Head = new BinaryTreeNode<int>(2);

            Assert.IsTrue(tree.Contains(2), "Tree should contain 10");

            int count = 0;
            foreach (int item in tree)
            {
                count++;
                Assert.AreEqual(1, count, "There should be exactly one item");
                Assert.AreEqual(item, 2, "The item value should be 2");
            }
        }
Exemplo n.º 31
0
        private BinaryTree <T> Contain(T x)  //此下三个函数功能为不太完美的运算符重载
        {
            BinaryTree <T> aa = new BinaryTree <T>(x);

            return(aa);
        }
Exemplo n.º 32
0
        /// <summary>
        /// Create a binary tree
        /// </summary>
        private void CreateTree()
        {
            //Init the tree
            _tree = new BinaryTree();
            lblMsg.Text = @"Random binary tree created";

            //Add a new node, to the right side of the root node
            Node currentNode = new Node(0);
            _tree.Add(currentNode, false);

            //Populate its children nodes
            PopulateLeftRightNode(currentNode, 0);

            //Draw the tree
            PaintTree();

            //Update label
            lblCount.Text = _tree.Count.ToString();
        }
Exemplo n.º 33
0
        static void Main(string[] args)
        {
            BinaryTree tree = new BinaryTree(80);

            //דוגמה
            //tree.Left = new TreeNode(10);
            //tree.Right = new TreeNode(5);
            //tree.Right.Left = new TreeNode(100);
            //BinaryTree bt = new BinaryTree(50);
            tree.Add(5);
            tree.Add(25);
            tree.Add(20);
            tree.Add(55);
            tree.Add(50);
            tree.Add(70);
            tree.PrintTree();
            Console.WriteLine("Add 75 to the tree :");
            tree.Add(75);
            tree.PrintTree();
            Console.WriteLine("************************************");
            Console.WriteLine("max: " + tree.GetMax());
            Console.WriteLine();
            Console.WriteLine("max: " + tree.GetMax2());
            Console.WriteLine("min: " + tree.GetMin());
            Console.WriteLine();
            Console.WriteLine("************************************");

            TreeNode myNode = tree.Find(0);

            if (myNode != null)
            {
                Console.WriteLine(myNode.Value + " is Found :)");
            }
            else
            {
                Console.WriteLine("Not Found!!!!!");
            }
            Console.WriteLine("************************************");
            Console.WriteLine();
            Console.Write("delete 5: \n" + tree.Delete(5));
            tree.PrintTree();
            Console.WriteLine();
            Console.Write("delete 20: \n" + tree.Delete(20));
            tree.PrintTree();
            Console.WriteLine();
            Console.Write("delete 75: \n" + tree.Delete(75));
            tree.PrintTree();
            Console.WriteLine();
            Console.Write("delete 70: \n" + tree.Delete(70));
            tree.PrintTree();
            Console.WriteLine();
            Console.Write("delete 55: \n" + tree.Delete(55));
            tree.PrintTree();

            //if head was public!!!!!
            //bt.head.Left = new TreeNode(20);
            //bt.head.Left.Left = new TreeNode(5);
            //bt.head.Left.Right = new TreeNode(25);
            //bt.head.Right = new TreeNode(70);
            //bt.PrintTree();
        }
Exemplo n.º 34
0
 private static void Print(BinaryTreeNode root)
 {
     BinaryTree.Traverse(root, n => Console.WriteLine(n.Value));
 }
Exemplo n.º 35
0
 public BinaryTree(int data, BinaryTree lesserTree, BinaryTree greaterTree)
 {
     this.Data        = data;
     this.LesserTree  = lesserTree;
     this.GreaterTree = greaterTree;
 }
        public void Remove_Missing_From_Tree()
        {
            BinaryTree<int> tree = new BinaryTree<int>();

            //         4
            //       /   \
            //      2     8 
            //     / \    /
            //    1   3  6
            //          / \
            //         5   7   

            int[] values = new[]{4, 2, 1, 3, 8, 6, 7, 5};

            foreach(int i in values)
            {
                Assert.IsFalse(tree.Contains(10), "Tree should not contain 10");
                tree.Add(i);
            }
        }
Exemplo n.º 37
0
 public void leftOrder(BinaryTree tree)
 {
     tree.preOrder(root);
 }
Exemplo n.º 38
0
        static void Main(string[] args)
        {
            const int min  = 0;
            const int max  = 100;
            var       tree = new BinaryTree <int>();
            var       rnd  = new int[10];

            Node <int> root = null;

            var rndNo = new Random();

            for (var i = 0; i < rnd.Length; i++)
            {
                rnd[i] = rndNo.Next(min, max);
            }

            foreach (var r in rnd)
            {
                tree.InsertNode(r);
            }

            Console.WriteLine("input array: ");
            Console.WriteLine(string.Join(",", rnd));

            var values = tree.PreorderTraverseTree().ToList();

            Console.WriteLine("\nthe trees preorder traversal: ");
            Console.WriteLine(string.Join(",", values));

            var vals = tree.InOrderTraverseTree().ToList();

            Console.WriteLine("\nthe trees inorder traversal: ");
            Console.WriteLine(string.Join(",", vals));

            var rndItem = rnd[rndNo.Next(0, rnd.Length)];

            Console.WriteLine("\n" + rndItem + " will be deleted from tree");
            tree.DeleteNode(rndItem);

            var restTree = tree.PreorderTraverseTree().ToList();

            Console.WriteLine("\ntree after deletion: ");
            Console.WriteLine(string.Join(",", restTree));

            var treeContent = tree.PreorderTraverseTree().ToList();
            var rndItem1    = treeContent[2];

            Console.WriteLine("\ntry to find: " + rndItem1);
            var test = tree.FindNode(rndItem1);

            Console.WriteLine("Found: " + test);

            Console.WriteLine("Balancing tree now");
            var balancedTree = tree.BalancedTree();

            var balancedPreOrder = balancedTree.PreorderTraverseTree().ToList();

            Console.WriteLine("\npreorder traversal of balanced tree: ");
            Console.WriteLine(string.Join(",", balancedPreOrder));
            Console.ReadLine();
        }