Exemplo n.º 1
0
        public static void Insert(NodeBT node, int newData)
        {
            if (root == null)
            {
                root = new NodeBT(newData, null, null);
                return;
            }
            int compareValue = newData - node.data;

            // Recursive left subtree, continue to find the insertion position
            if (compareValue < 0)
            {
                if (node.left == null)
                {
                    node.left = new NodeBT(newData, null, null);
                }
                else
                {
                    Insert(node.left, newData);
                }
            }
            else if (compareValue > 0)
            {
                // Recursive right subtree, continue to find the insertion position
                if (node.right == null)
                {
                    node.right = new NodeBT(newData, null, null);
                }
                else
                {
                    Insert(node.right, newData);
                }
            }
        }
Exemplo n.º 2
0
        public static NodeBT Remove(NodeBT node, int newData)
        {
            if (node == null)
            {
                return(node);
            }
            int compareValue = newData - node.data;

            if (compareValue > 0)
            {
                node.right = Remove(node.right, newData);
            }
            else if (compareValue < 0)
            {
                node.left = Remove(node.left, newData);
            }
            else if (node.left != null && node.right != null)
            {
                // Find the minimum node of the right subtree to replace the current node
                node.data  = SearchMinValue(node.right).data;
                node.right = Remove(node.right, node.data);
            }
            else
            {
                node = (node.left != null) ? node.left : node.right;
            }
            return(node);
        }
Exemplo n.º 3
0
 //Post-order transversal binary search tree
 public static void PostOrder(NodeBT root)
 {
     if (root == null)
     {
         return;
     }
     PostOrder(root.left);
     PostOrder(root.right);
     ConsoleUtility.WriteLine($"{root.data},");
 }
Exemplo n.º 4
0
 // In-order transversal binary search tree
 public static void InOrder(NodeBT root)
 {
     if (root == null)
     {
         return;
     }
     InOrder(root.left);  // Transversing the left subtree
     ConsoleUtility.WriteLine($"{root.data},");
     InOrder(root.right); // Transversing the right subtree
 }
Exemplo n.º 5
0
 // Preorder trasversal binary search tree
 public static void PreOrder(NodeBT root)
 {
     if (root == null)
     {
         return;
     }
     ConsoleUtility.WriteLine($"{root.data},");
     // Recursive Transversing the left subtree
     PreOrder(root.left);
     // Recursive Transversing the right subtree
     PreOrder(root.right);
 }
Exemplo n.º 6
0
 public static NodeBT SearchMinValue(NodeBT node)
 {
     if (node == null || node.data == 0)
     {
         return(null);
     }
     if (node.left == null)
     {
         return(node);
     }
     // Recursively find minimum from thje left subtree
     return(SearchMinValue(node.left));
 }
Exemplo n.º 7
0
 // Maximum value
 public static NodeBT SearchMaxValue(NodeBT node)
 {
     if (node == null || node.data == 0)
     {
         return(null);
     }
     if (node.right == null)
     {
         return(node);
     }
     // Recursively find minimum from the right subtree
     return(SearchMaxValue(node.right));
 }
Exemplo n.º 8
0
 public NodeBT(int data, NodeBT left, NodeBT right)
 {
     this.data  = data;
     this.left  = left;
     this.right = right;
 }