1个回答
展开全部
class Node {
private int value;
private Node left;
private Node right;
// 存储节点
public void store(int value) {
if (this.value > value) {
if (left == null) {
left = new Node();
left.value = value;
} else {
left.store(value);
}
} else {
if (right == null) {
right = new Node();
right.value = value;
} else {
/*right.value = value;*/
right.store(value);
}
}
}
// 查找节点
public boolean find(int value) {
if (this.value == value) {
return true;
} else if (this.value > value) {
if (right == null)
return false;
return right.find(value);
} else {
if (left == null)
return false;
return left.find(value);
}
}
// 前序遍历
public void preList() {
System.out.print(this.value + ">>");
if (left != null)
left.preList();
if (right != null)
right.preList();
}
// 中序遍历
public void middleList() {
if (left != null)
left.middleList();
System.out.print(this.value + ">>");
if (right != null)
right.middleList();
}
// 后序遍历
public void afterList() {
if (left != null)
left.afterList();
if (right != null)
right.afterList();
System.out.print(this.value + ">>");
}
/**
* @return the value
*/
public int getValue() {
return value;
}
/**
* @param value the value to set
*/
public void setValue(int value) {
this.value = value;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询