Skip to content

Commit 1bd4dc1

Browse files
committed
Java dsa-Tree
1 parent 5e13d56 commit 1bd4dc1

File tree

2 files changed

+245
-0
lines changed

2 files changed

+245
-0
lines changed

Tree/BST.java

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package Java_DSA.Tree;
2+
3+
import java.util.Scanner;
4+
5+
public class BST {
6+
private static Node root;
7+
8+
private static class Node{
9+
int data;
10+
Node left;
11+
Node right;
12+
13+
public Node(int data){
14+
this.data = data;
15+
}
16+
}
17+
18+
public static Node insert(Node root,int val){
19+
Node temp = new Node(val);
20+
if(root == null){
21+
root = temp;
22+
return root;
23+
}
24+
if(val < root.data){
25+
root.left = insert(root.left,val);
26+
}
27+
else{
28+
root.right = insert(root.right,val);
29+
}
30+
31+
return root;
32+
}
33+
34+
public static void insert(int val){
35+
Node p = root;
36+
Node ptr = null;
37+
while(p != null){
38+
ptr = p;
39+
if(val < p.data){
40+
p = p.left;
41+
}
42+
else if(val > p.data){
43+
p = p.right;
44+
}
45+
else{
46+
System.out.println(val +" is already present in tree.");
47+
return;
48+
}
49+
}
50+
Node temp = new Node(val);
51+
52+
if(ptr == null)
53+
root = temp;
54+
else if(val < ptr.data)
55+
ptr.left = temp;
56+
else
57+
ptr.right = temp;
58+
}
59+
60+
public static void inorder(Node root){
61+
if(root == null){
62+
return;
63+
}
64+
inorder(root.left);
65+
System.out.print(root.data + " ");
66+
inorder(root.right);
67+
}
68+
69+
public static boolean search(Node root,int key){
70+
if(root == null){
71+
return false;
72+
}
73+
if(key < root.data){
74+
return search(root.left,key);
75+
}
76+
else if(key == root.data){
77+
return true;
78+
}
79+
else{
80+
return search(root.right,key);
81+
}
82+
}
83+
84+
public static Node delete(Node root,int val){
85+
if(search(root,val)) {
86+
if (val < root.data) {
87+
root.left = delete(root.left, val);
88+
} else if (val > root.data) {
89+
root.right = delete(root.right, val);
90+
} else { //root.data == val
91+
if (root.left == null && root.right == null) {
92+
return null;
93+
}
94+
if (root.left == null)
95+
return root.right;
96+
else if (root.right == null)
97+
return root.left;
98+
else {
99+
Node IS = inorderSuccesser(root.right);
100+
root.data = IS.data;
101+
root.right = delete(root.right, IS.data);
102+
}
103+
}
104+
}
105+
else
106+
System.out.println("Data is not present to delete.");
107+
108+
return root;
109+
}
110+
111+
public static Node inorderSuccesser(Node root){
112+
while(root.left != null){
113+
root = root.left;
114+
}
115+
return root;
116+
}
117+
118+
public static int countNode(Node root){
119+
if(root == null){
120+
return 0;
121+
}
122+
int leftNode = countNode(root.left);
123+
int rightNode = countNode(root.right);
124+
return leftNode + rightNode + 1;
125+
}
126+
127+
public static void main(String[] args) {
128+
Scanner sc = new Scanner(System.in);
129+
//Node root = null;
130+
boolean flag = true;
131+
System.out.println("1)Insert data.\n2)Search.\n3)Delete.\n4)Display inorder.\n5)Count the nodes.\n6)Exit.");
132+
while(flag){
133+
System.out.println("Enter your choice : ");
134+
int choice = sc.nextInt();
135+
switch (choice){
136+
case 1->{
137+
System.out.println("Enter data to insert : ");
138+
int data = sc.nextInt();
139+
//root = insert(root,data);
140+
insert(data);
141+
}
142+
case 2->{
143+
System.out.println("Enter data to search : ");
144+
int searchValue = sc.nextInt();
145+
if(search(root,searchValue))
146+
System.out.println("Found");
147+
else
148+
System.out.println("Not found");
149+
}
150+
case 3->{
151+
System.out.println("Enter data to delete : ");
152+
int dataToDelete = sc.nextInt();
153+
root = delete(root,dataToDelete);
154+
}
155+
case 4->{
156+
inorder(root);
157+
System.out.println();
158+
}
159+
case 5->{
160+
System.out.println("Number of nodes : "+countNode(root));
161+
System.out.println();
162+
}
163+
case 6->{
164+
flag = false;
165+
}
166+
default -> {
167+
System.out.println("Invalid choice..!");
168+
}
169+
}
170+
}
171+
}
172+
}

Tree/BinaryTree.java

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package Java_DSA.Tree;
2+
3+
import java.util.Scanner;
4+
5+
public class BinaryTree<Private> {
6+
private static Node left;
7+
private static Node right;
8+
9+
private static class Node{
10+
int data;
11+
Node left;
12+
Node right;
13+
14+
public Node(int data){
15+
this.data = data;
16+
}
17+
}
18+
19+
static int index = -1;
20+
public static Node createTree(int[] nodes){
21+
index++;
22+
if(nodes[index] == -1){
23+
return null;
24+
}
25+
Node temp = new Node(nodes[index]);
26+
temp.left = createTree(nodes);
27+
temp.right = createTree(nodes);
28+
29+
return temp;
30+
}
31+
32+
public static void preorder(Node root){
33+
if(root == null){
34+
return;
35+
}
36+
System.out.print(root.data + " ");
37+
preorder(root.left);
38+
preorder(root.right);
39+
}
40+
41+
public static void inorder(Node root){
42+
if(root == null){
43+
return;
44+
}
45+
inorder(root.left);
46+
System.out.print(root.data + " ");
47+
inorder(root.right);
48+
}
49+
50+
public static void postorder(Node root){
51+
if(root == null){
52+
return;
53+
}
54+
postorder(root.left);
55+
postorder(root.right);
56+
System.out.print(root.data + " ");
57+
58+
}
59+
60+
public static void main(String[] args) {
61+
Scanner sc = new Scanner(System.in);
62+
int[] nodes = {4,7,2,-1,-1,3,9,-1,-1,10,-1,-1,5,-1,15,11,-1,-1,14,-1,-1};
63+
Node root = createTree(nodes);
64+
System.out.println("Preorder traversal ");
65+
preorder(root);
66+
System.out.println();
67+
System.out.println("Inorder traversal ");
68+
inorder(root);
69+
System.out.println();
70+
System.out.println("postorder traversal ");
71+
postorder(root);
72+
}
73+
}

0 commit comments

Comments
 (0)