forked from codehouseindia/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestTree.java
More file actions
152 lines (146 loc) · 3.42 KB
/
TestTree.java
File metadata and controls
152 lines (146 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package tree;
import java.util.Scanner;
class Bst
{
Node root;
class Node
{
Node left;
int data;
Node right;
}
Bst()
{
root=null;
}
public void insert(int data)
{
Node node=new Node();
node.data=data;
node.left=null;
node.right=null;
if(root==null)
root=node;
else if(root.data<data)
{
Node temp=root;
while(temp.data<data)
{
if(temp.right==null)
temp.right=node;
temp=temp.right;
}
while(temp.data>data)
{
if(temp.left==null)
temp.left=node;
temp=temp.left;
}
}
else if(root.data>data)
{
Node temp=root;
while(temp.data>data)
{
if(temp.left==null)
temp.left=node;
temp=temp.left;
}
while(temp.data<data)
{
if(temp.right==null)
temp.right=node;
temp=temp.right;
}
}
else System.out.println("Duplicate not allowed in tree");
}
public void delete(int data)
{
if(root.data==data)
{
if(root.right==null&&root.left==null)
root=null;
else if(root.right==null&&root.left!=null)
root=root.left;
else if(root.right!=null&&root.left==null)
root=root.right;
else if(root.right!=null&&root.left!=null)
{
Node temp=root.left;
root.data=temp.data;
root.left=temp.left;
Node temp2=root.right;
while(temp2.left!=null)
temp2=temp2.left;
temp2.left=temp.right;
}
}
else if(root.data<data)
{
Node temp=root.right;
if(temp.data!=data&&temp!=null)
{
while(temp.data<data)
{
}
}
/* Node temp=root.right;
if(temp.data==data)
{
if(temp.right==null&&temp.left==null)
temp=null;
else if(temp.right==null&&temp.left!=null)
temp=temp.left;
else if(temp.right!=null&&temp.left==null)
temp=temp.right;
else if(temp.right!=null&&temp.left!=null)
{
Node temp1=temp.left;
temp.data=temp.data;
temp.left=temp.left;
Node temp2=temp.right;
while(temp2.left!=null)
temp2=temp2.left;
temp2.left=temp.right;
}
}
*/
}
}
public void display(Node test)
{
if(test==null)
return;
System.out.println(test.data);
display(test.left);
display(test.right);
}
}
class TestTree
{
public static void main(String args[])
{
boolean check;
Scanner sc=new Scanner(System.in);
Bst tree=new Bst();
do {
System.out.println("Enter the element which you want to insert in tree");
int n;
n=sc.nextInt();
tree.insert(n);
System.out.println("Do you want to insert another element");
n=sc.nextInt();
if(n==1)
check=true;
else check=false;
} while (check);
tree.display(tree.root);
System.out.println("Enter the element which you want to delete in tree");
int n;
n=sc.nextInt();
tree.delete(n);
System.out.print("after deleteing element in tree");
tree.display(tree.root);
}
}