forked from kalaskarpranav/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
108 lines (96 loc) · 3.26 KB
/
BinaryTree.java
File metadata and controls
108 lines (96 loc) · 3.26 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
import java.util.Scanner;
class Node{
private int data;
private Node left,right;
public Node(int data){ //Constructor to work as setter
this.data=data;
left=right=null;
}
public void setLeft(Node left) {this.left = left;} //setters for left and right pointers
public void setRight(Node right) {this.right = right;}
public int getData() { //getter for retrieving the value
return data;
} //getters for instance variables
public Node getLeft() {return left;}
public Node getRight() {return right;}
}
public class BinaryTree {
static Node root;
static Scanner sn=new Scanner(System.in); //made static so that instance and static methods both can access
public void insert(){
Node current=root;
System.out.println("Enter the value to be inserted");
int data=sn.nextInt();
Node n=new Node(data);
if (root==null)root=n;
else
{
while(true){ //loop terminates internally
if (n.getData() < current.getData()){
if(current.getLeft()==null){
current.setLeft(n);
//
return;
}
else current=current.getLeft();
}
else{
if(current.getRight()==null) {
current.setRight(n);
//
return;
}
else current=current.getRight();
}
}
}
}
public void Inorder(Node node){
if(node==null)return;
Inorder(node.getLeft());
System.out.print(node.getData()+" ");
Inorder(node.getRight());
}
public void Preorder(Node node){
if(node==null)return;
System.out.print(node.getData()+" ");
Inorder(node.getLeft());
Inorder(node.getRight());
}
public void Postorder(Node node){
if(node==null)return;
Inorder(node.getLeft());
Inorder(node.getRight());
System.out.print(node.getData()+" ");
}
public static void main(String[] args) {
BinaryTree bt =new BinaryTree(); //so that instance methods can be accessed
int k=1;
while(k==1){
System.out.println("Press 1 for Insertion in tree");
System.out.println("Press 2 for Inorder traversal in tree");
System.out.println("Press 3 for Preorder traversal in tree");
System.out.println("Press 4 for Postorder traversal in tree");
int choice=sn.nextInt();
switch(choice){
case 1:
bt.insert();
break;
case 2:
bt.Inorder(root);
break;
case 3:
bt.Preorder(root);
break;
case 4:
bt.Postorder(root);
break;
default:
System.out.println("Please enter correct option");
break;
}
System.out.println("Press 0 to terminate or 1 to continue");
k=sn.nextInt();
}
}
}