예제 #1
0
 public BinarySearchTree()
 {
     this.head = null;
 }
예제 #2
0
 public BinaryNode(BinaryNode left, BinaryNode right, int value)
 {
     this.left  = left;
     this.right = right;
     this.val   = value;
 }
예제 #3
0
        /// <summary>
        /// Delete the specified val.
        /// </summary>
        /// <returns>The delete.</returns>
        /// <param name="val">Value.</param>
        public void Delete(int val)
        {
            if (head == null)
            {
                Console.WriteLine("The root is null dawg");
                return;
            }

            BinaryNode theParent = null, theIter = head;
            bool       isLeft = false;

            /*
             * Finds the node to be deleted
             */
            while (theIter != null && theIter.Value != val)
            {
                theParent = theIter;

                if (theIter.Value > val)
                {
                    theIter = theIter.Left;
                    isLeft  = true;
                }
                else
                {
                    theIter = theIter.Right;
                    isLeft  = false;
                }

                if (theIter == null)
                {
                    Console.WriteLine("Your value ain't here");
                    return;
                }
            }

            /*
             * The following readjusts the pointers
             */

            //CASE 1: IF THE ITER HAS NO RIGHT CHILD, THEN THE ITER'S LEFT CHILD BECOMES POINTED AT BY THE PARENT

            if (theIter.Right == null)
            {
                if (theParent == null)
                {
                    head = theIter.Left;
                }
                else
                {
                    if (isLeft)
                    {
                        theParent.Left = theIter.Left;
                    }
                    else
                    {
                        theParent.Right = theIter.Left;
                    }
                }
                //CASE 2: If iter's right child has no left child, then iter's right child replaces iter in the tree
            }
            else if (theIter.Right.Left == null)
            {
                theIter.Right.Left = theIter.Left;

                if (theParent == null)
                {
                    head = theIter.Right;
                }
                else
                {
                    if (isLeft)
                    {
                        theParent.Left = theIter.Right;
                    }
                    else
                    {
                        theParent.Right = theIter.Right;
                    }
                }
                //CASE 3: If current's right child has a left child, replace current with current's right child's left-most descendent
            }
            else
            {
                BinaryNode leftMost = theIter.Right.Left, lmParent = theIter.Right;

                while (leftMost.Left != null)
                {
                    lmParent = leftMost;
                    leftMost = leftMost.Left;
                }

                lmParent.Left  = leftMost.Right;
                leftMost.Left  = theIter.Left;
                leftMost.Right = theIter.Right;

                if (theParent == null)
                {
                    head = leftMost;
                }
                else
                {
                    if (isLeft)
                    {
                        theParent.Left = leftMost;
                    }
                    else
                    {
                        theParent.Right = leftMost;
                    }
                }
            }
        }
예제 #4
0
 public BinaryNode(int value)
 {
     this.val   = value;
     this.left  = null;
     this.right = null;
 }