Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            // Using Insert
            var binaryTree = new BinaryTree(new[] { 1, 2, 3, 4, 5 });

            Console.WriteLine("[1] Breath-first:");
            binaryTree.PrintLevelOrder(binaryTree.Root);
            Console.WriteLine("\n[1] Depth-first:");
            binaryTree.PrintDepthOrder(binaryTree.Root);

            Console.WriteLine();

            // Manual
            var tree = new BinaryTree.Node(1);

            tree.Left        = new BinaryTree.Node(2);
            tree.Right       = new BinaryTree.Node(3);
            tree.Right.Left  = new BinaryTree.Node(4);
            tree.Right.Right = new BinaryTree.Node(5);

            var secondBinaryTree = new BinaryTree(tree);

            Console.WriteLine("[2] Breath-first:");
            secondBinaryTree.PrintLevelOrder(binaryTree.Root);
            Console.WriteLine("\n[2] Depth-first:");
            secondBinaryTree.PrintDepthOrder(binaryTree.Root);
        }
Exemplo n.º 2
0
        // Methods

        public void Add(int value)
        {
            if (topOfTree == null)
            {
                Node newNode = new Node(value);
                topOfTree = newNode;
                return;
            }
            Node currentNode = topOfTree; // sets the current node to the top Node
            bool nodeAdded   = false;

            do
            {
                if (value < currentNode.value)
                {
                    // Go Left
                    // if left = null then create Node
                    if (currentNode.left == null)
                    {
                        Node newNode = new Node(value);
                        currentNode.left = newNode;
                        Console.WriteLine("Left");
                        nodeAdded = true;
                    }
                    else
                    {
                        currentNode = currentNode.left;
                    }
                }
                else if (value > currentNode.value)
                {
                    // go right
                    if (currentNode.right == null)
                    {
                        Node newNode = new BinaryTree.Node(value);
                        currentNode.right = newNode;
                        Console.WriteLine("Right");
                        nodeAdded = true;
                    }
                    else
                    {
                        currentNode = currentNode.right;
                    }
                }
                else
                {
                    Console.WriteLine("Node already exists");
                }
            } while (!nodeAdded);
        }
Exemplo n.º 3
0
        public void Add(int value)
        {
            //non-recursive add
            if (top == null) //tree is empty
            {
                //Add item as the base node
                Node NewNode = new BinaryTree.Node(value);
                top = NewNode;
                return;
            }
            Node currentnode = top;
            bool added       = false;

            do
            {
                // traverse tree
                if (value < currentnode.value)
                {
                    if (currentnode.left == null)
                    {
                        Node NewNode = new Node(value);
                        currentnode.left = NewNode;
                        added            = true;
                    }
                    else
                    {
                        currentnode = currentnode.left;
                    }
                }
                if (value >= currentnode.value)
                {
                    if (currentnode.right == null)
                    {
                        Node NewNode = new Node(value);
                        currentnode.right = NewNode;
                        added             = true;
                    }
                    else
                    {
                        currentnode = currentnode.right;
                    }
                }
            } while (!added);
        }