Class TreeIndex<N extends AbstractNode>
(level, index).
Levels are counted up from the leaf nodes, with the leaves at level zero;
index is just an index into nodes at that level. Instances are immutable and
safe under concurrent access.
Terminology
Most expositions use the term "promoted" to describe how the last odd node at a level is stitched up the higher node levels to root. (Note in our zero-based index, the index of this last odd node is in fact even; the count is odd.) Here, we use the term carry to represent this special type of joining of two lower level nodes to form a higher level parent. Except for the edge case involving such carries, a node's children are always at adjacent indices at the level just below; for carries, however, a node's children may be from different levels (with the left child always at the same or higher level than the right child).
- Carry. The parent node formed from 2 child nodes at different levels, or a parent node formed if one or more of its descendants have been so formed. There can only be one such node at any level, and then it may only be the node at the last index at that level. Another way to think of a carry is an existing node whose value would necessarily change if another (single) leaf node had been added.
- Joins Carry. A child node of a carry. The child node itself may or may not be a carry.
Note, the root node is itself usually a carry. The only times it's not is when the number of leaves is an exact power of 2 in which case there are no carries in the tree. Conversely, if a node is a carry, then every ancestor of it (including the root node), every is another carry.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceTreeIndex.NodeFactory<N extends AbstractNode>A node factory for aTreeIndex. -
Constructor Summary
ConstructorsConstructorDescriptionTreeIndex(int count, TreeIndex.NodeFactory<N> factory) Creates a type-specific instance with the given factory. -
Method Summary
Modifier and TypeMethodDescriptionfinal intcount()Returns the number leaf nodes (data items) in the tree.final intcount(int level) Returns the number of nodes at the givenlevel.final intcountSansCarry(int level) Returns the numbern of nodes at the givenlevelexcluding the carry (if it has one).final booleanDetermines whether an instance is structurally equivalent to another.Returns the frontier nodes.final NgetLeftChild(int level, int index) Returns the left child of the internal node at the given coordinates.final NgetLeftChild(AbstractNode parent) Returns the left child of the given parent node.final NgetNode(int serialIndex) Returns the node at the given serial index.final NgetNode(int level, int index) Returns the node at the given coordinates.final NgetParent(int level, int index) Returns the parent of the node at the given coordinates.final NgetParent(AbstractNode node) Returns the given node's parent.final NgetRightChild(int level, int index) Returns the right child of the internal node at the given coordinates.final NgetRightChild(AbstractNode parent) Returns the right child of the given parent node.final NgetSibling(int level, int index) Returns the sibling of the node at the given coordinates.final NgetSibling(AbstractNode node) Returns the given node's sibling.final booleanhasCarry(int level) Determines whether the last node is a carry.final inthashCode()The instance's hash code is just the number of its leaves.final intheight()Returns the height of the root of the tree relative to its base (the leaves).final booleanisCarry(int level, int index) Determines whether node at the given coordinates is a carry.final booleanisLeft(int level, int index) Determines if the node at the given coordinates is the left child of its parent node.final booleanisRight(int level, int index) Determines if the node at the given coordinates is the right child of its parent node.final booleanisRoot(AbstractNode node) Determines whether the given node is this tree's root node.final intmaxIndex(int level) Returns the maximum allowed index at the givenlevel.final booleanmaxIndexJoinsCarry(int level) Determines whether there are an odd number of nodes at thislevel(after accounting for the carries).static TreeIndex<?> newGeneric(int count) Creates and returns a purely structural tree index (the tree sans data, only node coordinates).static introotHeightForCount(int count) Returns the height of the Merkle tree root node with count-many leaf elements.final intserialIndex(int level, int index) Returns the serial index of the node at the given cooridinate.toString()final intReturns the total number of carries (otherwise known as promoted nodes).final intReturns the total number of nodes in the tree.final intReturns the total number of nodes in the tree excluding the carries.
-
Constructor Details
-
TreeIndex
Creates a type-specific instance with the given factory.- Parameters:
count- the number of items (leaf nodes) in the tree (≥ 2)factory- the node factory controls the return type<N>- See Also:
-
-
Method Details
-
newGeneric
Creates and returns a purely structural tree index (the tree sans data, only node coordinates). It has a miniscule memory footprint, no matter how large the tree.- Parameters:
count- the number items (leaves) in the tree (≥ 2)- Returns:
new TreeIndex<>(count, AbstractNode.FACTORY)
-
count
public final int count()Returns the number leaf nodes (data items) in the tree. An instance's count uniquely determines its structure. -
totalCount
public final int totalCount()Returns the total number of nodes in the tree.- Returns:
2 * count() - 1
-
totalCarries
public final int totalCarries()Returns the total number of carries (otherwise known as promoted nodes). -
totalCountSansCarries
public final int totalCountSansCarries()Returns the total number of nodes in the tree excluding the carries.- Returns:
- the difference of totalCount() and totalCarries()
-
serialIndex
Returns the serial index of the node at the given cooridinate. A node's serial index is the node's index in a breadth-first traversal of the tree, starting with the tree's root node indexed at zero.- Parameters:
level- ≥ 0 and ≤height()index- ≥ 0 and <count(level)- Throws:
IndexOutOfBoundsException- See Also:
-
height
public final int height()Returns the height of the root of the tree relative to its base (the leaves). In this terminology, the leaves are at level zero, and the root is at the level with maximum height.- See Also:
-
count
Returns the number of nodes at the givenlevel.- Parameters:
level- ≥ 0 and ≤height()- Throws:
IndexOutOfBoundsException
-
countSansCarry
public final int countSansCarry(int level) Returns the numbern of nodes at the givenlevelexcluding the carry (if it has one). -
maxIndex
Returns the maximum allowed index at the givenlevel.- Returns:
- count(level) - 1
- Throws:
IndexOutOfBoundsException
-
maxIndexJoinsCarry
Determines whether there are an odd number of nodes at thislevel(after accounting for the carries). If so, then the last node at this level joins the next unpaired node at a higher level to form a parent node one level above that next unpaired node.- Parameters:
level- ≥ 0 and ≤height()- Returns:
trueiff there are an odd number of nodes at this level- Throws:
IndexOutOfBoundsException
-
hasCarry
Determines whether the last node is a carry. There is at most one carry in each level.- Parameters:
level- ≥ 0 and ≤height()- Throws:
IndexOutOfBoundsException
-
isCarry
public final boolean isCarry(int level, int index) Determines whether node at the given coordinates is a carry.- Parameters:
level- ≥ 0 and ≤height()index- ≥ 0 and <count(level)- See Also:
-
isRoot
Determines whether the given node is this tree's root node.- Returns:
node.level() == height()- See Also:
-
getNode
Returns the node at the given coordinates.- Parameters:
level- ≥ 0 and ≤height()index- ≥ 0 and <count(level)- Throws:
IndexOutOfBoundsException- See Also:
-
getNode
Returns the node at the given serial index.- Parameters:
serialIndex- ≥ 0 and <totalCount()- Throws:
IndexOutOfBoundsException- See Also:
-
getParent
Returns the given node's parent. Convenience method.- Throws:
IndexOutOfBoundsException- See Also:
-
getParent
Returns the parent of the node at the given coordinates.- Parameters:
level- ≥ 0 and <height()index- ≥ 0 and <count(level)- Throws:
IndexOutOfBoundsException- See Also:
-
getLeftChild
Returns the left child of the given parent node.- Parameters:
parent- an internal (non-leaf) node- Throws:
IndexOutOfBoundsException
-
getLeftChild
Returns the left child of the internal node at the given coordinates.- Parameters:
level- ≥ 0 and ≤height()index- ≥ 1 and <count(level)- Throws:
IndexOutOfBoundsException- See Also:
-
getRightChild
Returns the right child of the given parent node.- Parameters:
parent- an internal (non-leaf) node- Throws:
IndexOutOfBoundsException
-
getRightChild
Returns the right child of the internal node at the given coordinates.- Parameters:
level- ≥ 0 and ≤height()index- ≥ 1 and <count(level)- Throws:
IndexOutOfBoundsException- See Also:
-
getSibling
Returns the given node's sibling. Convenience method.- Throws:
IndexOutOfBoundsException- See Also:
-
getSibling
Returns the sibling of the node at the given coordinates. Two nodes are siblings iff they have the same parent node in the tree.- Parameters:
level- 0 ≤ level < height()index- 0 ≤ index < count(level)- Throws:
IndexOutOfBoundsException
-
isRight
Determines if the node at the given coordinates is the right child of its parent node. The root level is defined to be left (tho, in principle, it should be undefined).- Parameters:
level- between zero andheight()(inclusive)index- the zero-based node index at the givenlevel- Throws:
IndexOutOfBoundsException- See Also:
-
isLeft
Determines if the node at the given coordinates is the left child of its parent node. The root level is defined to be left (tho, in principle, it should be undefined).- Parameters:
level- between zero andheight()(inclusive)index- the zero-based node index at the givenlevel- Throws:
IndexOutOfBoundsException- See Also:
-
getFrontier
-
equals
-
hashCode
-
toString
-
rootHeightForCount
Returns the height of the Merkle tree root node with count-many leaf elements. (The height of the leaves is zero.)- Parameters:
count- ≥ 2- Returns:
ceil(log2(count)- Throws:
IllegalArgumentException
-