-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTree.java
More file actions
168 lines (148 loc) · 4.9 KB
/
BinaryTree.java
File metadata and controls
168 lines (148 loc) · 4.9 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BinaryTree {
private TreeNode root; // null<--Root Node-->null
// this the top node of the binary tree.
private class TreeNode{ // Class to represent the node in binary tree
private TreeNode left;
private TreeNode right;
private int data; //Generic Type
public TreeNode(int data){
this.data = data;
}
}
public void createBinaryTree(){
TreeNode first = new TreeNode(9);
TreeNode second = new TreeNode(2);
TreeNode third = new TreeNode(3);
TreeNode fourth = new TreeNode(4);
TreeNode fifth = new TreeNode(5);
TreeNode sixth = new TreeNode(6);
root = first; // null<--First-->null
first.left = second; // second<--First-->null
first.right = third; // second<--First-->third
second.left = fourth; // fourth<--second-->null
third.right = fifth; // null<--third-->fifth
third.left = sixth; // sixth<--third-->>fifth
}
// to create preorder of the tree which first visit the left node and then right node
public void preOrder(TreeNode root){
if(root == null) //base case
return;
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
public void preOrderIterative(TreeNode root){
if (root == null)
return;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()){
TreeNode temp = stack.pop();
System.out.print(temp.data + "-->");
if (temp.right != null){
stack.push(temp.right);
}
if (temp.left != null){
stack.push(temp.left);
}
}
}
public void inOrder(TreeNode root){
if (root == null)
return;
inOrder(root.left);
System.out.print(root.data+"-->");
inOrder(root.right);
}
public void inOderIterative(TreeNode root){
if (root == null)
return;
Stack<TreeNode> stack = new Stack<>();
TreeNode temp = root;
while (!stack.isEmpty() || temp != null){
if (temp != null){
stack.push(temp);
temp = temp.left;
} else {
temp = stack.pop();
System.out.print(temp.data+"-->");
temp = temp.right;
}
}
}
public void postOrder(TreeNode root){
if (root == null)
return;
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data+"-->");
}
public void postOrderIterative(TreeNode root){
if(root == null)
return;
TreeNode current = root;
Stack<TreeNode> stack = new Stack<>();
while(current != null || !stack.isEmpty()){
if(current != null){
stack.push(current);
current = current.left;
} else {
TreeNode temp = stack.peek().right;
if(temp == null){
temp = stack.pop();
System.out.print(temp.data+"-->");
while (!stack.isEmpty() && temp == stack.peek().right){
temp = stack.pop();
System.out.print(temp.data+"-->");
}
} else {
current = temp;
}
}
}
}
public void levelOrder(TreeNode root){
if(root == null)
return;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()){
TreeNode temp = queue.poll();
System.out.print(temp.data+"-->");
if (temp.left != null){
queue.offer(temp.left);
}
if (temp.right != null){
queue.offer(temp.right);
}
}
}
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
bt.createBinaryTree();
System.out.println("Pre order");
bt.preOrder(bt.root);
System.out.println();
System.out.println("using Iterative");
bt.preOrderIterative(bt.root);
System.out.println();
System.out.println("in Order");
bt.inOrder(bt.root);
System.out.println();
System.out.println("in Order using Iterative");
bt.inOderIterative(bt.root);
System.out.println();
System.out.println("post Order");
bt.postOrder(bt.root);
System.out.println();
System.out.println("post Order using Iterative");
bt.postOrderIterative(bt.root);
System.out.println();
System.out.println("level order tree");
bt.levelOrder(bt.root);
System.out.println();
}
}