Check Binary is Continues Tree

A Continues Tree is a special type of binary tree in which the difference between node data and its corresponding left and the difference between node data and its corresponding right should always be 1.

package org.wesome.dsalgo;

import lombok.Data;

import java.util.Objects;

public class BinarySearchTree {
    Node root;

    static boolean continuousTree(Node node) {
        if (Objects.isNull(node)) {
            return true;
        }
        /*  if left and right child nodes are null  */
        if (Objects.isNull(node.left) && Objects.isNull(node.right)) {
            return true;
        }
        /*  if left subtree is null */
        if (Objects.isNull(node.left)) {
            return (Math.abs(node.data - node.right.data) == 1) && continuousTree(node.right);
        }

        /*  if right subtree is null    */
        if (Objects.isNull(node.right)) {
            return (Math.abs(node.data - node.left.data) == 1) && continuousTree(node.left);
        }
        return (Math.abs(node.data - node.left.data) == 1) && (Math.abs(node.data - node.right.data) == 1) && continuousTree(node.left) && continuousTree(node.right);
    }

    /* Inorder -> Left -> Root -> Right */
    void printInOrder() {
        System.out.println("print In Order");
        printInOrder(root);
    }

    void printInOrder(Node root) {
        if (Objects.isNull(root)) {
            return;
        }
        printInOrder(root.left);
        System.out.print(" " + root.data);
        printInOrder(root.right);
    }

    void insert(int data) {
        System.out.println("BinarySearchTree.insert data = " + data);
        root = insert(root, data);
    }

    Node insert(Node root, int data) {
        if (Objects.isNull(root)) {
            Node tempNode = new Node(data);
            return tempNode;
        }
        if (data > root.data) {
            root.right = insert(root.right, data);
        } else {
            root.left = insert(root.left, data);
        }
        return root;
    }
}

@Data
class Node {
    int data;
    Node left, right;

    public Node(int data) {
        this.data = data;
        left = right = null;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.BinarySearchTree.continuousTree;

public class BinarySearchTreeTest {
    @Test
    void continuousTreeTest() {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        binarySearchTree.insert(3);
        binarySearchTree.insert(2);
        binarySearchTree.insert(4);
        binarySearchTree.insert(1);
        binarySearchTree.insert(3);
        binarySearchTree.insert(5);
        binarySearchTree.printInOrder();
        Assertions.assertTrue(continuousTree(binarySearchTree.root));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on