-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.java
117 lines (103 loc) · 1.6 KB
/
BinaryTree.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
import java.util.Scanner;
class node
{
int data;
node left;
node right;
public node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
public void setData(int data)
{
this.data = data;
}
public int getData()
{
return data;
}
public void setLeft(node n)
{
this.left = n;
}
public node getLeft()
{
return left;
}
public void setRight(node n)
{
this.right = n;
}
public node getRight()
{
return right;
}
}
class tree
{
node root;
public tree()
{
this.root = null;
}
public void insert()
{
node x = new node(4);
node y = new node(5);
node z = new node(6);
x.setLeft(y);
x.setRight(z);
y = new node(7);
z = new node(8);
x.getLeft().setLeft(y);
x.getLeft().setRight(z);
y = new node(9);
z = new node(10);
x.getRight().setLeft(y);
x.getRight().setRight(z);
inOrder(x);
preOrder(x);
postOrder(x);
}
public void inOrder(node n)
{
if(n != null)
{
inOrder(n.getLeft());
System.out.println(n.getData());
inOrder(n.getRight());
}
}
public void preOrder(node n)
{
if(n != null)
{
System.out.println(n.getData());
preOrder(n.getLeft());
preOrder(n.getRight());
}
}
public void postOrder(node n)
{
if(n != null)
{
postOrder(n.getLeft());
postOrder(n.getRight());
System.out.println(n.getData());
}
}
}
class main
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
tree t = new tree();
t.insert();
// t.inOrder();
// t.preOrder();
// t.postOrder();
}
}