コード例 #1
0
        public static BinaryTree CreateTreeTwo()
        {
            Node rootTwo = new Node(2);
            Node eight   = new Node(8);
            Node nine    = new Node(9);
            Node three   = new Node(3);

            rootTwo.LeftChild  = eight;
            eight.RightChild   = nine;
            rootTwo.RightChild = three;

            BinaryTree bt = new BinaryTree(rootTwo);

            return(bt);
        }
コード例 #2
0
        public static BinaryTree CreateTreeOne()
        {
            Node root  = new Node(9);
            Node eight = new Node(8);
            Node nine  = new Node(9);
            Node one   = new Node(1);

            root.LeftChild   = eight;
            eight.RightChild = nine;
            root.RightChild  = one;

            BinaryTree bt = new BinaryTree(root);

            return(bt);
        }
コード例 #3
0
        public static BinaryTree SampleTree()
        {
            Node       root = new Node(2);
            BinaryTree tree = new BinaryTree(root);
            // LeftChildChild side
            Node firstLeftChild = new Node(7);

            root.LeftChild = firstLeftChild;

            Node secondLeftChild = new Node(2);

            firstLeftChild.LeftChild = secondLeftChild;

            Node firstLeftChildRight = new Node(6);

            firstLeftChild.RightChild = firstLeftChildRight;


            Node firstLeftChildRightLeftChild = new Node(5);

            firstLeftChildRight.LeftChild = firstLeftChildRightLeftChild;

            Node firstLeftChildRightRight = new Node(11);

            firstLeftChildRight.RightChild = firstLeftChildRightRight;


            // Right side
            Node firstRight = new Node(5);

            root.RightChild = firstRight;

            Node firstRightRight = new Node(9);

            firstRight.RightChild = firstRightRight;

            Node firstRightRightLeftChild = new Node(4);

            firstRightRight.LeftChild = firstRightRightLeftChild;

            return(tree);
        }
コード例 #4
0
        /// <summary>
        /// Breadthes the first.
        /// </summary>
        /// <param name="tree">The tree.</param>
        /// <returns></returns>
        public static string BreadthFirst(BinaryTree tree)
        {
            // use a generic que and enqueue the tree root node.
            Queue <Node> queue = new Queue <Node>();

            queue.Enqueue(tree.Root);
            string result = "";

            // check to see if the queue is empty.
            while (queue.Count > 0)
            {
                Node current = queue.Dequeue();
                if (current == null)
                {
                    continue;
                }
                queue.Enqueue(current.LeftChild);
                queue.Enqueue(current.RightChild);
                result += $" {current.Data} --> ";
            }
            return(result);
        }