-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.java
73 lines (53 loc) · 1.07 KB
/
BinarySearchTree.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
import java.util.LinkedList;
import java.util.Queue;
public class BinarySearchTree implements Comparable<BinarySearchTree>{
Node root;
public void addNode(String word){
Node newNode = new Node(word);
if(root == null){
root = newNode;
} else {
Node focusNode = root;
Node parent;
while(true){
parent = focusNode;
if(word.compareTo(focusNode.word) < 0){
focusNode = focusNode.left;
if(focusNode == null){
parent.left = newNode;
return;
}
} else if (word.compareTo(focusNode.word) > 0){
focusNode = focusNode.right;
if(focusNode == null){
parent.right = newNode;
return;
}
}
else{
parent.freq = parent.freq + 1;
return;
}
}
}
}
@Override
public int compareTo(BinarySearchTree o) {
return 0;
}
}
class Node{
int freq;
String word;
Node left;
Node right;
public Node(String word){
this.freq = 1;
this.word = word;
left = null;
right = null;
}
public String toString(){
return "word = " + word + " freq = " + freq;
}
}