-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordStoreImp.java
151 lines (141 loc) · 3.75 KB
/
WordStoreImp.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
class WordStoreImp implements WordStore
{
Node rootNode;
public WordStoreImp(int n)
{
rootNode = null;
}
public void add(String word)
{
rootNode = addNode(word, rootNode);
}
public int count(String word)
{
return countNode(word, rootNode);
}
public void remove(String word)
{
removeNode(word, rootNode);
}
private Node addNode(String word, Node rootNode)
{
if(rootNode == null)
{
rootNode = new Node(word);
}
else if(word.equals(rootNode.data))
{
rootNode.countWord++;
}
else if(word.compareTo(rootNode.data) < 0)
{
rootNode.left = addNode(word, rootNode.left);
}
else
{
rootNode.right = addNode(word, rootNode.right);
}
return rootNode;
}
private int countNode(String word, Node rootNode)
{
if(rootNode == null)
{
return 0;
}
else if(word.equals(rootNode.data))
{
return rootNode.countWord;
}
else if(word.compareTo(rootNode.data) <= 0)
{
return countNode(word, rootNode.left);
}
else
{
return countNode(word, rootNode.right);
}
}
private Node removeNode(String word, Node rootNode)
{
if(rootNode == null)
{
return null;
}
if(word.equals(rootNode.data) && rootNode.countWord > 1)
{
rootNode.countWord--;
}
else if(word.equals(rootNode.data) && rootNode.countWord == 1)
{
if(rootNode.left != null && rootNode.right != null)//Node has 2 children
{
rootNode.data = min(rootNode.right).data;//minimum element used to replace the root node
removeNode(rootNode.data, rootNode.right);//minimum node removed
}
else if(rootNode.left != null && rootNode.right == null)//Node has 1 child on the left
{
rootNode.data = rootNode.left.data;
rootNode.countWord = rootNode.left.countWord;
rootNode.left = null;
}
else if(rootNode.right != null && rootNode.left == null)//Node has one child on the right
{
rootNode.data = rootNode.right.data;
rootNode.countWord = rootNode.right.countWord;
rootNode.right = null;
}
else//Node has 0 children
{
rootNode = null;
}
}
else if(word.compareTo(rootNode.data) < 0)
{
rootNode.left = removeNode(word, rootNode.left);
}
else
{
rootNode.right = removeNode(word, rootNode.right);
}
return rootNode;
}
private Node min(Node node)//finds the smallest elements node in a part of the tree and returns it. Only needs to search the left side of the given node as smallest elements are on the left.
{
if(node.left == null)
{
return node;
}
else
{
return min(node.left);
}
}
private Node getNode()
{
return rootNode;
}
public static void print(Node node)
{
if(node != null)
{
print(node.left);
System.out.print(node.data + " ");
print(node.right);
}
}
private class Node
{
public String data;
public Node left;
public Node right;
public int countWord;
public Node(String word)
{
this.data = word;
left = null;
right = null;
countWord = 1;
}
}
}