-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBTree.java
More file actions
232 lines (150 loc) · 5.15 KB
/
BTree.java
File metadata and controls
232 lines (150 loc) · 5.15 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package Flawless_Feedback;
//Source: http://www.newthinktank.com/2013/03/binary-tree-in-java/
// New Think Tank
public class BTree {
BNode root;
String temp = "";
public void addNode(int key, String name) {
// Create a new Node and initialize it
BNode newNode = new BNode(key, name);
// If there is no root this becomes root
if (root == null) {
root = newNode;
} else {
// Set root as the Node we will start
// with as we traverse the tree
BNode focusNode = root;
// Future parent for our new Node
BNode parent;
while (true) {
// root is the top parent so we start
// there
parent = focusNode;
// Check if the new node should go on
// the left side of the parent node
if (key < focusNode.key) {
// Switch focus to the left child
focusNode = focusNode.leftChild;
// If the left child has no children
if (focusNode == null) {
// then place the new node on the left of it
parent.leftChild = newNode;
return; // All Done
}
} else { // If we get here put the node on the right
focusNode = focusNode.rightChild;
// If the right child has no children
if (focusNode == null) {
// then place the new node on the right of it
parent.rightChild = newNode;
return; // All Done
}
}
}
}
}
// All nodes are visited in ascending order
// Recursion is used to go to one node and
// then go to its child nodes and so forth
public String inOrderTraverseTree(BNode focusNode) {
temp = "";
if (focusNode != null) {
// Traverse the left node
temp += inOrderTraverseTree(focusNode.leftChild);
// Visit the currently focused on node
temp += focusNode + ", ";
// Traverse the right node
temp += inOrderTraverseTree(focusNode.rightChild);
return temp;
}
return "";
}
public String preorderTraverseTree(BNode focusNode) {
temp = "";
if (focusNode != null) {
temp += focusNode + ", ";
temp += preorderTraverseTree(focusNode.leftChild);
temp += preorderTraverseTree(focusNode.rightChild);
return temp;
}
return "";
}
public String postOrderTraverseTree(BNode focusNode) {
temp = "";
if (focusNode != null) {
temp += postOrderTraverseTree(focusNode.leftChild);
temp += postOrderTraverseTree(focusNode.rightChild);
temp += focusNode + ", ";
return temp;
}
return "";
}
public BNode findNode(int key) {
// Start at the top of the tree
BNode focusNode = root;
// While we haven't found the Node
// keep looking
while (focusNode.key != key) {
// If we should search to the left
if (key < focusNode.key) {
// Shift the focus Node to the left child
focusNode = focusNode.leftChild;
} else {
// Shift the focus Node to the right child
focusNode = focusNode.rightChild;
}
// The node wasn't found
if (focusNode == null)
return null;
}
return focusNode;
}
}
// public static void main(String[] args) {
//
// BinaryTree theTree = new BinaryTree();
//
// theTree.addNode(50, "Boss");
//
// theTree.addNode(25, "Vice President");
//
// theTree.addNode(15, "Office Manager");
//
// theTree.addNode(30, "Secretary");
//
// theTree.addNode(75, "Sales Manager");
//
// theTree.addNode(85, "Salesman 1");
//
// // Different ways to traverse binary trees
//
// // theTree.inOrderTraverseTree(theTree.root);
//
// // theTree.preorderTraverseTree(theTree.root);
//
// // theTree.postOrderTraverseTree(theTree.root);
//
// // Find the node with key 75
//
// System.out.println("\nNode with the key 75");
//
// System.out.println(theTree.findNode(75));
//
// }
class BNode {
int key;
String name;
BNode leftChild;
BNode rightChild;
BNode(int key, String name) {
this.key = key;
this.name = name;
}
public String toString() {
return key + "-" + name;
/*
* return name + " has the key " + key + "\nLeft Child: " + leftChild +
* "\nRight Child: " + rightChild + "\n";
*/
}
}