Beispiel #1
0
        public void FindInorderPredecessorAndSuccessorOfKey(BinarySearchTreeNodeSM root, int i, ref BinarySearchTreeNodeSM pred, ref BinarySearchTreeNodeSM succ)
        {
            if (root == null)
            {
                return;
            }
            if (root.Data == i)
            {
                BinarySearchTreeNodeSM dummy = root.LeftChild;
                while (dummy.RightChild != null)
                {
                    dummy = dummy.RightChild;
                }

                pred = dummy;
                if (root.RightChild != null)
                {
                    dummy = dummy.LeftChild;
                }

                succ = dummy;
                return;
            }
            if (root.Data > i)
            {
                succ = root;
                FindInorderPredecessorAndSuccessorOfKey(root.LeftChild, i, ref pred, ref succ);
            }

            if (root.Data < i)
            {
                pred = root;
                FindInorderPredecessorAndSuccessorOfKey(root.RightChild, i, ref pred, ref succ);
            }
        }
Beispiel #2
0
 internal int FindCeilSm(BinarySearchTreeNodeSM root, BinarySearchTreeNodeSM temp, int key)
 {
     if (root == null)
     {
         if (temp != null)
         {
             return(temp.Data);
         }
         else
         {
             return(-1);
         }
     }
     if (root.Data == key)
     {
         return(root.Data);
     }
     if (root.Data > key)
     {
         temp = root;
         return(FindCeilSm(root.LeftChild, temp, key));
     }
     if (root.Data < key)
     {
         return(FindCeilSm(root.RightChild, temp, key));
     }
     return(-1);
 }
Beispiel #3
0
        private BinarySearchTreeNodeSM InOrderSuccessorNode(BinarySearchTreeNodeSM node, BinarySearchTreeNodeSM successorOf)
        {
            if (successorOf.RightChild != null)
            {
                BinarySearchTreeNodeSM temp = successorOf.RightChild;
                while (temp.LeftChild != null)
                {
                    temp = temp.LeftChild;
                }

                return(temp);
            }

            BinarySearchTreeNodeSM temp2 = null;

            while (node != null)
            {
                if (node.Data < successorOf.Data)
                {
                    node = node.RightChild;
                }
                else if (node.Data > successorOf.Data)
                {
                    temp2 = node;
                    node  = node.LeftChild;
                }
                else
                {
                    break;
                }
            }

            return(temp2);
        }
Beispiel #4
0
        public int FindTheMinValue(BinarySearchTreeNodeSM root)
        {
            if (root.LeftChild == null)
            {
                return(root.Data);
            }

            return(FindTheMinValue(root.LeftChild));
        }
Beispiel #5
0
        private int MaxNodeValue(BinarySearchTreeNodeSM nodeLeftChild)
        {
            while (nodeLeftChild.RightChild != null)
            {
                nodeLeftChild = nodeLeftChild.RightChild;
            }

            return(nodeLeftChild.Data);
        }
Beispiel #6
0
 public void InOrderTransversal(BinarySearchTreeNodeSM root)
 {
     if (root == null)
     {
         return;
     }
     InOrderTransversal(root.LeftChild);
     Console.WriteLine(root.Data);
     InOrderTransversal(root.RightChild);
 }
Beispiel #7
0
        public BinarySearchTreeNodeSM InOrderSuccessor(BinarySearchTreeNodeSM node, int i)
        {
            if (node == null)
            {
                return(null);
            }
            BinarySearchTreeNodeSM successorOf   = FindTheGivenData(node, i);
            BinarySearchTreeNodeSM successorNode = InOrderSuccessorNode(node, successorOf);

            return(successorNode);
        }
Beispiel #8
0
        public void CorrectSwappedNodes(BinarySearchTreeNodeSM root)
        {
            BinarySearchTreeNodeSM first = null, prev = null, last = null, middle = null;

            CorrectSwappedNodesUtil(root, ref first, ref middle, ref last, ref prev);
            if (first != null && last != null)
            {
                swap(ref first.Data, ref last.Data);
            }
            else if (first != null && middle != null)
            {
                swap(ref first.Data, ref middle.Data);
            }
        }
Beispiel #9
0
        public bool CheckIfBstOrNot(BinarySearchTreeNodeSM node)
        {
            if (node == null)
            {
                return(true);
            }
            if (node.LeftChild != null && MaxNodeValue(node.LeftChild) > node.Data)
            {
                return(false);
            }
            if (node.RightChild != null && MinNodeValue(node.RightChild) < node.Data)
            {
                return(false);
            }

            return(CheckIfBstOrNot(node.LeftChild) && CheckIfBstOrNot(node.RightChild));
        }
Beispiel #10
0
        public BinarySearchTreeNodeSM GetLeastCommonAncestor(BinarySearchTreeNodeSM node, int a, int b)
        {
            if (node == null)
            {
                return(null);
            }
            if (node.Data >= a && node.Data <= b)
            {
                return(node);
            }

            if (node.Data > a && node.Data > b)
            {
                return(GetLeastCommonAncestor(node.LeftChild, a, b));
            }
            return(GetLeastCommonAncestor(node.RightChild, a, b));
        }
Beispiel #11
0
        public BinarySearchTreeNodeSM AddNode(BinarySearchTreeNodeSM node, int data)
        {
            if (node == null)
            {
                node = new BinarySearchTreeNodeSM(data);
                return(node);
            }

            if (node.Data < data)
            {
                node.RightChild = AddNode(node.RightChild, data);
            }
            else
            {
                node.LeftChild = AddNode(node.LeftChild, data);
            }

            return(node);
        }
Beispiel #12
0
 public BinarySearchTreeNodeSM FindTheGivenData(BinarySearchTreeNodeSM node, int d)
 {
     if (node == null)
     {
         return(null);
     }
     if (node.Data == d)
     {
         return(node);
     }
     if (node.Data > d)
     {
         return(FindTheGivenData(node.LeftChild, d));
     }
     else
     {
         return(FindTheGivenData(node.RightChild, d));
     }
 }
Beispiel #13
0
        private void CorrectSwappedNodesUtil(BinarySearchTreeNodeSM root, ref BinarySearchTreeNodeSM first,
                                             ref BinarySearchTreeNodeSM middle, ref BinarySearchTreeNodeSM last, ref BinarySearchTreeNodeSM prev)
        {
            if (root == null)
            {
                return;
            }
            CorrectSwappedNodesUtil(root.LeftChild, ref first, ref middle, ref last, ref prev);
            if (prev != null && root.Data < prev.Data)
            {
                if (first == null)
                {
                    first  = prev;
                    middle = root;
                }
                else
                {
                    last = root;
                }
            }

            prev = root;
            CorrectSwappedNodesUtil(root.RightChild, ref first, ref middle, ref last, ref prev);
        }
Beispiel #14
0
 internal void DeleteNodeWithData(BinarySearchTreeNodeSM root, int v)
 {
 }