Exemple #1
0
        static void Main(string[] args)
        {
            var tree = new BinarySearchTree();

            tree.Add(100, 20, 500, 10, 30, 40);

            Console.WriteLine("InOrder:");
            tree.InOrder(x => { Console.Write($"{x}, "); });
            Console.WriteLine("\nPreOrder:");
            tree.PreOrder(x => { Console.Write($"{x}, "); });
            Console.WriteLine("\nPostOrder:");
            tree.PostOrder(x => { Console.Write($"{x}, "); });
        }
Exemple #2
0
        /// <summary>
        /// Makes a Binary Search Tree and prints it to the console. Searches for the node with a value 60.
        /// </summary>
        static void MakeBSTree()
        {
            BinarySearchTree bst     = new BinarySearchTree();
            Node             node100 = new Node(100);

            bst.Add(node100, new Node(50));
            bst.Add(node100, new Node(200));
            bst.Add(node100, new Node(25));
            bst.Add(node100, new Node(75));
            bst.Add(node100, new Node(150));
            bst.Add(node100, new Node(300));
            bst.Add(node100, new Node(60));
            bst.Add(node100, new Node(90));

            //bst.PreOrder(node100);
            bst.InOrder(node100);
            //bst.PostOrder(node100);
            //bst.BreadthFirst(node100);
            Console.WriteLine("Find a node and print it out");
            Console.WriteLine(bst.Search(node100, 60).Value);
        }
        public static void TreeExample()
        {
            Node node7 = new Node(null, null, 70);
            Node node6 = new Node(null, null, 60);
            Node node5 = new Node(null, null, 50);
            Node node4 = new Node(null, null, 40);
            Node node3 = new Node(node6, node7, 30);
            Node node2 = new Node(node4, node5, 20);
            Node node1 = new Node(node2, node3, 10);

            BinaryTree BT1 = new BinaryTree(node1);

            BT1.nodes = new List <Node>();
            BinaryTree BT2 = new BinaryTree(node1);

            BT2.nodes = new List <Node>();
            BinaryTree BT3 = new BinaryTree(node1);

            BT3.nodes = new List <Node>();

            List <Node> preOrder  = BT1.PreOrder(node1);
            List <Node> inOrder   = BT2.InOrder(node1);
            List <Node> postOrder = BT3.PostOrder(node1);

            Console.WriteLine("PreOrder search results");

            foreach (Node item in preOrder)
            {
                Console.WriteLine($"{item.Value}");
            }

            BT1.nodes.Clear();

            Console.WriteLine("===========================");

            Console.WriteLine("InOrder search results");

            foreach (Node item in inOrder)
            {
                Console.WriteLine($"{item.Value}");
            }

            BT2.nodes.Clear();

            Console.WriteLine("===========================");

            Console.WriteLine("PostOrder search results");

            foreach (Node item in postOrder)
            {
                Console.WriteLine($"{item.Value}");
            }

            BT3.nodes.Clear();

            Console.WriteLine("===========================");

            Node nodeSeven = new Node(null, null, 70);
            Node nodeSix   = new Node(null, null, 50);
            Node nodeFive  = new Node(null, null, 30);
            Node nodeFour  = new Node(null, null, 10);
            Node nodeThree = new Node(nodeSix, nodeSeven, 60);
            Node nodeTwo   = new Node(nodeFour, nodeFive, 20);
            Node nodeOne   = new Node(nodeTwo, nodeThree, 40);

            BinarySearchTree BTS = new BinarySearchTree(nodeOne);
            BinaryTree       BT  = new BinaryTree(nodeOne);

            BT.nodes = new List <Node>();

            List <Node> search = BT.InOrder(nodeOne);

            Console.WriteLine("BinarySearchTree results");

            foreach (Node item in search)
            {
                Console.WriteLine($"{item.Value}");
            }

            BT.nodes.Clear();

            Console.WriteLine("===========================");

            Console.WriteLine("Binary Search: searching for 50");

            Console.WriteLine($"Search result: {BTS.Search(50, nodeOne).Value}");

            Console.WriteLine("===========================");

            Console.WriteLine("Adding to Binary Search Tree");

            Node nodeEight = new Node(null, null, 25);

            Console.WriteLine($"Node to add: {nodeEight.Value}");

            BTS.Add(nodeOne, nodeEight);

            List <Node> add = BT.InOrder(nodeOne);

            foreach (Node item in add)
            {
                Console.WriteLine($"{item.Value}");
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            BinarySearchTree test = new BinarySearchTree();

            test.Add(5);
            test.Add(4);
            test.Add(3);
            Console.WriteLine($"Test one two three {test.Root.Left.Data}");

            Node find = test.Search(4);

            Console.WriteLine($"hey {find.Left.Data} and {test.Root.Data}");

            Node Root = new Node(2);

            Root.Left        = new Node(3);
            Root.Right       = new Node(4);
            Root.Left.Left   = new Node(5);
            Root.Left.Right  = new Node(6);
            Root.Right.Left  = new Node(7);
            Root.Right.Right = new Node(8);

            List <int> post = BinaryTree.PostOrder(Root);

            Console.WriteLine();

            foreach (var item in post)
            {
                Console.Write($"{item} ");
            }

            List <int> inb = BinaryTree.InOrder(Root);

            foreach (var item in inb)
            {
                Console.Write($"{item} ");
            }


            Console.WriteLine();

            List <int> pre = BinaryTree.PreOrder(Root);


            foreach (var item in pre)
            {
                Console.Write($"{item} ");
            }


            Console.WriteLine();


            Node       okay = null;
            List <int> po   = BinaryTree.PreOrder(okay);

            foreach (var item in po)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            BinarySearchTree tree = new BinarySearchTree();


            tree.Add(34, tree.Root);
            tree.Add(3, tree.Root);
            tree.Add(32, tree.Root);
            tree.Add(14, tree.Root);
            tree.Add(27, tree.Root);
            tree.Add(67, tree.Root);
            tree.Add(234, tree.Root);

            Console.WriteLine("Binary Search Tree Add: 34, 3, 67");
            Console.WriteLine();
            Console.WriteLine("Root.Value:");
            Console.WriteLine(tree.Root.Value);
            Console.WriteLine("RightChild.Value");
            Console.WriteLine(tree.Root.RightChild.Value);
            Console.WriteLine("LeftChild.Value:");
            Console.WriteLine(tree.Root.LeftChild.Value);


            Console.WriteLine("Contins: 3");
            bool truth = tree.Contains(3, tree.Root);

            Console.WriteLine(truth);

            Console.WriteLine("Binary Tree: PreOrder");
            BinaryTree <int> bT    = new BinaryTree <int>();
            Node <int>       node1 = new Node <int>()
            {
                Value = 1
            };
            Node <int> node2 = new Node <int>()
            {
                Value = 2
            };
            Node <int> node3 = new Node <int>()
            {
                Value = 3
            };
            Node <int> node4 = new Node <int>()
            {
                Value = 4
            };
            Node <int> node5 = new Node <int>()
            {
                Value = 5
            };
            Node <int> node6 = new Node <int>()
            {
                Value = 6
            };
            Node <int> node7 = new Node <int>()
            {
                Value = 7
            };

            bT.Root                       = node1;
            bT.Root.LeftChild             = node2;
            bT.Root.RightChild            = node3;
            bT.Root.LeftChild.LeftChild   = node4;
            bT.Root.LeftChild.RightChild  = node5;
            bT.Root.RightChild.LeftChild  = node6;
            bT.Root.RightChild.RightChild = node7;

            List <int> pre     = new List <int>();
            List <int> inorder = new List <int>();
            List <int> post    = new List <int>();

            bT.PreOrder(bT.Root, pre);
            bT.InOrder(bT.Root, inorder);
            bT.PostOrder(bT.Root, post);


            pre.ForEach(i => Console.Write("{0} ", i));
            Console.WriteLine();
            Console.WriteLine("Binary Tree: InOrder");
            inorder.ForEach(i => Console.Write("{0} ", i));
            Console.WriteLine();
            Console.WriteLine("Binary Tree: PostOrder");
            post.ForEach(i => Console.Write("{0} ", i));
        }
Exemple #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Binary Tree!");

            BinaryTree myTree = new BinaryTree(1);

            myTree.root.Left       = new Node(2);
            myTree.root.Right      = new Node(3);
            myTree.root.Left.Left  = new Node(4);
            myTree.root.Left.Right = new Node(5);

            List <int> PreOrderList  = new List <int>();
            List <int> InOrderList   = new List <int>();
            List <int> PostOrderList = new List <int>();

            myTree.PreOrder(myTree.root, PreOrderList);

            /// <summary>
            /// Pre-order tree traversal
            /// </summary>
            foreach (int val in PreOrderList)
            {
                Console.Write($"{val} ");
            }
            Console.WriteLine("Print PreOrder data traversal  values");

            /// <summary>
            /// In-order tree traversal
            /// </summary>
            myTree.InOrder(myTree.root, InOrderList);
            foreach (int val in InOrderList)
            {
                Console.Write($"{val} ");
            }
            Console.WriteLine("Print InOrder data traversal  values");

            /// <summary>
            /// Post-order tree traversal
            /// </summary>
            myTree.PostOrder(myTree.root, PostOrderList);
            foreach (int val in PostOrderList)
            {
                Console.Write($"{val} ");
            }
            Console.WriteLine("Print PostOrder data traversal values");

            Console.WriteLine();
            int[] bstValues = { 1, 2, 3, 4, 5 };

            /// <summary>
            /// Print values of binary search tree
            /// </summary>
            BinarySearchTree BST = new BinarySearchTree();

            foreach (int num in bstValues)
            {
                BST.root = BST.Add(BST.root, num);
            }

            List <int> bstInOrder = new List <int>();

            BST.InOrder(BST.root, bstInOrder);

            foreach (int num in bstInOrder)
            {
                Console.Write($"{num}");
            }
            Console.WriteLine(" Binary Search Tree InOrder data traversal values");
        }