2 Star 0 Fork 0

CS-IMIS-23/zc20172324

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
BinaryTreeNode.java 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
zc20172324 提交于 7年前 . 书上代码
package chap12;
/**
* @author LbZhang
* @version 创建时间:2015年11月22日 上午10:54:19
* @description 二叉树结点类
*/
public class BinaryTreeNode<T> {
protected T element;
protected BinaryTreeNode<T> left;
protected BinaryTreeNode<T> right;
/**
* Creates a new tree node with the specified data.
*
* @param obj
* the element that will become a part of the new tree node
*/
public BinaryTreeNode(T obj) {
this.element = obj;
this.left = null;
this.right = null;
}
///合并构建声明
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;
}
public BinaryTreeNode<T> getRight() {
return right;
}
public void setRight(BinaryTreeNode<T> node) {
right = node;
}
public BinaryTreeNode<T> getLeft() {
return left;
}
public void setLeft(BinaryTreeNode<T> node) {
left = node;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/zc20172324.git
git@gitee.com:CS-IMIS-23/zc20172324.git
CS-IMIS-23
zc20172324
zc20172324
master

搜索帮助