Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Java example to print root, height of a tree and check whether it is balanced or not

import java.util.*;

public class Question4_3 {

    static class Node {
        Integer data;
        Node left;
        Node right;

        public Node(Integer data) {
            this.data = data;
        }

        public int height() {
            int leftHeight = left != null ? left.height() : 0;
            int rightHeight = right != null ? right.height() : 0;
            return 1 + Math.max(leftHeight, rightHeight);
        }

        public boolean isBalanced() {
            int leftHeight = left != null ? left.height() : 0;
            int rightHeight = right != null ? right.height() : 0;
            return (Math.abs(leftHeight - rightHeight) <= 1);
        }
    }

    public static Node minimalBST(int[] sorted) {
        return minimalBST(sorted, 0, sorted.length - 1);
    }

    private static Node minimalBST(int[] sorted, int start, int end) {

        if (start > end) {
            return null;
        }

        int mid = (start + end) / 2;
        
        Node node = new Node(sorted[mid]);

        node.left = minimalBST(sorted, start, mid - 1);
        node.right = minimalBST(sorted, mid + 1, end);

        return node;
    }

    public static Node insert(Node node, Integer data) {
        if (data == null) {
            return null;
        }

        if (node == null) {
            node = new Node(data);
            return node;
        }

        if (data.compareTo(node.data) <= 0) {
            if (node.left == null) {
                node.left = new Node(data);
            } else {
                insert(node.left, data);
            }
        } else {
            if (node.right == null) {
                node.right = new Node(data);
            } else {
                insert(node.right, data);
            }
        }

        return node;
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        Node root = minimalBST(array);

        System.out.println("Root? " + root.data);
        // System.out.println("Created BST? " + root.isBST());
        System.out.println("Height: " + root.height());
        System.out.println("Is balanced? " + root.isBalanced());

        Random random = new Random();

        root = null;
        array = new int[20];

        for (int i = 0; i < array.length; i++)
        {
            array[i] = random.nextInt(array.length);
            root = insert(root, array[i]);
            System.out.print(array[i] + " ");
        }
        System.out.println();

        // root = minimalBST(array);

        System.out.println("Root? " + root.data);
        System.out.println("Height: " + root.height());
        System.out.println("Is balanced? " + root.isBalanced());
    }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.