T
- any Java object that will be sampledpublic class StochasticTree<T>
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
class |
StochasticTree.STNode
A class for storing a stochastic tree node.
|
Modifier and Type | Field and Description |
---|---|
protected java.util.Map<T,StochasticTree.STNode> |
nodeMap
A map from elements to the node that holds them.
|
protected java.util.Random |
rand
A random object used for sampling.
|
protected StochasticTree.STNode |
root
Root node of the stochastic tree
|
Constructor and Description |
---|
StochasticTree()
Initializes with an empty tree.
|
StochasticTree(java.util.List<java.lang.Double> weights,
java.util.List<T> elements)
Initializes a tree for objects with the given weights
|
Modifier and Type | Method and Description |
---|---|
void |
changeWeight(T element,
double w)
Changes the weight of the given element.
|
T |
getStoredEntry(T el)
Returns the pointer to the stored entry in this tree for the given query element.
|
protected void |
init()
Initializes the three data structure
|
void |
insert(double w,
T el)
Inserts the given element into the tree with the given weight
|
protected void |
insertHelper(StochasticTree.STNode node,
double w,
T el)
Helper recursive method for inserting an element
|
static void |
main(java.lang.String[] args)
Demos how to use this class
|
protected void |
percolateWeightChange(StochasticTree.STNode node,
double delta)
A recursive method for percolating a weight change of a node
|
T |
poll()
Samples an element according to a probability defined by the relative weight of objects, removes it from the tree, and returns it.
|
void |
remove(T element)
Removes the given element from the tree.
|
protected void |
removeHelper(StochasticTree.STNode node)
A recursive method for removing a node
|
T |
sample()
Samples an element according to a probability defined by the relative weight of objects from the tree and returns it
|
protected StochasticTree.STNode |
sampleHelper(StochasticTree.STNode node,
double v)
A recursive method for performing sampling
|
void |
setRandom(java.util.Random r)
Sets the tree to use a specific random object when performing sampling
|
int |
size()
Returns the number of objects in this tree
|
static void |
test1()
Another example usage
|
static void |
test2()
An example usage
|
protected StochasticTree.STNode root
protected java.util.Map<T,StochasticTree.STNode> nodeMap
protected java.util.Random rand
public StochasticTree()
public StochasticTree(java.util.List<java.lang.Double> weights, java.util.List<T> elements)
weights
- the weights of a set objects that determine how likely they are to be sampledelements
- the elements of the tree that will be sampledprotected void init()
public void setRandom(java.util.Random r)
r
- the random generator to usepublic int size()
public T getStoredEntry(T el)
el
- the element whose stored object in the tree is to be returnedpublic void insert(double w, T el)
w
- the weight of the elementel
- the element to insertpublic void changeWeight(T element, double w)
element
- the element whose weight should be changed.w
- the new weight of the element.public void remove(T element)
element
- the element to removepublic T sample()
public T poll()
protected void insertHelper(StochasticTree.STNode node, double w, T el)
node
- the node from which to insert the elementw
- the weight of the elementel
- the element to be insertedprotected void percolateWeightChange(StochasticTree.STNode node, double delta)
node
- the node whose weight change should be percolateddelta
- the change in weightprotected void removeHelper(StochasticTree.STNode node)
node
- the node to be removedprotected StochasticTree.STNode sampleHelper(StochasticTree.STNode node, double v)
node
- the node from which to samplev
- the random value used to determine which direction to gopublic static void main(java.lang.String[] args)
args
- emptypublic static void test2()
public static void test1()