blob: 3545ff95639af2cc621b7d255d8b08d844f0063e (
plain)
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
|
public class FrequencyBag<T>
{
// TO DO: Instance Variables
private FNode head;
private boolean hasChanged = true;
private int max;
//Tracks the number of times add has been called
private int total;
/**
* Constructor
* Constructs an empty frequency bag.
*/
public FrequencyBag()
{
head = null;
}
/**
* Adds new entry into this frequency bag.
* @param aData the data to be added into this frequency bag.
*/
public void add(T aData)
{
//Add to the start to make add an O(1) operation
if(!addhelper(head,aData))
{
hasChanged = true;
FNode n = new FNode(aData,head);
head = n;
}
total++;
}
public boolean addhelper(FNode head, T data)
{
if(head == null)
{
//We haven't found the item in the list, return false
return false;
}
if(head.getObject().equals(data))
{
head.add();
return true;
}
return addhelper(head.getNext(),data);
}
/**
* Gets the number of occurrences of aData in this frequency bag.
* @param aData the data to be checked for its number of occurrences.
* @return the number of occurrences of aData in this frequency bag.
*/
public int getFrequencyOf(T aData)
{
return getFrequencyOfHelper(head,aData);
}
public int getFrequencyOfHelper(FNode<T> node,T data)
{
//If we've hit a null node, the object isn't in the list
if(node == null)
{
return 0;
}
if(node.getObject().equals(data))
{
return node.getFrequency();
}
else
{
return getFrequencyOfHelper(node.getNext(),data);
}
}
/**
* Gets the maximum number of occurrences in this frequency bag.
* @return the maximum number of occurrences of an entry in this
* frequency bag.
*/
public int getMaxFrequency()
{
if(hasChanged)
{
hasChanged = false;
max = getMaxFrequencyHelper(head);
return max;
}
else
{
return max;
}
}
private int getMaxFrequencyHelper(FNode<T> node)
{
if(node == null) //Were at the end
return 0;
int nmax = getMaxFrequencyHelper(node.getNext());
return node.getFrequency() > nmax?node.getFrequency():nmax;
}
/**
* Gets the probability of aData
* @param aData the specific data to get its probability.
* @return the probability of aData
*/
public double getProbabilityOf(T aData)
{
return getFrequencyOf(aData)/(total*1.0);
}
/**
* Empty this bag.
*/
public void clear()
{
head = null;
hasChanged = true;
total = 0;
}
/**
* Gets the number of entries in this bag.
* @return the number of entries in this bag.
*/
public int size()
{
return total;
}
public class FNode<T>
{
private T object = null;
private int frequency = 1;
private FNode next = null;
public FNode()
{
}
public FNode(T obj,FNode n)
{
object = obj;
next = n;
}
public void setObject(T obj)
{
object = obj;
}
public T getObject()
{
return object;
}
public void add()
{
frequency++;
}
public int getFrequency()
{
return frequency;
}
public FNode getNext()
{
return next;
}
public void setNext(FNode n)
{
next = n;
}
}
}
|