Java Data Structures - Contents
A D V E R T I S E M E N T
As with everything else, there are TONS of ways to
write exactly the same thing! With nodes, there is no exception. I prefer to implement
node-pool nodes with integer children; where the integer points to the next child inside
the node-pool array. This may not be the best Object Oriented way to do this,
since it's binding the node-pool node to the node-pool tree. However, there are many other
solutions to this, and many are more "elegant" than the one I'll be
using here. So, if you don't like it, implement your own version!
If you've paid close attention to the discussion
about regular binary trees (somewhere in this document), you'll find this discussion of
node-pool stuff pretty easy and similar to that "regular" stuff. Enough talk,
lets go and write this node-pool node!
public class pNodePoolTreeNode{
private Object data;
private int left;
private int right;
public pNodePoolTreeNode(){
left = right = -1;
data = null;
}
public pNodePoolTreeNode(int l,int r){
left = l;
right = r;
data = null;
}
public Object getData(){
return data;
}
public void setData(Object o){
data = o;
}
public int getLeft(){
return left;
}
public void setLeft(int l){
left = l;
}
public int getRight(){
return right;
}
public void setRight(int r){
right = r;
}
public String toString(){
return new String(data.toString());
}
}
The code above is pretty much self explanatory (if
it's not, take a look back at binary tree nodes section, and you'll figure it out). The
only significant thing you should notice is that children of the node are now integers,
instead of references to other node objects. Although, it later turns out that what these
integer children are referencing are in fact nodes.
Well, that was easy enough, are you ready to figure
out how a tree actually manipulates these?
Back to Table of Contents
A D V E R T I S E M E N T
|
Subscribe to SourceCodesWorld - Techies Talk |
|