Example #1
0
        //first level, then second level, etc
        public void breadthFirst()
        {
            Queue <Node <int> > queue = new Queue <Node <int> >();

            if (root == null)
            {
                return;
            }
            queue.Enqueue(root);

            //use a queue to list the nodes breadth first
            while (queue.Count != 0)
            {
                Node <int> n = queue.Dequeue();
                Console.Write(n.getData() + " ");
                //if we have left node, add it to the end of the queue
                if (n.getLeft() != null)
                {
                    queue.Enqueue(n.getLeft());
                }
                //if we have right node, add it to the end of the queue
                if (n.getRight() != null)
                {
                    queue.Enqueue(n.getRight());
                }
            }
        }
Example #2
0
        //get number of nodes
        public int getNumberOfNodes()
        {
            Queue <Node <int> > queue = new Queue <Node <int> >();
            int numNodes = 0;

            if (root == null)
            {
                return(numNodes);
            }
            queue.Enqueue(root);

            //use a queue to list the nodes breadth first
            while (queue.Count != 0)
            {
                Node <int> n = queue.Dequeue();
                numNodes++;
                //if we have left node, add it to the end of the queue
                if (n.getLeft() != null)
                {
                    queue.Enqueue(n.getLeft());
                }
                //if we have right node, add it to the end of the queue
                if (n.getRight() != null)
                {
                    queue.Enqueue(n.getRight());
                }
            }

            return(numNodes);
        }
Example #3
0
 public void inOrder(Node <T> root)
 {
     if (root != null)
     {
         inOrder(root.getLeft());
         order.addLast(root);
         inOrder(root.getRight());
     }
 }
Example #4
0
 public void preOrder(Node <T> root)
 {
     if (root != null)
     {
         Console.Write(root.getElement() + "->");
         preOrder(root.getLeft());
         preOrder(root.getRight());
     }
 }
Example #5
0
 public int treeDepth(Node <T> root)
 {
     if (root != null)
     {
         return(0);
     }
     else
     {
         int leftDepth  = treeDepth(root.getLeft());
         int rightDepth = treeDepth(root.getRight());
         return(Math.Max(leftDepth, rightDepth));
     }
 }
Example #6
0
        public int numberOfChildren(Node <T> root)
        {
            int count = 0;

            if (root.getLeft() != null)
            {
                count++;
            }
            if (root.getRight() != null)
            {
                count++;
            }
            return(count);
        }
Example #7
0
 public Node <int> searchRecursive(Node <int> root, int key)
 {
     //base case is root null or key found
     if (root == null || root.getData().Equals(key))
     {
         return(root);
     }
     //if key less than root key, traverse left subtree
     if (key < root.getData())
     {
         return(searchRecursive(root.getLeft(), key));
     }
     //if key greater than root key, traverse right subtree
     return(searchRecursive(root.getRight(), key));
 }
Example #8
0
        public void printPostOrder(Node <int> root)
        {
            if (root == null)
            {
                return;
            }

            //traverse left subtree recursively
            printPostOrder(root.getLeft());

            //traverse right subtree recursively
            printPostOrder(root.getRight());

            //root node
            Console.Write(root.getData() + " ");
        }
Example #9
0
        public Node <int> insertRecursive(Node <int> root, Node <int> parent, int key)
        {
            if (root == null)
            {
                return(new Node <int>(parent, key));
            }

            if (key < root.getData())
            {
                root.setLeft(insertRecursive(root.getLeft(), root, key));
            }
            else if (key > root.getData())
            {
                root.setRight(insertRecursive(root.getRight(), root, key));
            }
            return(root);
        }
Example #10
0
        public Node <int> deleteRecursive(Node <int> root, int key)
        {
            if (root == null)
            {
                return(root);
            }
            //if key less than root key, traverse left subtree
            if (key < root.getData())
            {
                root.setLeft(deleteRecursive(root.getLeft(), key));
            }
            //if key greater than root key, traverse right subtree
            else if (key > root.getData())
            {
                root.setRight(deleteRecursive(root.getRight(), key));
            }
            //found node to delete
            else
            {
                //node to delete has zero or one child node
                if (root.getLeft() == null)
                {
                    if (root.getRight() != null)
                    {
                        root.getRight().setParent(root.getParent());
                    }
                    return(root.getRight());
                }
                else if (root.getRight() == null)
                {
                    if (root.getLeft() != null)
                    {
                        root.getLeft().setParent(root.getParent());
                    }
                    return(root.getLeft());
                }

                //node to delete has two children, so find the smallest node on the right subtree
                //replace the node to delete with that node, then delete the smallest node on the right subtree
                root.setData(getMinValue(root.getRight()));
                root.setRight(deleteRecursive(root.getRight(), root.getData()));
            }

            return(root);
        }
Example #11
0
 public bool elementExists(Node <T> root, T element)
 {
     if (root == null)
     {
         return(false);
     }
     else if (comp.Compare(element, root.getElement()) == 0)
     {
         return(true);
     }
     else
     {
         if (comp.Compare(element, root.getElement()) < 0)
         {
             return(elementExists(root.getLeft(), element));
         }
         else
         {
             return(elementExists(root.getRight(), element));
         }
     }
 }
Example #12
0
        public int getHeightRecursive(Node <int> root)
        {
            if (root == null)
            {
                return(0);
            }
            else
            {
                //get height of subtree
                int leftHeight  = getHeightRecursive(root.getLeft());
                int rightHeight = getHeightRecursive(root.getRight());

                //return larger of the two heights
                if (leftHeight > rightHeight)
                {
                    return(leftHeight + 1);
                }
                else
                {
                    return(rightHeight + 1);
                }
            }
        }
Example #13
0
 private void addElement(Node <T> root, T element)
 {
     if (this.root == null)
     {
         this.root = new Node <T>(element, null, null, null);
         numberOfNodes++;
     }
     else
     {
         if (comp.Compare(element, root.getElement()) < 0)//x es menor que y
         {
             if (root.getLeft() == null)
             {
                 root.setLeft(new Node <T>(element, root, null, null));
                 numberOfNodes++;
             }
             else
             {
                 addElement(root.getLeft(), element);
             }
         }
         else if (comp.Compare(element, root.getElement()) > 0)//x es mayor que y
         {
             if (root.getRight() == null)
             {
                 root.setRight(new Node <T>(element, root, null, null));
                 numberOfNodes++;
             }
             else
             {
                 addElement(root.getRight(), element);
             }
         }
         else//x es igual que y
         {
             throw new Exception("NO SE PERMITEN DUPLICADOS");
         }
     }
 }
Example #14
0
        public T remove(Node <T> root, T element)
        {
            if (root == null)
            {
                return(default(T));
            }
            else if (comp.Compare(element, root.getElement()) == 0)
            {
                if (numberOfChildren(root) == 0)
                {
                    T aux = root.getElement();
                    if (root == this.root)
                    {
                        this.root = null;
                    }
                    else
                    {
                        if (comp.Compare(element, root.getParent().getLeft().getElement()) == 0)
                        {
                            root.getParent().setLeft(null);
                            root = null;
                        }
                        else
                        {
                            root.getParent().setRight(null);
                            root = null;
                        }
                    }
                    numberOfNodes--;
                    return(aux);
                }
                else if (numberOfChildren(root) == 1)
                {
                    T aux = root.getElement();
                    if (root == this.root)
                    {
                        if (root.getLeft() != null)
                        {
                            this.root = root.getLeft();
                        }
                        else
                        {
                            this.root = root.getRight();
                        }
                    }
                    else
                    {
                        if (root.getParent().getLeft() != null)
                        {
                            if (root.getLeft() != null)
                            {
                                root.getParent().setLeft(root.getLeft());
                            }
                            else
                            {
                                root.getParent().setLeft(root.getRight());
                            }
                        }
                        else
                        {
                            if (root.getLeft() != null)
                            {
                                root.getParent().setRight(root.getLeft());
                            }
                            else
                            {
                                root.getParent().setRight(root.getRight());
                            }
                        }
                    }
                    numberOfNodes--;
                    return(aux);
                }
                else//El que sustituirá será el más derecho de los izquierdos
                {
                    Node <T> next = root.getLeft();
                    T        aux  = root.getElement();
                    if (next.getRight() != null)
                    {
                        while (next.getRight() != null)
                        {
                            next = next.getRight();
                        }
                        root.setElement(next.getElement());
                        Node <T> father = next.getParent();
                        father.setRight(null);
                    }
                    else
                    {
                        root.setElement(next.getElement());
                        root.setLeft(null);
                    }

                    numberOfNodes--;
                    return(aux);
                }
            }
            else
            {
                if (comp.Compare(element, root.getElement()) < 0)
                {
                    return(remove(root.getLeft(), element));
                }
                else
                {
                    return(remove(root.getRight(), element));
                }
            }
        }
        //Tree diagram
        //                 50
        //
        //         30             90
        //
        //      10   45         70   95
        //
        //     5    35  48    55       105
        static void Main(string[] args)
        {
            Tree tree = new Tree();

            tree.insert(50);
            tree.insert(30);
            tree.insert(80);
            tree.insert(10);
            tree.insert(45);
            tree.insert(70);
            tree.insert(95);
            tree.insert(5);
            tree.insert(35);
            tree.insert(48);
            tree.insert(55);
            tree.insert(90);
            tree.insert(105);

            Console.WriteLine("Tree has a height of " + tree.getHeight() + ".");
            tree.insert(120);
            Console.WriteLine("Tree has a height of " + tree.getHeight() + ".");
            tree.insert(4);
            tree.insert(3);
            Console.WriteLine("Tree has a height of " + tree.getHeight() + ".");

            Console.WriteLine("Tree has " + tree.getNumberOfNodes() + " nodes.");

            Console.WriteLine("Is 70 in the tree? " + (tree.contains(70) ? "Yes" : "No"));
            Console.WriteLine("Is 12 in the tree? " + (tree.contains(12) ? "Yes" : "No"));
            Console.WriteLine("Is 55 in the tree? " + (tree.contains(55) ? "Yes" : "No"));
            Console.WriteLine("Is 40 in the tree? " + (tree.contains(40) ? "Yes" : "No"));
            Console.WriteLine("Is 5 in the tree? " + (tree.contains(5) ? "Yes" : "No"));
            Console.WriteLine("Is 6 in the tree? " + (tree.contains(6) ? "Yes" : "No"));
            Console.WriteLine("Is 48 in the tree? " + (tree.contains(48) ? "Yes" : "No"));
            Console.WriteLine("Is 47 in the tree? " + (tree.contains(47) ? "Yes" : "No"));

            Node <int> node45       = tree.search(45);
            String     leftNode45   = node45.getLeft() == null ? "empty" : node45.getLeft().getData().ToString();
            String     rightNode45  = node45.getRight() == null ? "empty" : node45.getRight().getData().ToString();
            String     parentNode45 = node45.getParent() == null ? "empty" : node45.getParent().getData().ToString();

            Console.WriteLine("Node 45 left child: " + leftNode45 + " right child: " + rightNode45 + " parent: " + parentNode45);

            Node <int> node70       = tree.search(70);
            String     leftNode70   = node70.getLeft() == null ? "empty" : node70.getLeft().getData().ToString();
            String     rightNode70  = node70.getRight() == null ? "empty" : node70.getRight().getData().ToString();
            String     parentNode70 = node70.getParent() == null ? "empty" : node70.getParent().getData().ToString();

            Console.WriteLine("Node 70 left child: " + leftNode70 + " right child: " + rightNode70 + " parent: " + parentNode70);

            Node <int> node105       = tree.search(105);
            String     leftNode105   = node105.getLeft() == null ? "empty" : node105.getLeft().getData().ToString();
            String     rightNode105  = node105.getRight() == null ? "empty" : node105.getRight().getData().ToString();
            String     parentNode105 = node105.getParent() == null ? "empty" : node105.getParent().getData().ToString();

            Console.WriteLine("Node 105 left child: " + leftNode105 + " right child: " + rightNode105 + " parent: " + parentNode105);

            tree.delete(80);
            Node <int> node90       = tree.search(90);
            String     leftNode90   = node90.getLeft() == null ? "empty" : node90.getLeft().getData().ToString();
            String     rightNode90  = node90.getRight() == null ? "empty" : node90.getRight().getData().ToString();
            String     parentNode90 = node90.getParent() == null ? "empty" : node90.getParent().getData().ToString();

            Console.WriteLine("Node 90 left child: " + leftNode90 + " right child: " + rightNode90 + " parent: " + parentNode90);

            Node <int> node95       = tree.search(95);
            String     leftNode95   = node95.getLeft() == null ? "empty" : node95.getLeft().getData().ToString();
            String     rightNode95  = node95.getRight() == null ? "empty" : node95.getRight().getData().ToString();
            String     parentNode95 = node95.getParent() == null ? "empty" : node95.getParent().getData().ToString();

            Console.WriteLine("Node 95 left child: " + leftNode95 + " right child: " + rightNode95 + " parent: " + parentNode95);

            Node <int> node50       = tree.search(50);
            String     leftNode50   = node50.getLeft() == null ? "empty" : node50.getLeft().getData().ToString();
            String     rightNode50  = node50.getRight() == null ? "empty" : node50.getRight().getData().ToString();
            String     parentNode50 = node50.getParent() == null ? "empty" : node50.getParent().getData().ToString();

            Console.WriteLine("Node 50 left child: " + leftNode50 + " right child: " + rightNode50 + " parent: " + parentNode50);


            Console.Write("Inorder: ");
            tree.inOrder();
            Console.WriteLine();
            Console.Write("Preorder: ");
            tree.preOrder();
            Console.WriteLine();
            Console.Write("Postorder: ");
            tree.postOrder();
            Console.WriteLine();
            Console.Write("Breadth first: ");
            tree.breadthFirst();
            Console.WriteLine();
        }