/* 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); }
/* 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); }
public TreeNodev1(int d) { data = d; left = right = null; }