/
BinaryTreeExample.java
186 lines (157 loc) · 3.82 KB
/
BinaryTreeExample.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package Btree;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;
public class BinaryTreeExample
{
public static void main(String[] args)
{
new BinaryTreeExample().run();
}
static class Node
{
Node left;
Node right;
int value;
public Node(int value)
{
this.value = value;
}
}
public void run()
{
Node rootnode = new Node(25);
System.out.println("Building tree with rootvalue " + rootnode.value);
System.out.println("=================================");
insertBST(rootnode, 11);
insertBST(rootnode, 9);
insertBST(rootnode, 15);
insertBST(rootnode, 16);
insertBST(rootnode, 23);
insertBST(rootnode, 79);
//System.out.print("\nInfix traversal-");infix(rootnode);
//System.out.print("\nLeastCommonAncestor-"+new BtreeLCA().findLCA(rootnode, new Node(9), new Node(15)).value);
System.out.print("\nBFS-");bfs(rootnode);
System.out.print("\nDFS-"); dfs(rootnode);
/* System.out.print("\nDFS with recursion-"); dfsRec(rootnode); // postfix is same
System.out.println("\nInserting on BTree-"); bfs(insert(rootnode,77));
System.out.println("\nInserting on BTree-"); bfs(insertChg(rootnode,77));*/
}
public Node insert(Node node, int value)
{
if(node==null)
return new Node(value);
else
{
if(node.left==null) // Check is req to avoid duplicate insertion
{
System.out.println("Lnull-"+node.value);
node.left = insert(node.left,value);
}
else
{
System.out.println("!Lnull-"+node.value);
node.right = insert(node.right,value);
}
return node;
}
}
public Node insertChg(Node node, int value)
{
if(node==null)
return new Node(value);
else
{
if(node.right==null)
{
System.out.println("Rnull-"+node.value);
node.right = insertChg(node.right,value);
}
else
{
System.out.println("!Rnull-"+node.value);
node.left = insertChg(node.left,value);
}
return node;
}
}
public void insertBST(Node node, int value)
{
if (value < node.value)
{
if (node.left != null)
{
insertBST(node.left, value);
}
else
{
System.out.println(" Inserted " + value + " to left of node " + node.value);
node.left = new Node(value);
}
}
else if (value > node.value)
{
if (node.right != null)
{
insertBST(node.right, value);
}
else
{
System.out.println(" Inserted " + value + " to right of node " + node.value);
node.right = new Node(value);
}
}
}
public void infix(Node node)
{
if (node != null)
{
infix(node.left);
System.out.print(" " + node.value);
infix(node.right);
}
}
public void bfs(Node rt)
{
Queue<Node> q = new ArrayDeque<Node>();
if(rt != null) q.add(rt);
while(!q.isEmpty())
{
Node tNode = q.remove();
System.out.print(tNode.value+" ");
if(tNode.left!=null)
q.add(tNode.left);
if(tNode.right!=null)
q.add(tNode.right);
}
}
public void dfs(Node rt)
{
Stack<Node> s = new Stack<Node>();
if(rt != null) s.add(rt);
while(!s.isEmpty())
{
Node tNode = s.pop();
System.out.print(tNode.value+" ");
if(tNode.right!=null)
s.add(tNode.right);
if(tNode.left!=null)
s.add(tNode.left);
}
}
public void bfsRec(Node rt)
{
// recursion provides stack which is quiet opposite in property to stack
}
public void dfsRec(Node rt) //prefix traversal & DFS seems same
{
if(rt == null)
return;
else
{
System.out.print(rt.value+" ");
dfsRec(rt.left);
dfsRec(rt.right);
}
}
}