Exemple #1
0
 /// <summary>
 /// Try to add a node to the current node.
 /// </summary>
 /// <param name="node"></param>
 public void AddNode(NodeInt node)
 {
     //If the new node's value is less than the current node's value.
     if (node.value < this.value)
     {
         //If the current node dosnt have a left node.
         if (this.left == null)
         {
             //Set the child to the new node.
             this.left = node;
         }
         else
         {
             //Try to add the new node to the child.
             this.left.AddNode(node);
         }
     }
     //If the new node's value is more than the current node's value.
     else if (node.value > this.value)
     {
         //If the current node dosnt have a right node.
         if (this.right == null)
         {
             //Set the child to the new node.
             this.right = node;
         }
         else
         {
             //Try to add the new node to the child.
             this.right.AddNode(node);
         }
     }
 }
 int min(NodeInt p)
 {
     while (p.left != null)
     {
         p = p.left;
     }
     return(p.info);
 }
Exemple #3
0
        /// <summary>
        /// Search through the tree.
        /// </summary>
        /// <param name="val">The value to search for.</param>
        /// <returns>The found node.</returns>
        public NodeInt Search(int val)
        {
            //Start the search at the root.
            NodeInt found = root.Search(val);

            //Return what we found.
            return(found);
        }
 int max(NodeInt p)
 {
     while (p.right != null)
     {
         p = p.right;
     }
     return(p.info);
 }
 /*
  * Pre InOrder Of BST*
  * LNR
  */
 void diplayTreeDisplayInOrderR(NodeInt p)
 {
     if (p == null)
     {
         return;
     }
     diplayTreeDisplayInOrderR(p.left);
     Console.Write(p.info + " ");
     diplayTreeDisplayInOrderR(p.right);
 }
 internal void InsertItem(int item)
 {
     if (rootInt == null)
     {
         Console.WriteLine("Tree empty");
         rootInt = new NodeInt(item);
         return;
     }
     NodeInt temp = new NodeInt(item);
     NodeInt p    = Insert(rootInt, item);
 }
 public void createIntTree()
 {
     rootInt                   = new NodeInt(50);
     rootInt.left              = new NodeInt(40);
     rootInt.left.left         = new NodeInt(30);
     rootInt.left.right        = new NodeInt(45);
     rootInt.left.left.left    = new NodeInt(20);
     rootInt.left.right.left   = new NodeInt(42);
     rootInt.left.right.right  = new NodeInt(49);
     rootInt.right             = new NodeInt(60);
     rootInt.right.right       = new NodeInt(70);
     rootInt.right.right.left  = new NodeInt(65);
     rootInt.right.right.right = new NodeInt(80);
     Console.WriteLine("Tree Is ready");
 }
Exemple #8
0
        /// <summary>
        /// Add a value to the tree.
        /// </summary>
        /// <param name="val"></param>
        public void AddValue(int val)
        {
            //Create a new node.
            NodeInt node = new NodeInt(this, val);

            //If there isnt a root node.
            if (this.root == null)
            {
                //Set the new node as the root node.
                this.root = node;
            }
            else
            {
                //Try to add the new node to the root.
                this.root.AddNode(node);
            }
        }
        static void diplayTreeDisplayLevelOrderR(NodeInt root)
        {
            // Base Case
            if (root == null)
            {
                return;
            }

            // Create an empty queue for level
            // order tarversal
            Queue <NodeInt> q = new Queue <NodeInt>();

            // Enqueue Root and initialize height
            q.Enqueue(root);

            while (true)
            {
                // nodeCount (queue size) indicates
                // number of nodes at current level.
                int nodeCount = q.Count;
                if (nodeCount == 0)
                {
                    break;
                }

                // Dequeue all nodes of current level
                // and Enqueue all nodes of next level
                while (nodeCount > 0)
                {
                    NodeInt node = q.Peek();
                    Console.Write(node.info + " ");
                    q.Dequeue();
                    if (node.left != null)
                    {
                        q.Enqueue(node.left);
                    }
                    if (node.right != null)
                    {
                        q.Enqueue(node.right);
                    }
                    nodeCount--;
                }
                Console.WriteLine();
            }
        }
        int Height(NodeInt p)
        {
            if (p == null)
            {
                return(0);
            }
            int HL = Height(p.left);
            int HR = Height(p.right);

            if (HL > HR)
            {
                return(1 + HL);
            }
            else
            {
                return(1 + HR);
            }
        }
 NodeInt Insert(NodeInt p, int item)
 {
     if (p == null)
     {
         p = new NodeInt(item);
     }
     else if (item < p.info)
     {
         p.left = Insert(p.left, item);
     }
     else if (item > p.info)
     {
         p.right = Insert(p.right, item);
     }
     else if (p.info == item)
     {
         Console.WriteLine("Duplicate Item");
     }
     return(p);
 }
        public virtual void iterativePreorder(NodeInt NodeInt)
        {
            // Base Case
            if (NodeInt == null)
            {
                return;
            }

            // Create an empty stack and push root to it
            Stack <NodeInt> nodeStack = new Stack <NodeInt>();

            nodeStack.Push(NodeInt);

            /* Pop all items one by one. Do following
             * for every popped item
             * a) print it
             * b) push its right child
             * c) push its left child
             * Note that right child is pushed first so
             * that left is processed first */
            while (nodeStack.Count > 0)
            {
                // Pop the top item from stack and print it
                NodeInt mynode = nodeStack.Peek();
                Console.Write(mynode.info + " ");
                nodeStack.Pop();

                // Push right and left children of
                // the popped node to stack
                if (mynode.right != null)
                {
                    nodeStack.Push(mynode.right);
                }
                if (mynode.left != null)
                {
                    nodeStack.Push(mynode.left);
                }
            }
        }
        internal void InsertItemI(int x)
        {
            NodeInt p   = rootInt;
            NodeInt Par = null;

            while (p != null)
            {
                Par = p;
                if (x < p.info)
                {
                    p = p.left;
                }
                else if (x > p.info)
                {
                    p = p.right;
                }
                else
                {
                    return;
                }
            }
            NodeInt temp = new NodeInt(x);

            if (Par == null)
            {
                rootInt = temp;
            }
            else if (x < Par.info)
            {
                Par.left = temp;
            }
            else
            {
                Par.right = temp;
            }
        }
        public virtual void inorderIterative()
        {
            if (rootInt == null)
            {
                return;
            }


            Stack <NodeInt> s    = new Stack <NodeInt>();
            NodeInt         curr = rootInt;

            // traverse the tree
            while (curr != null || s.Count > 0)
            {
                /* Reach the left most Node of the
                 * curr Node */
                while (curr != null)
                {
                    /* place pointer to a tree node on
                     * the stack before traversing
                     * the node's left subtree */
                    s.Push(curr);
                    curr = curr.left;
                }

                /* Current must be NULL at this point */
                curr = s.Pop();

                Console.Write(curr.info + " ");

                /* we have visited the node and its
                 * left subtree.  Now, it's right
                 * subtree's turn */
                curr = curr.right;
            }
        }
 public BinarySearchTree()
 {
     rootchar = null;
     rootInt  = null;
 }
Exemple #16
0
 public NodeInt(int i)
 {
     info  = i;
     left  = null;
     right = null;
 }
        //Dose that thing.
        static public void IntegerSearch()
        {
            //Welcome the user.
            Console.WriteLine("Welcome to the Integer Binary Search Tree.");
            Console.WriteLine();

            //Create the Binary Tree.
            BinaryTreeInt tree = new BinaryTreeInt();

            //Create a Random number.
            Random rand = new Random();

            //Print to the console.
            Console.WriteLine("Here is 10 random numbers:");

            //Get 10 random numbers.
            for (int i = 0; i < 10; i++)
            {
                //Get the next random number.
                int num = rand.Next(1, 100);

                //Add the number to the Binary Tree.
                tree.AddValue(num);

                //Print the number to the console.
                Console.Write(num);

                //Insert the commas.
                if (i < 9)
                {
                    Console.Write(",");
                }
            }

            //Output to the console.
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Here they are sorted:");

            //Traverse the tree.
            tree.Traverse();
            tree.Print();

            Console.Write("\b \b");

            //Output to the console.
            Console.WriteLine();
            Console.WriteLine();
            Console.Write("Enter number to search for: ");

            //Get the input to search.
            string input = Console.ReadLine();
            int    number;

            //Convert to int.
            Int32.TryParse(input, out number);

            //Search the Binary Tree.
            NodeInt search = tree.Search(number);

            //New Line.
            Console.WriteLine();

            //If the returned node isnt null.
            if (search != null)
            {
                //We found it.
                Console.WriteLine("Found: " + search.value + " :) \n");
            }
            else
            {
                //Not found.
                Console.WriteLine("Not Found :( \n");
            }

            //Print to the console.
            Console.Write("Press enter to restart...");

            //Pause.
            Console.ReadLine();

            //Clear the console.
            Console.Clear();

            //Restart the Console App.
            IntegerSearch();
        }
        void DelelteItemI(NodeInt P, int x)
        {
            /*
             * Deletion of item from BST has 3 cases .
             * P -> Presnet Node
             * Par -> Parent Node
             * Ch -> Chlid
             * PS -> InOrder Successor
             * S -> Sucessor
             * Case A : If leaf node. No left and right child
             * Par.left OR Parent.Right = null
             * Case B : Only one chlid Either left or right
             * Par =P;
             * Case C : Both Child exist.
             */
            NodeInt par;

            par = null;
            while (P != null)
            {
                par = P;
                if (x < P.info)
                {
                    P = P.left;
                }
                else if (x > P.info)
                {
                    P = P.right;
                }
                else if (x == P.info)
                {
                    break;
                }
            }
            if (P == null)
            {
                Console.WriteLine("Item not exist in the tree!");
                return;
            }

            /*
             * Case C : if Both Right & Left Presnet. Check InOrder Successor Of Node P
             */
            NodeInt PS, s;

            if (P.left != null && P.right != null)
            {
                /* Find InOrderSuccessor of Node P as  'S'
                 *   Find Parent Of InOrderSuccessor of as 'PS'
                 */
                PS = P;
                s  = P.right;
                while (s.left != null)
                {
                    PS = s;
                    s  = s.left;
                }
                P.info = s.info;
                par    = PS;
                P      = s;
            }

            /* Check Case B & Case A
             * Case B : If P has Only One Chlid i.e. Right/ Left
             * Case A :If P jhs no child. P.Right & P.left both are null
             */
            NodeInt Ch;

            if (P.left != null)
            {
                Ch = P.left;
            }
            else
            {
                Ch = P.right;
            }

            /*
             * This code will do actual assignment of Child.
             */
            if (par == null)
            {
                rootInt = Ch;
            }
            else if (par.left == P)
            {
                par.left = Ch;
            }
            else
            {
                par.right = Ch;
            }
            Console.WriteLine("Deletion Is  Sucessfull");
        }
Exemple #19
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public BinaryTreeInt()
 {
     //Set the root to null.
     this.root = null;
 }