Ejemplo n.º 1
0
 // private find, if current node is null return false,if current node is item return true
 // if current node is greater than item call find on left subtree, if lesser call find on right subtree
 private bool Find(T item, BinarySearchTreeNode <T> root)
 {
     if (root == null)
     {
         return(false);
     }
     else if (item.CompareTo(root.value) == 0)
     {
         return(true);
     }
     else if (item.CompareTo(root.value) < 0)
     {
         return(Find(item, root.left));
     }
     else
     {
         return(Find(item, root.right));
     }
 }
Ejemplo n.º 2
0
 // Deletes given node
 private BinarySearchTreeNode <T> DeleteNode(BinarySearchTreeNode <T> root)
 {
     // if node has no children simply delete
     if (root.left == null && root.right == null)
     {
         return(null);
     }
     // if node has only one child replace it with child
     if (root.right == null)
     {
         root.left.parent = root.parent;
         return(root.left);
     }
     if (root.left == null)
     {
         root.right.parent = root.parent;
         return(root.right);
     }
     // if node has 2 children replace with next smallest node
     return(ReplaceNode(root));
 }
Ejemplo n.º 3
0
        // in order traversal, returns a string containing all elements
        // implemented making use of the parent nodes to test their functionality
        public string ParentTraverse()
        {
            BinarySearchTreeNode <T> current = root;
            BinarySearchTreeNode <T> previous;
            string output = "";

            if (current == null)
            {
                return(output);
            }
            // loops until returning to root from the right or from left if there is no right branch
            do
            {
                // moves to furthest left child of current and adds its value to output, sets previous to current
                while (current.left != null)
                {
                    current = current.left;
                }
                previous = current;
                output  += current.value + " ";

                // traverses parent nodes until reaching a node with an unvisited right node or reaching the root
                while ((current.right == null || current.right == previous) && current.parent != null)
                {
                    // sets previous to current then sets current to parent and adds value to output if previous was not on right side
                    previous = current;
                    current  = current.parent;
                    if (current.right != previous)
                    {
                        output += current.value + " ";
                    }
                }
                // if there is an unvisited branch to the right, moves current to right
                if (current.right != previous && current.right != null)
                {
                    current = current.right;
                }
            } while (current.parent != null);
            return(output);
        }
Ejemplo n.º 4
0
 //private insert, takes a node and item and attempts to insert at that node
 // if node is not empty recurses on left/right node if item is smaller/larger than item at current position
 private BinarySearchTreeNode <T> Insert(BinarySearchTreeNode <T> root, T item)
 {
     if (root == null)
     {
         root       = new BinarySearchTreeNode <T>();
         root.value = item;
     }
     // insertion logic, if the value (v )is < root, insert to the root.left
     // otherwise it's >=, so insert to the right
     // connects node to parent after
     else if (item.CompareTo(root.value) < 0)
     {
         root.left        = Insert(root.left, item);
         root.left.parent = root;
     }
     else
     {
         root.right        = Insert(root.right, item);
         root.right.parent = root;
     }
     return(root);
 }
Ejemplo n.º 5
0
 // private delete, finds if item is in tree and if so deletes it
 private BinarySearchTreeNode <T> Delete(T item, BinarySearchTreeNode <T> root)
 {
     // if current node is null end search
     if (root != null)
     {
         // if current node is item, delete it
         if (item.CompareTo(root.value) == 0)
         {
             return(DeleteNode(root));
         }
         // otherwise attempt to delete from left/right subtree if item is lesser/greater than current node
         if (item.CompareTo(root.value) < 0)
         {
             root.left = Delete(item, root.left);
         }
         else
         {
             root.right = Delete(item, root.right);
         }
     }
     return(root);
 }
Ejemplo n.º 6
0
 // public insert, takes an item and calls private insert on item at root
 public void Insert(T item)
 {
     root = Insert(root, item);
 }
Ejemplo n.º 7
0
 // public delete, calls private delete on given item
 public void Delete(T item)
 {
     root = Delete(item, root);
 }