代码拉取完成,页面将自动刷新
package srcNo16.TextbookCode.jsjf;
/**
* BinaryTreeNode represents a node in a binary tree with a left and
* right child.
*
* @author Lewis and Chase
* @version 4.0
*/
public class BinaryTreeNode<T>
{
protected T element;
protected BinaryTreeNode<T> left, right;
public BinaryTreeNode(T obj)
{
element = obj;
left = null;
right = null;
}
/**
* Creates a new tree node with the specified data.
*
* @param obj the element that will become a part of the new tree node
* @param left the tree that will be the left subtree of this node
* @param right the tree that will be the right subtree of this node
*/
public BinaryTreeNode(T obj, LinkedBinaryTree<T> left, LinkedBinaryTree<T> right)
{
element = obj;
if (left == null)
this.left = null;
else
this.left = left.getRootNode();
if (right == null)
this.right = null;
else
this.right = right.getRootNode();
}
/**
* Returns the number of non-null children of this node.
*
* @return the integer number of non-null children of this node
*/
public int numChildren()
{
int children = 0;
if (left != null)
children = 1 + left.numChildren();
if (right != null)
children = children + 1 + right.numChildren();
return children;
}
/**
* Return the element at this node.
*
* @return the element stored at this node
*/
public T getElement()
{
return element;
}
/**
* Return the right child of this node.
*
* @return the right child of this node
*/
public BinaryTreeNode<T> getRight()
{
return right;
}
/**
* Sets the right child of this node.
*
* @param node the right child of this node
*/
public void setRight(BinaryTreeNode<T> node)
{
right = node;
}
/**
* Return the left child of this node.
*
* @return the left child of the node
*/
public BinaryTreeNode<T> getLeft()
{
return left;
}
/**
* Sets the left child of this node.
*
* @param node the left child of this node
*/
public void setLeft(BinaryTreeNode<T> node)
{
left = node;
}
public int count(){
int result = 1;
if(left != null)
result += left.count();
if(right != null)
result += right.count();
return result;
}
/*
Performs an inorder traversal on this subtree,updating the specified iterator.
*/
public void inorder(ArrayIterator<T> iter){
if(left != null)
left.inorder(iter);
iter.add(element);
if(right != null)
right.inorder(iter);
}
/*
Performs an preorder traversal on this subtree,updating the specified iterator.
*/
public void preorder(ArrayIterator<T> iter){
iter.add(element);
if(left != null)
left.preorder(iter);
if(right != null)
right.preorder(iter);
}
/*
Performs an postorder traversal on this subtree,updating the specified iterator.
*/
public void postorder(ArrayIterator<T> iter){
if(left != null)
left.postorder(iter);
if(right != null)
right.postorder(iter);
iter.add(element);
}
public boolean isleaf(){
return this.left==null&&this.right==null;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。