Create BST From Sorted Array

package com.thealgorithms.datastructures.trees;

import com.thealgorithms.datastructures.trees.BinaryTree.Node;

/**
 * Given a sorted array. Create a balanced binary search tree from it.
 *
 * Steps: 1. Find the middle element of array. This will act as root 2. Use the
 * left half recursively to create left subtree 3. Use the right half
 * recursively to create right subtree
 */
public class CreateBSTFromSortedArray {

    public static void main(String[] args) {
        test(new int[]{});
        test(new int[]{1, 2, 3});
        test(new int[]{1, 2, 3, 4, 5});
        test(new int[]{1, 2, 3, 4, 5, 6, 7});
    }

    private static void test(int[] array) {
        BinaryTree root = new BinaryTree(createBst(array, 0, array.length - 1));
        System.out.println("\n\nPreorder Traversal: ");
        root.preOrder(root.getRoot());
        System.out.println("\nInorder Traversal: ");
        root.inOrder(root.getRoot());
        System.out.println("\nPostOrder Traversal: ");
        root.postOrder(root.getRoot());
    }

    private static Node createBst(int[] array, int start, int end) {
        // No element left.
        if (start > end) {
            return null;
        }
        int mid = start + (end - start) / 2;

        // middle element will be the root
        Node root = new Node(array[mid]);
        root.left = createBst(array, start, mid - 1);
        root.right = createBst(array, mid + 1, end);
        return root;
    }
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.