Esempio n. 1
0
 /* A utility function to print preorder traversal of BST */
 public virtual void preOrder(TreeNodev1 node)
 {
     if (node == null)
     {
         return;
     }
     Console.Write(node.data + " ");
     preOrder(node.left);
     preOrder(node.right);
 }
Esempio n. 2
0
        /* A function that constructs Balanced Binary Search Tree
         *      from a sorted array */
        public virtual TreeNodev1 sortedArrayToBST(int[] arr, int start, int end)
        {
            /* Base Case */
            if (start > end)
            {
                return(null);
            }

            /* Get the middle element and make it root */
            int        mid  = (start + end) / 2;
            TreeNodev1 node = new TreeNodev1(arr[mid]);

            /* Recursively construct the left subtree and make it
             *          left child of root */
            node.left = sortedArrayToBST(arr, start, mid - 1);

            /* Recursively construct the right subtree and make it
             *          right child of root */
            node.right = sortedArrayToBST(arr, mid + 1, end);

            return(node);
        }
Esempio n. 3
0
 public TreeNodev1(int d)
 {
     data = d;
     left = right = null;
 }