Пример #1
0
        //find the common ancestor of x and y: Do tree find;go down the tree until you find a node where you have to choose different path- this node is common ancestor
        public int commonAncestor(treeNode node, int x, int y)
        {
            if (node == null)
            {
                return(-1);             //error
            }
            treeNode ancestor = node;

            while (node != null)
            {
                if (node.value == x || node.value == y)
                {
                    return(node.value);
                }
                if (x > node.value && y > node.value)
                {
                    node = node.right;
                }
                else if (x < node.value && y < node.value)
                {
                    node = node.left;
                }
                else
                {
                    return(node.value);
                }
            }
            return(-1);
        }
Пример #2
0
        // left is small, right is large - this is IMP
        //fix m on the way down (while node is finding the right location)
        public void InsertIntoTree(int pVal, int pHighVal, treeNode node)
        {
            treeNode tmp;

            if (node == null)
            {
                return;//error
            }
            if (pVal > node.lowValue)
            {
                //update node's mValue
                if (node.maxValue < pHighVal)
                {
                    node.maxValue = pHighVal;
                }

                if (node.right == null)
                {
                    tmp           = new treeNode();
                    tmp.lowValue  = pVal;
                    tmp.highValue = pHighVal;
                    tmp.maxValue  = pHighVal;
                    tmp.left      = null;
                    tmp.right     = null;
                    node.right    = tmp;
                }
                else
                {
                    InsertIntoTree(pVal, pHighVal, node.right);
                }
            }
            else if (pVal <= node.lowValue)
            {
                //update node's mValue
                if (node.maxValue < pHighVal)
                {
                    node.maxValue = pHighVal;
                }

                if (node.left == null)
                {
                    tmp           = new treeNode();
                    tmp.lowValue  = pVal;
                    tmp.highValue = pHighVal;
                    tmp.maxValue  = pHighVal;
                    tmp.left      = null;
                    tmp.right     = null;
                    node.left     = tmp;
                }
                else
                {
                    InsertIntoTree(pVal, pHighVal, node.left);
                }
            }
            else
            {
                Console.WriteLine("Issue with insert {0} {1}", pVal, pHighVal);
                return; //error does not take care of situation of duplicate lowValues
            }
        }
Пример #3
0
 //check whether current node overlaps and add to output
 public void CheckOverlap(treeNode node, int pLowVal, int pHighVal)
 {
     if (node.lowValue > pLowVal && node.lowValue < pHighVal || node.highValue > pLowVal && node.highValue < pHighVal)
     {
         Console.WriteLine("Overlap found {0} {1}", node.lowValue, node.highValue);
     }
     //TODO take care of = ; if they start or end at the same time there need not be an overlap, so need to take care of this.
 }
Пример #4
0
 public void InOrderSearch(treeNode pNode)
 {
     if (pNode == null)
     {
         return;
     }
     InOrderSearch(pNode.left);
     Console.WriteLine(pNode.value);
     InOrderSearch(pNode.right);
 }
Пример #5
0
 public void InOrderSearch(treeNode pNode)
 {
     if (pNode == null)
     {
         return;
     }
     InOrderSearch(pNode.left);
     Console.WriteLine("Low Value {0} High Value {1}", pNode.lowValue, pNode.highValue);
     InOrderSearch(pNode.right);
 }
Пример #6
0
 //Assuming updates to the child's (being promoted) left or right  is done after calling this function
 public void UpdateParent(treeNode parentNode, treeNode node, bool isLeftParent, bool isLeftChild)
 {
     if (parentNode == null)
     {
         return;
     }
     if (node == null)
     {
         Console.WriteLine(" ERROR!! Null node in Update Parent. No Delete Operation done! ");
         return;
     }
     if (isLeftParent)
     {
         if (isLeftChild)
         {
             if (node.left != null)
             {
                 node.left.right = node.right;                  //TODO do the same for all cases
             }
             parentNode.left = node.left;
         }
         else
         {
             if (node.right != null)
             {
                 node.right.left = node.left;
             }
             parentNode.left = node.right;
         }
     }
     else //isLeftParent==false
     if (isLeftChild)
     {
         if (node.left != null)
         {
             node.left.right = node.right;
         }
         parentNode.right = node.left;
     }
     else
     {
         if (node.right != null)
         {
             node.right.left = node.left;
         }
         parentNode.right = node.right;
     }
     //free node
     node = null;
 }
Пример #7
0
 //Hw3Given sorted binary tree and 2 INT return total number of nodes in graph between teh 2 INTs
 public void SearchForINTRange(treeNode pNode, int start, int end)
 {
     //Inorder search finds elements in order, starting with max element; so modify Inorder
     if (pNode == null)
     {
         return;
     }
     SearchForINTRange(pNode.left, start, end);
     if (pNode.value >= start && pNode.value <= end)
     {
         Console.WriteLine(pNode.value);
     }
     SearchForINTRange(pNode.right, start, end);
 }
Пример #8
0
        public treeNode FindParentOfInorderSucc(treeNode node)
        {
            //Find parent of Inorder successor instead of Inorder succ since we want to deal with cases where inorder succ has a right child
            //Also we know that inorder succ is left child of parent.
            if (node == null || node.left == null)
            {
                return(node);                                  // this should never happen
            }
            treeNode inOrderSuccParent = node;

            while (inOrderSuccParent.left != null && inOrderSuccParent.left.left != null)
            {
                inOrderSuccParent = inOrderSuccParent.left;
            }
            return(inOrderSuccParent);
        }
Пример #9
0
 //This function prints out all intervals that overlaps pLowVal and pHighVal
 public void IntervalSearch(treeNode node, int pLowVal, int pHighVal)
 {
     if (node == null)
     {
         return;
     }
     CheckOverlap(node, pLowVal, pHighVal); //check overlap with current node)
     //go left if plowVal<node.lowVal || node.left.m>plowVal - the second condition exist because the high value could exist in left; added= afterwards for duplicate support
     if (node.left != null && (pLowVal <= node.lowValue || node.left.maxValue >= pLowVal))
     {
         IntervalSearch(node.left, pLowVal, pHighVal);
     }
     //go right if plowVal>node.lowVal;added= afterwards for duplicate support
     if (node.right != null && pLowVal >= node.lowValue)
     {
         IntervalSearch(node.right, pLowVal, pHighVal);
     }
 }
Пример #10
0
        // left is small, right is large - this is IMP
        public void InsertIntoTree(int pVal, treeNode node)
        {
            treeNode tmp;

            if (node == null)
            {
                return;              //error
            }
            if (pVal > node.value)
            {
                if (node.right == null)
                {
                    tmp        = new treeNode();
                    tmp.value  = pVal;
                    tmp.left   = null;
                    tmp.right  = null;
                    node.right = tmp;
                }
                else
                {
                    InsertIntoTree(pVal, node.right);
                }
            }
            else if (pVal < node.value)
            {
                if (node.left == null)
                {
                    tmp       = new treeNode();
                    tmp.value = pVal;
                    tmp.left  = null;
                    tmp.right = null;
                    node.left = tmp;
                }
                else
                {
                    InsertIntoTree(pVal, node.left);
                }
            }
            else
            {
                return; //value already exist; assume no duplicates
            }
        }
Пример #11
0
 public void BuildTree(int[] pVal)
 {
     //build binary search tree
     if (pVal == null || pVal.Length == 0)
     {
         root = null;
     }
     foreach (int i in pVal)
     {
         if (root == null)
         {
             root       = new treeNode();
             root.value = i;
             root.left  = null;
             root.right = null;
         }
         else
         {
             InsertIntoTree(i, root);
         }
     }
 }
Пример #12
0
 public void BuildTree(int[] pVal, int[] pHighVal)
 {
     //build binary search tree
     if (pVal == null || pVal.Length == 0)
     {
         root = null;
     }
     for (int i = 0; i < pVal.Count(); i++)
     {
         if (root == null)
         {
             root           = new treeNode();
             root.lowValue  = pVal[i];
             root.highValue = pHighVal[i];
             root.left      = null;
             root.right     = null;
         }
         else
         {
             InsertIntoTree(pVal[i], pHighVal[i], root);
         }
     }
 }
Пример #13
0
        public void DeleteFromTree(int pVal, treeNode node, treeNode parentNode, bool isLeft)
        {
            //Find the node
            //case 1: no right child: replace the pointer leading to p by p's left child, if it has one, or by a null pointer, if not.
            //case 2: p's right child has no left child: Replace p with right child r
            //case 3: p's right child has a left child: Replace p with inorder successor (node with next biggest number=smallest value in p's right sub tree).
            //We can easily detach inorder successor s from its position:Since s has the smallest value in p's right subtree, s cannot have a left child. Because s doesn't have a left child, we can simply replace it by its right child, if any. This is the mirror image of case 1
            if (node == null)
            {
                return;               //error
            }
            if (pVal < node.value)
            {
                if (node.left != null)
                {
                    DeleteFromTree(pVal, node.left, node, true);
                }
            }
            else if (pVal > node.value)
            {
                if (node.right != null)
                {
                    DeleteFromTree(pVal, node.right, node, false);
                }
            }
            else
            {
                //found node
                if (node.right == null)
                {
                    //case1 replace the pointer leading to p by p's left child
                    UpdateParent(parentNode, node, isLeft, true);
                }
                else if (node.right.left == null)
                {
                    //case2 replace the pointer leading to p with right child r
                    UpdateParent(parentNode, node, isLeft, false);
                }
                else
                {
                    //case3 Replace the pointer leading to p with inorder successor (node with next biggest number=smallest value in p's right sub tree).
                    treeNode inOrderSuccParent = FindParentOfInorderSucc(node.right);
                    if (inOrderSuccParent == null || inOrderSuccParent.left == null)
                    {
                        //this should never happen!
                        Console.WriteLine("Error! No inorder successor found. Delete Operation failed");
                        return;
                    }
                    //save inordersuccessor Right
                    treeNode inOrderSuccRight = inOrderSuccParent.left.right;
                    if (isLeft)
                    {
                        parentNode.left       = inOrderSuccParent.left;
                        parentNode.left.right = node.right;
                    }
                    else
                    {
                        parentNode.right       = inOrderSuccParent.left;
                        parentNode.right.right = node.right;
                    }
                    //inOrderSucc cannot have a left child; so update left child
                    inOrderSuccParent.left.left = node.left;

                    //IMP: put the right child as the left child of InorderSuc's parent for cases where there is a right child for inorder succ
                    inOrderSuccParent.left = inOrderSuccRight;
                    //free node
                    node = null;
                }
            }
        }
Пример #14
0
 public Tree()
 {
     root = null;
 }