Skip to content

Commit 3e2f312

Browse files
Trees - 1
1 parent fe5fddb commit 3e2f312

File tree

3 files changed

+241
-0
lines changed

3 files changed

+241
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
class BST {
2+
public class Node {
3+
private int value;
4+
private Node left;
5+
private Node right;
6+
private int height;
7+
8+
public Node(int value) {
9+
this.value = value;
10+
}
11+
12+
public int getValue() {
13+
return value;
14+
}
15+
}
16+
17+
private Node root;
18+
19+
public BST() {
20+
21+
}
22+
23+
public int height(Node node) {
24+
if (node == null) {
25+
return -1;
26+
}
27+
return node.height;
28+
}
29+
30+
public boolean isEmpty() {
31+
return root == null;
32+
}
33+
34+
public void insert(int value) {
35+
root = insert(value, root);
36+
}
37+
38+
private Node insert(int value, Node node) {
39+
if (node == null) {
40+
node = new Node(value);
41+
return node;
42+
}
43+
44+
if (value < node.value) {
45+
node.left = insert(value, node.left);
46+
}
47+
48+
if (value > node.value) {
49+
node.right = insert(value, node.right);
50+
}
51+
52+
node.height = Math.max(height(node.left), height(node.right)) + 1;
53+
return node;
54+
}
55+
56+
public void populate(int[] nums) {
57+
for (int i = 0; i < nums.length; i++) {
58+
this.insert(nums[i]);
59+
}
60+
}
61+
62+
public void populatedSorted(int[] nums) {
63+
populatedSorted(nums, 0, nums.length);
64+
}
65+
66+
private void populatedSorted(int[] nums, int start, int end) {
67+
if (start >= end) {
68+
return;
69+
}
70+
71+
int mid = (start + end) / 2;
72+
73+
this.insert(nums[mid]);
74+
populatedSorted(nums, start, mid);
75+
populatedSorted(nums, mid + 1, end);
76+
}
77+
78+
public boolean balanced() {
79+
return balanced(root);
80+
}
81+
82+
private boolean balanced(Node node) {
83+
if (node == null) {
84+
return true;
85+
}
86+
return Math.abs(height(node.left) - height(node.right)) <= 1 && balanced(node.left) && balanced(node.right);
87+
}
88+
89+
public void display() {
90+
display(this.root, "Root Node: ");
91+
}
92+
93+
private void display(Node node, String details) {
94+
if (node == null) {
95+
return;
96+
}
97+
System.out.println(details + node.value);
98+
display(node.left, "Left child of " + node.value + " : ");
99+
display(node.right, "Right child of " + node.value + " : ");
100+
}
101+
102+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import java.util.Scanner;
2+
3+
class BinaryTree {
4+
5+
public BinaryTree() {
6+
7+
}
8+
9+
private static class Node {
10+
int value;
11+
Node left;
12+
Node right;
13+
14+
public Node(int value) {
15+
this.value = value;
16+
}
17+
}
18+
19+
private Node root;
20+
21+
// insert elements
22+
public void populate(Scanner scanner) {
23+
System.out.println("Enter the root Node: ");
24+
int value = scanner.nextInt();
25+
root = new Node(value);
26+
populate(scanner, root);
27+
}
28+
29+
private void populate(Scanner scanner, Node node) {
30+
System.out.println("Do you want to enter left of " + node.value);
31+
boolean left = scanner.nextBoolean();
32+
if (left) {
33+
System.out.println("Enter the value of the left of " + node.value);
34+
int value = scanner.nextInt();
35+
node.left = new Node(value);
36+
populate(scanner, node.left);
37+
}
38+
39+
System.out.println("Do you want to enter right of " + node.value);
40+
boolean right = scanner.nextBoolean();
41+
if (right) {
42+
System.out.println("Enter the value of the right of " + node.value);
43+
int value = scanner.nextInt();
44+
node.right = new Node(value);
45+
populate(scanner, node.right);
46+
}
47+
48+
}
49+
50+
public void display() {
51+
display(this.root, "");
52+
}
53+
54+
private void display(Node node, String indent) {
55+
if (node == null) {
56+
return;
57+
}
58+
System.out.println(indent + node.value);
59+
display(node.left, indent + "\t");
60+
display(node.right, indent + "\t");
61+
}
62+
63+
public void prettyDisplay() {
64+
prettyDisplay(root, 0);
65+
}
66+
67+
private void prettyDisplay(Node node, int level) {
68+
if (node == null) {
69+
return;
70+
}
71+
72+
prettyDisplay(node.right, level + 1);
73+
74+
if (level != 0) {
75+
for (int i = 0; i < level - 1; i++) {
76+
System.out.print("|\t\t");
77+
}
78+
System.out.println("|------->" + node.value);
79+
} else {
80+
System.out.println(node.value);
81+
}
82+
prettyDisplay(node.left, level + 1);
83+
}
84+
85+
public void preOrder() {
86+
preOrder(root);
87+
}
88+
89+
private void preOrder(Node node) {
90+
if (node == null) {
91+
return;
92+
}
93+
System.out.print(node.value + " ");
94+
preOrder(node.left);
95+
preOrder(node.right);
96+
}
97+
98+
public void inOrder() {
99+
preOrder(root);
100+
}
101+
102+
private void inOrder(Node node) {
103+
if (node == null) {
104+
return;
105+
}
106+
preOrder(node.left);
107+
System.out.print(node.value + " ");
108+
preOrder(node.right);
109+
}
110+
111+
public void postOrder() {
112+
preOrder(root);
113+
}
114+
115+
private void postOrder(Node node) {
116+
if (node == null) {
117+
return;
118+
}
119+
preOrder(node.left);
120+
preOrder(node.right);
121+
System.out.print(node.value + " ");
122+
}
123+
124+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.Scanner;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
// Scanner scanner = new Scanner(System.in);
6+
// BinaryTree tree = new BinaryTree();
7+
// tree.populate(scanner);
8+
// tree.prettyDisplay();
9+
10+
BST tree = new BST();
11+
int[] nums = { 5, 2, 7, 1, 4, 6, 9, 8, 3, 10 };
12+
tree.populate(nums);
13+
tree.display();
14+
}
15+
}

0 commit comments

Comments
 (0)