-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
149 lines (129 loc) · 2.92 KB
/
BinaryTree.java
File metadata and controls
149 lines (129 loc) · 2.92 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
package technical;
import java.util.Stack;
public class BinaryTree {
Treenode root;
BinaryTree() {
this.root = null;
}
public class Treenode {
private Treenode left;
private Treenode right;
private int data;
public Treenode(int data) {
this.data = data;
}
}
public void createBinaryTree() {
Treenode first = new Treenode(22);
Treenode second = new Treenode(33);
Treenode third = new Treenode(11);
Treenode fourth = new Treenode(42);
Treenode fifth = new Treenode(26);
root = first;
first.left = second;
first.right = third;
second.left = fourth;
second.right = fifth;
}
public void preorder(Treenode root) {
if (root == null) {
return;
}
System.out.print(root.data + " ");
preorder(root.left);
preorder(root.right);
}
public void Inorder(Treenode root) {
if (root == null) {
return;
}
preorder(root.left);
System.out.print(root.data + " ");
preorder(root.right);
}
public void postorder(Treenode root) {
if (root == null) {
return;
}
preorder(root.left);
preorder(root.right);
System.out.print(root.data + " ");
}
// preorder iterative
public void preorderit(Treenode root) {
if (root == null) {
return;
}
Stack<Treenode> s = new Stack<>();
s.push(root);
while (!s.isEmpty()) {
Treenode temp = s.pop();
System.out.print(temp.data + " ");
if (temp.left != null) {
s.push(temp.left);
}
if (temp.right != null) {
s.push(temp.right);
}
}
}
// Inorder iterative
public void inorderit(Treenode root) {
if(root==null) return;
Stack<Treenode> stack=new Stack<>();
Treenode temp=root;
while(temp!=null || !stack.isEmpty()) {
if(temp!=null) {
stack.push(temp);
temp=temp.left;
}else {
temp=stack.pop();
System.out.print(temp.data+" ");
temp=temp.right;
}
}
public static int maxtree(Treenode root) {
if (root == null) {
return Integer.MIN_VALUE;
}
int result = root.data;
int left = maxtree(root.left);
int right = maxtree(root.right);
if (result < left) {
result = left;
}
if (result < right) {
result = right;
}
return result;
}
public void levelorder(Treenode root) {
if (root == null) {
return;
}
Queue<Treenode> q = new LinkedList<>();
q.offer(root);
while (!q.isEmpty()) {
Treenode temp = q.poll();
System.out.print(temp.data + " ");
if (temp.left != null) {
q.offer(temp.left);
}
if (temp.right != null) {
q.offer(temp.right);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryTree b = new BinaryTree();
b.createBinaryTree();
b.preorder(b.root);
System.out.println();
b.preorderit(b.root);
System.out.println("Post order");
b.postorder(b.root);
System.out.println("In order");
b.Inorder(b.root);
}
}