import java.util.Scanner;
public class BTree {
public static void main(String args[]) {
List c = new List();
int d;
Scanner s = new Scanner(System.in);
do {
System.out.println("Binary tree operations");
System.out.println("1.create()");
System.out.println("2.inorder()");
System.out.println("3.preorder()");
System.out.println("4.postorder()");
System.out.println("5.empty()");
System.out.println("Enter ur choice");
d = s.nextInt();
switch (d) {
case 1:
c.create();
break;
case 2:
c.inorder(c.root);
break;
case 3:
c.preorder(c.root);
break;
case 4:
c.postorder(c.root);
break;
case 5:
if (c.isempty())
System.out.println("BTree is empty");
else
System.out.println("BTree is not empty");
break;
}
} while (d < 6);
}
}
class Node {
Object data;
Node left;
Node right;
}
class List {
Node s[] = new Node[10];
int top;
Object value;
Node root;
Node p, temp;
Scanner sc = new Scanner(System.in);
public List() {
top = 0;
s[top] = null;
}
public boolean isempty() {
return root == null;
}
public void create() {
root = new Node();
System.out.println("Enter a value");
Object value;
value = sc.nextInt();
String ch;
root.data = value;
p = root;
while (p != null) {
System.out.println("The right child" + p.data + "is exists");
ch = sc.next();
if (ch.equals("n"))
p.right = null;
else {
temp = new Node();
System.out.println("Enter a value");
value = sc.nextInt();
temp.data = value;
p.right = temp;
push(temp);
}
System.out.println("The left child of " + p.data + "is exists");
ch = sc.next();
if (ch.equals("n")) {
p.left = null;
p = pop();
} else {
temp = new Node();
System.out.println("Enter a value");
value = sc.nextInt();
temp.data = value;
p.left = temp;
p = temp;
}
}
}
public void push(Node value) {
if (top == s.length - 1) {
System.out.println("Stack is overflow");
return;
} else
s[++top] = value;
}
public Node pop() {
if (top == -1) {
System.out.println("Stack is underflow");
return null;
}
Node x = s[top--];
return x;
}
public void inorder(Node p) {
if (p != null) {
inorder(p.left);
System.out.print(p.data + " ");
inorder(p.right);
}
}
public void preorder(Node p) {
if (p != null) {
System.out.print(p.data + " ");
preorder(p.left);
preorder(p.right);
}
}
public void postorder(Node p) {
if (p != null) {
postorder(p.left);
postorder(p.right);
System.out.print(p.data + " ");
}
}
}
Output:
Binary tree
operations
1.create()
2.inorder()
3.preorder()
4.postorder()
5.empty()
Enter ur
choice
1
Enter a value
10
The right child10is exists
Y
Enter a value
20
The left child of 10is exists
Y
Enter a value
30
The right child30is
exists
Y
Enter a value
40
The left child of 30is exists
n
The right child40is exists
n
The left child of 40is exists
n
The right child20is exists
n
The left child of 20is exists
n
Binary tree
operations
1.create()
2.inorder()
3.preorder()
4.postorder()
5.empty()
Enter ur
choice
2
The inorder is =30
,40, 10, 20,
Binary tree operations
1.create()
2.inorder()
3.preorder()
4.postorder()
5.empty()
Enter ur
choice
3
The preorder is=10 30 40 20
Binary tree operations
1.create()
2.inorder()
3.preorder()
4.postorder()
5.empty()
Enter ur
choice
4
The post order is=40 30 20 10
Binary tree operations
1.create()
2.inorder()
3.preorder()
4.postorder()
5.empty()
Enter ur
choice
5
BTree is not empty
Binary tree operations
1.create()
2.inorder()
3.preorder()
4.postorder()
5.empty()
Enter ur
choice
6
Read more ...