-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.java
233 lines (215 loc) · 4.56 KB
/
Tree.java
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package Tree;
import java.util.*;
class Node{
int data;
Node left,right;
public Node(int data){
this.left = null;
this.right = null;
this.data = data;
}
}
class BinaryTree{
Node root,ptr;
public BinaryTree(){
this.root = null;
}
void create() {
Scanner sc = new Scanner(System.in);
int cnt = 1;
do{
System.out.print("Enter node value: ");
int data = sc.nextInt();
Node new_node = new Node(data);
if(root == null) {
root = new_node;
System.out.println("Root = "+root.data);
}
else {
ptr = root;
while(ptr != null) {
sc.nextLine();
System.out.println("Where do you want to place the new data\n"
+ "Press (l) for left side and (r) for right: ");
char side = sc.next().charAt(0);
if (side == 'l') {
if(ptr.left==null) {
ptr.left = new_node;
System.out.println("New data: "+new_node.data);
break;
}
else {
ptr = ptr.left;
}
}
else if (side == 'r') {
if(ptr.right==null) {
ptr.right = new_node;
System.out.println("New data: "+new_node.data);
break;
}
else {
ptr = ptr.right;
}
}
else {
System.out.println("Enter valid input!");
}
}
}
System.out.print("\nPress 1 to add another value and 0 to exit: ");
cnt = sc.nextInt();
if(cnt == 0) {
break;
}
}while(cnt == 1);
}
void Inorder(Node root){ // L N R
if(root != null) {
Inorder(root.left);
System.out.print(root.data+" ");
Inorder(root.right);
}
}
void Inorder() {
Inorder(root);
}
void Preorder(Node root){ // N L R
if(root != null) {
System.out.print(root.data+" ");
Preorder(root.left);
Preorder(root.right);
}
}
void Preorder() {
Preorder(root);
}
void Postorder(Node root){ // L R N
if(root != null) {
Postorder(root.left);
Postorder(root.right);
System.out.print(root.data+" ");
}
}
void Postorder() {
Postorder(root);
}
void NonRecursiveIn(){
if(root==null){
System.out.println("The tree is empty");
}
Stack s = new Stack();
ptr=root;
do {
while(ptr!=null) {
s.push(ptr);
ptr=ptr.left;
}
if(!s.isEmpty()) {
ptr=(Node)s.pop();
System.out.print(ptr.data+" ");
ptr=ptr.right;
}
}while(!s.isEmpty()||ptr!=null);
}
void NonRecursivePre(){
if(root==null){
System.out.println("The tree is empty");
}
Stack s = new Stack();
ptr=root;
do {
while(ptr!=null) {
System.out.print(ptr.data+" ");
s.push(ptr);
ptr=ptr.left;
}
if(!s.isEmpty()) {
ptr=(Node)s.pop();
ptr=ptr.right;
}
}while(!s.isEmpty()||ptr!=null);
}
void NonRecursivePost(){
if(root==null){
System.out.println("The tree is empty");
}
Stack s1=new Stack();
Stack s2=new Stack();
ptr=root;
s1.push(root);
while(!s1.isEmpty()){
ptr = (Node)s1.pop();
s2.push(ptr.data);
if(ptr.left!=null) {
s1.push(ptr.left);
}
if(ptr.right!=null) {
s1.push(ptr.right);
}
}
while(!s2.isEmpty()) {
System.out.print(s2.pop()+" ");
}
}
}
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
BinaryTree bt = new BinaryTree();
System.out.println("----- Welcome -----");
System.out.println("\nCreate a Binary Tree");
bt.create();
System.out.println("--- Binary Tree created succesfully! ---");
int cnt = 1;
do{
System.out.print("\nTraversal Operations: \n1. Inorder\n2. Preorder\n3. Postorder"
+ "\nSELECT OPERATION: ");
int select = sc.nextInt();
switch(select) {
case 1:{
System.out.println("\n1. Recursive\n2. Non Recursive");
int choose = sc.nextInt();
switch(choose) {
case 1:bt.Inorder();
break;
case 2:bt.NonRecursiveIn();
break;
default:System.out.println("Invalid option");
}
break;
}
case 2:{
System.out.println("\n1. Recursive\n2. Non Recursive");
int choose = sc.nextInt();
switch(choose) {
case 1:bt.Preorder();
break;
case 2:bt.NonRecursivePre();
break;
default:System.out.println("Invalid option");
}
break;
}
case 3:{
System.out.println("\n1. Recursive\n2. Non Recursive");
int choose = sc.nextInt();
switch(choose) {
case 1:bt.Postorder();
break;
case 2:bt.NonRecursivePost();
break;
default:System.out.println("Invalid option");
}
break;
}
default:System.out.println("Invalid option");
}
System.out.print("\n\nPress 1 to CONTINUE and any number to EXIT");
cnt = sc.nextInt();
if(cnt != 1) {
System.out.print("----- Thank You -----");
}
}while(cnt == 1);
}
}