Class IntAVLTree
java.lang.Object
com.tdunning.math.stats.IntAVLTree
- All Implemented Interfaces:
Serializable
An AVL-tree structure stored in parallel arrays.
This class only stores the tree structure, so you need to extend it if you
want to add data to the nodes, typically by using arrays and node
identifiers as indices.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classA stack of int values.private static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate byte[]private int[]protected static final intWe use 0 instead of -1 so that left(NIL) works without condition.private final IntAVLTree.NodeAllocatorprivate int[]private int[]private int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd()Add current data to the tree and return true if a new node was added to the tree or false if the node was merged into an existing node.private intbalanceFactor(int node) intcapacity()Return the current capacity, which is the number of nodes that this tree can hold.(package private) voidcheckBalance(int node) protected abstract intcompare(int node) Compare data against data which is stored innode.protected abstract voidcopy(int node) Compare data intonode.intdepth(int node) Return the depth nodes that are stored belownodeincluding itself.private voiddepth(int node, int depth) intfind()Find a node in this tree.intfirst(int node) Return the least node undernode.protected voidfixAggregates(int node) intlast(int node) Return the largest node undernode.intleft(int node) Return the left child of the provided node.private voidleft(int node, int left) protected abstract voidmerge(int node) Merge data intonode.final intnext(int node) Return the least node that is strictly greater thannode.(package private) static intoversize(int size) Grow a size by 1/8.intparent(int node) Return the parent of the provided node.private voidparent(int node, int parent) final intprev(int node) Return the highest node that is strictly less thannode.private voidrebalance(int node) private voidrelease(int node) voidremove(int node) Remove the specified node from the tree.protected voidresize(int newCapacity) Resize internal storage in order to be able to store data for nodes up tonewCapacity(excluded).intright(int node) Return the right child of the provided node.private voidright(int node, int right) introot()Return the current root of the tree.private voidrotateLeft(int n) Rotate left the subtree undernprivate voidrotateRight(int n) Rotate right the subtree undernintsize()Return the size of this tree.private voidswap(int node1, int node2) voidupdate(int node) Updatenodewith the current data.
-
Field Details
-
NIL
protected static final int NILWe use 0 instead of -1 so that left(NIL) works without condition.- See Also:
-
nodeAllocator
-
root
private int root -
parent
private int[] parent -
left
private int[] left -
right
private int[] right -
depth
private byte[] depth
-
-
Constructor Details
-
IntAVLTree
IntAVLTree(int initialCapacity) -
IntAVLTree
IntAVLTree()
-
-
Method Details
-
oversize
static int oversize(int size) Grow a size by 1/8. -
root
public int root()Return the current root of the tree. -
capacity
public int capacity()Return the current capacity, which is the number of nodes that this tree can hold. -
resize
protected void resize(int newCapacity) Resize internal storage in order to be able to store data for nodes up tonewCapacity(excluded). -
size
public int size()Return the size of this tree. -
parent
public int parent(int node) Return the parent of the provided node. -
left
public int left(int node) Return the left child of the provided node. -
right
public int right(int node) Return the right child of the provided node. -
depth
public int depth(int node) Return the depth nodes that are stored belownodeincluding itself. -
first
public int first(int node) Return the least node undernode. -
last
public int last(int node) Return the largest node undernode. -
next
public final int next(int node) Return the least node that is strictly greater thannode. -
prev
public final int prev(int node) Return the highest node that is strictly less thannode. -
compare
protected abstract int compare(int node) Compare data against data which is stored innode. -
copy
protected abstract void copy(int node) Compare data intonode. -
merge
protected abstract void merge(int node) Merge data intonode. -
add
public boolean add()Add current data to the tree and return true if a new node was added to the tree or false if the node was merged into an existing node. -
find
public int find()Find a node in this tree. -
update
public void update(int node) Updatenodewith the current data. -
remove
public void remove(int node) Remove the specified node from the tree. -
release
private void release(int node) -
swap
private void swap(int node1, int node2) -
balanceFactor
private int balanceFactor(int node) -
rebalance
private void rebalance(int node) -
fixAggregates
protected void fixAggregates(int node) -
rotateLeft
private void rotateLeft(int n) Rotate left the subtree undern -
rotateRight
private void rotateRight(int n) Rotate right the subtree undern -
parent
private void parent(int node, int parent) -
left
private void left(int node, int left) -
right
private void right(int node, int right) -
depth
private void depth(int node, int depth) -
checkBalance
void checkBalance(int node)
-