This class represents a binary tree. It is implemented as an array with
pointers to the children.
childCount
public final int childCount(int parent)
Return the number of children for the given node.
parent
- the parent to start counting
- the number of children for the given node
children
public final int[] children(int parent)
Return the children of the given node.
parent
- the parent of the children
- an array of size <= 2 containing the left and the right child
extendOn
public final int extendOn(int depth,
int pos)
Add a new node on depth depth
and position pos
.
depth
- the depth to add a new nodepos
- the position to add a new node
- the parent of the new node, or
-1
when no node could be added
getBinaryCount
public final int getBinaryCount()
Return the current number of binary operators.
getNodeCount
public final int getNodeCount()
Return the current number of nodes, this equals: binaryCount * 2 + unaryCount + 1
.
getUnaryCount
public final int getUnaryCount()
Return the current number of unary operators.
hasLeftChild
public final boolean hasLeftChild(int parent)
Does the node have a left child?
parent
- parent node nummber
true
when parent
has a left child or false otherwise
hasRightChild
public final boolean hasRightChild(int parent)
Does the node have a right child?
parent
- parent node nummber
true
when parent
has a right child or false otherwise
leftChild
public final int leftChild(int parent)
Return the left child of the given node.
parent
- the parent of the child
- the left child of the parent
newLeftChild
public final int newLeftChild(int parent)
Create a new left child for the given node.
parent
- the node that will get a new left child
newRightChild
public final int newRightChild(int parent)
Create a new right child for the given node.
parent
- the node that will get a new right child
- the new child or
-1
if there is no left child
nodesonlevel
public final int nodesonlevel(int depth)
Return the number of nodes on the given depth.
depth
- the depth of the tree
- the number of nodes on the given depth
removeLeftChild
public final void removeLeftChild(int parent)
Remove the left child of the given node.
parent
- the parent of which the left child should be removed
removeOn
public final void removeOn(int depth,
int pos,
int parent)
Remove the node on the given depth
and pos
with the given parent
.
removeRightChild
public final void removeRightChild(int parent)
Remove the right child of the given node.
parent
- the parent of which the right child should be removed
rightChild
public final int rightChild(int parent)
Return the right child of the given node.
parent
- the parent of the child
- the right child of the parent
toString
public String toString()
toString
protected String toString(int parent)