I am currently checking about coding an algorithms. If we have the following case:
Given a sorted (increasing order) array with unique integer elements, wrote an alg
middle
of the array and make it as root
middle
of the left half and make it the left child
of the rootmiddle
of the right half and make it the right child
of the root public TreeNode Convert(int[] array)
{
if (array == null || array.Length == 0)
{
return null;
}
return Convert(array, 0, array.Length - 1);
}
private static TreeNode Convert(int[] array, int lo, int hi)
{
if (lo > hi)
{
return null;
}
int mid = lo + (hi - lo) / 2;
var root = new TreeNode(array[mid]);
root.Left = Convert(array, lo, mid - 1);
root.Right = Convert(array, mid + 1, hi);
return root;
}
Time Complexity: O(n)
from CodeStandard
Algorithm -
Wikipedia Link
CODE
class Node {
int data;
Node left, right;
Node(int d) {
data = d;
left = right = null;
}
}
class BinaryTree {
static Node root;
/* A function that constructs Balanced Binary Search Tree
from a sorted array */
Node 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;
Node node = new Node(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;
}
/* A utility function to print preorder traversal of BST */
void preOrder(Node node) {
if (node == null) {
return;
}
System.out.print(node.data + " ");
preOrder(node.left);
preOrder(node.right);
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
int arr[] = new int[]{2, 4, 6, 8, 10, 12};
int n = arr.length;
root = tree.sortedArrayToBST(arr, 0, n - 1);
System.out.println("Preorder traversal of constructed BST");
tree.preOrder(root);
}
}
A sorted array will give you a balanced binary tree. This could be done easily in O(n) time, as we can get the middle element in O(1) time. Following is a simple algorithm,
Construct a node for the middle element in the array and return it (this will be the root in the base case).
Repeat from 1. on the left half of the array, assigning the return value to the left child of the root.
Repeat from 1. on the right half of the array, assigning the return value to the right child of the root.
Java implementation
TreeNode sortedArrayToBST(int arr[], int start, int end) {
if (start > end)
return null; // same as (start+end)/2, avoids overflow.
int mid = start + (end - start) / 2;
TreeNode node = new
TreeNode(arr[mid]);
node.left = sortedArrayToBST(arr, start, mid-1);
node.right = sortedArrayToBST(arr, mid+1, end);
return node;
}
TreeNode sortedArrayToBST(int arr[]) { return sortedArrayToBST(arr, 0, arr.length-1); }
Time Complexity: ** O(n) ** Following is the recurrance relation for sortedArrayToBST().
T(n) = 2T(n/2) + C
T(n) --> Time taken for an array of size n
C --> Constant (Finding middle of array and linking root to left and right subtrees take constant time)