-
Notifications
You must be signed in to change notification settings - Fork 1
/
bstJava.java
174 lines (135 loc) · 3.35 KB
/
bstJava.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
import java.util.LinkedList;
public class bstJava {
public static void main(String args[]) {
BST bst = new BST(10);
Node node = new Node(5);
bst.insert(node);
node = new Node(20);
bst.insert(node);
node = new Node(7);
bst.insert(node);
node = new Node(12);
bst.insert(node);
bst.dfsPrint();
bst.bfsPrint();
LinkedList<LinkedList<Node>> lists = bst.make_level_lists(bst.root);
for (LinkedList<Node> list : lists) {
for (Node traverse : list)
System.out.print(traverse.data + " ");
System.out.println("\n");
}
}
}
class Node {
public int data;
public Node lChild;
public Node rChild;
public Node parent;
public Node (int data) {
this.data = data;
this.lChild = null;
this.rChild = null;
this.parent = null;
}
}
class BST {
Node root;
int nodeCount;
public BST(int rootData) {
root = new Node(rootData);
this.nodeCount = 1;
}
public void insert(Node node) {
insertHelper(this.root, node);
this.nodeCount++;
return;
}
private void insertHelper(Node node, Node toBeInsert) {
if (toBeInsert.data <= node.data) { // insert in left subtree
if (null != node.lChild) {
insertHelper(node.lChild, toBeInsert);
} else {
node.lChild = toBeInsert;
toBeInsert.parent = node;
}
} else { // insert in right subtree
if (null != node.rChild) {
insertHelper(node.rChild, toBeInsert);
} else {
node.rChild = toBeInsert;
toBeInsert.parent = node;
}
}
return;
}
/*
public void delete(Node node, int target) {
if (target == node.data) {
if (node.rChild == null) && (node.lChild != null)
if (null != node.parent)
node.parent. = node.lChild;
else if (node.lChild == null) && (node.rChild != null))
node = node.rChild;
else if (node.lChild == null) && (node.rChild == null)
node = null;
}
deleteHelper(root, target);
return;
}
private void deleteHelper (Node node, int target) {
if (target == node.data) {
}
}
*/
public void dfsPrint() {
if (null != this.root)
dfsPrintHelper(root);
System.out.println("\n" + this.nodeCount + " nodes\n");
return;
}
private void dfsPrintHelper(Node node) {
if (null == node)
return;
if (null != node.lChild)
dfsPrintHelper(node.lChild);
System.out.print(node.data + " ");
if (null != node.rChild)
dfsPrintHelper(node.rChild);
return;
}
public void bfsPrint() {
if (null == this.root)
return;
LinkedList<Node> queue= new LinkedList<>();
queue.addFirst(this.root);
while (0 < queue.size()) {
Node node = queue.removeLast();
System.out.print(node.data + " ");
if (null != node.lChild)
queue.addFirst(node.lChild);
if (null != node.rChild)
queue.addFirst(node.rChild);
}
System.out.print("\n" + this.nodeCount + " nodes.\n");
return;
}
public LinkedList<LinkedList<Node>> make_level_lists(Node root) {
if (null == root)
return null;
LinkedList<LinkedList<Node>> lists = new LinkedList<LinkedList<Node>>();
LinkedList<Node> list = new LinkedList<Node>();
list.add(root);
while (0 < list.size()) {
lists.add(list);
LinkedList<Node> parents = list;
list = new LinkedList<Node>();
for (Node parent : parents) {
if (null != parent.lChild)
list.add(parent.lChild);
if (null != parent.rChild)
list.add(parent.rChild);
}
}
return lists;
}
}