-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.java
182 lines (162 loc) · 5.25 KB
/
BST.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package programming4;
/**
*
* @author austindase
*/
public class BST {
static String name;
node root;
public BST(String str){
name = str;
root = null;
}
public String getMin(){
String min = null;
return min;
}
public String getMax(){
String max = null;
return max;
}
public void insert(node newNode){
node currentNode = root;
if (currentNode == null){
root = newNode;
return;
}
String newWord = newNode.word;
int depth = newNode.depth;
while(true){
node parent = currentNode;
String currentWord = currentNode.word;
int compare = newWord.compareTo(currentWord);
if(compare<0){
currentNode = currentNode.leftChild;
depth++;
if(currentNode == null){
newNode.depth = depth;
parent.leftChild = newNode;
return;
}
}
if(compare>0){
depth++;
currentNode = currentNode.rightChild;
if(currentNode == null){
newNode.depth = depth;
parent.rightChild = newNode;
return;
}
}
if (compare == 0){
parent.freq = currentNode.freq +1;
return;
}
}
}
public void delete(node delNode){
node parentNode = findParent(delNode);
node currentNode = root;
node leftChild = delNode.leftChild;
node rightChild = delNode.rightChild;
if (leftChild == null && rightChild == null){
}
}
public boolean find(node findNode){
node currentNode = root;
String findWord = findNode.word;
String currentWord;
while(currentNode!=null){
currentWord = currentNode.word;
int compare = findWord.compareTo(currentWord);
if(compare == 0){
return true;
}else if(compare < 0){
currentNode = currentNode.leftChild;
}else{
currentNode = currentNode.rightChild;
}
}
return false;
}
public node findParent(node findNode){
node currentNode = root;
node parent = null;
String findWord = findNode.word;
String currentWord;
while(currentNode!=null){
parent = currentNode;
currentWord = currentNode.word;
int compare = findWord.compareTo(currentWord);
if(compare == 0){
return parent;
}else if(compare < 0){
currentNode = currentNode.leftChild;
}else{
currentNode = currentNode.rightChild;
}
}
return currentNode;
}
// public void printBST(){
// node currentNode = root;
// String findWord = findNode.word;
// String currentWord;
// int n = 1;
// while(currentNode!=null){
//
// currentWord = currentNode.word;
// node leftChild = currentNode.leftChild;
// node rightChild = currentNode.rightChild;
// String leftChildWord = leftChild.word;
// String rightChildWord = rightChild.word;
// int frequency = currentNode.freq;
//
// System.out.println("node: " + currentWord +" freq: "+ frequency + " leftChild: "+ leftChildWord + " rightChild: "+ rightChildWord);
// int compare = findWord.compareTo(currentWord);
// if(compare == 0){
// continue;
// }else if(compare < 0){
// currentNode = currentNode.leftChild;
// }else{
// currentNode = currentNode.rightChild;
// }
// n++;
// }
// System.out.println("Tree has " + n + " nodes.");
// }
//
public void preOrder(node root) {
if(root != null) {
//Visit the node by Printing the node data
//System.out.println(root.word);
int depth = root.depth;
//System.out.println(depth);
String[] temp = new String[depth];
int count = 0;
String spaces = "";
while(count<depth){
spaces = spaces + " ";
count++;
// System.out.println(count);
}
System.out.println(spaces + root.word);
preOrder(root.leftChild);
preOrder(root.rightChild);
}
}
public void inOrder(node root) {
if(root != null) {
inOrder(root.leftChild);
System.out.println(root.word);
//Visit the node by Printing the node data
//System.out.println(root.word);
inOrder(root.rightChild);
}
}
}