Exemple #1
0
        private void deleteNode(int data, BinaryNode currentRoot)
        {
            BinaryNode tempNode = search(data);

            if (tempNode.isLeaf())
            {
                if (position)
                {
                    parent.setLeftChild(null);
                }
                else
                {
                    parent.setRightChild(null);
                }
            }
            else if (tempNode.hasOneChild())
            {
                if (tempNode.getchildPosition())
                {
                    parent.setLeftChild(tempNode.getLeftChild());
                }
                else
                {
                    parent.setRightChild(tempNode.getRightChild());
                }
            }
            else
            {
                BinaryNode min = getMinor(tempNode.getRightChild());
                deleteNode(min.getData());
                tempNode.setData(min.getData());
            }
        }
Exemple #2
0
 private void inOrder(BinaryNode currentRoot)
 {
     if (currentRoot != null)
     {
         inOrder(currentRoot.getLeftChild());
         Console.WriteLine(currentRoot.getData() + " ");
         inOrder(currentRoot.getRightChild());
     }
 }
Exemple #3
0
 private void add(int data, BinaryNode root)
 {
     if (data < root.getData())
     {
         //validacion por la izquierada
         insertion(data, root, 0);
     }
     else
     {
         //validacion por la derecha
         insertion(data, root, 1);
     }
 }
Exemple #4
0
 private BinaryNode search(int data, BinaryNode currentRoot)
 {
     if (currentRoot == null)
     {
         return(null);
     }
     if (data == currentRoot.getData())
     {
         return(currentRoot);
     }
     parent = currentRoot;
     if (data < currentRoot.getData())
     {
         //if false the position is left
         position = true;
         return(search(data, currentRoot.getLeftChild()));
     }
     else
     {
         //if true the the position is right
         position = false;
         return(search(data, currentRoot.getRightChild()));
     }
 }
Exemple #5
0
        public void preOrder()
        {
            if (root == null)
            {
                Console.WriteLine("My man, the tree is empty");
            }

            if (root.isLeaf())
            {
                Console.WriteLine("My man, the only existing node is the root: " + root.getData());
            }
            else
            {
                preOrder(root);
            }
        }