-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchBinaryTree.java
130 lines (110 loc) · 3.52 KB
/
SearchBinaryTree.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
/*
* SearchBinaryTree
* Copyright (C) 2015, Logan Martel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class SearchBinaryTree {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
// breadth-first search implemented by queue
public void BFS(TreeNode root) {
if (root == null)
return;
queue.clear();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.remove();
System.out.print(node.val + ",");
if (node.left != null)
queue.add(node.left);
if (node.right != null)
queue.add(node.right);
}
}
// build binary tree from inOrder and postOrder traversal input
public TreeNode buildTree(int[] inOrder, int inOrdStart, int inOrdEnd,
int[] postOrder, int postOrdStart, int postOrdEnd) {
if (inOrdStart > inOrdEnd || postOrdStart > postOrdEnd) {
return null;
}
// root = last key in postOrderTraversal
int rootKey = postOrder[postOrdEnd];
TreeNode root = new TreeNode(rootKey);
// find rootKey in inOrderTraversal
int r = 0;
for (int i = 0; i < inOrder.length; i++) {
if (inOrder[i] == rootKey) {
r = i;
break;
}
}
// build left subtree at root's left child
root.left = buildTree(inOrder, inOrdStart, r - 1, postOrder,
postOrdStart, postOrdStart + r - (inOrdStart + 1));
// build right subtree at root's right child
root.right = buildTree(inOrder, r + 1, inOrdEnd, postOrder,
postOrdStart + r - (inOrdStart + 1) + 1, postOrdEnd - 1);
return root;
}
public SearchBinaryTree() {
// line1=inOrderTraversal, line2=postOrderTraversal
String line1 = "";
String line2 = "";
// read traversal results from text file
try {
File file = new File("PracticeInput.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
line1 = bufferedReader.readLine();
line2 = bufferedReader.readLine();
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
// parse inOrderTraversal to int array
String[] ascii = line1.split(",");
int[] inOrder = new int[ascii.length];
for (int i = 0; i < ascii.length; i++) {
inOrder[i] = Integer.parseInt(ascii[i]);
}
// parse postOrderTraversal to int array
ascii = line2.split(",");
int[] postOrder = new int[ascii.length];
for (int i = 0; i < ascii.length; i++) {
postOrder[i] = Integer.parseInt(ascii[i]);
}
// build binary tree from inOrder and postOrder traversal
TreeNode root = buildTree(inOrder, 0, inOrder.length - 1, postOrder, 0,
postOrder.length - 1);
// Run BFS on binary tree and print nodes as visited
BFS(root);
}
public static void main(String args[]) {
// test SearchBinaryTree class
SearchBinaryTree test = new SearchBinaryTree();
}
}