package org.wesome.dsalgo;
import lombok.Data;
import java.util.LinkedList;
import java.util.Objects;
import java.util.Queue;
@Data
class Node {
int data;
int hrzDis;
Node left, right;
public Node(int data) {
this.data = data;
left = right = null;
}
}
public class BinarySearchTree {
Node root;
public static void leftView(Node root) {
if (Objects.isNull(root)) {
return;
}
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 1; i <= size; i++) {
Node node = queue.poll();
if (i == 1) {
System.out.print(node.data + " ");
}
if (Objects.nonNull(node.left)) {
queue.add(node.left);
}
if (Objects.nonNull(node.right)) {
queue.add(node.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;
}
}
package org.wesome.dsalgo;
import org.junit.jupiter.api.Test;
public class BinarySearchTreeTest {
@Test
void bottomViewTest() {
BinarySearchTree binarySearchTree = new BinarySearchTree();
binarySearchTree.insert(4);
binarySearchTree.insert(2);
binarySearchTree.insert(1);
binarySearchTree.insert(3);
binarySearchTree.insert(6);
binarySearchTree.insert(5);
binarySearchTree.insert(7);
binarySearchTree.insert(7);
binarySearchTree.leftView(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()
}