public class MyBtree {
private Node root;
class Node{
private int data;
private Node left;
private Node right;
public void add(Node node) {
if (this.data > node.data){
if(this.left == null){
this.left = node;
}else{
this.left.add(node);
}
}
if(this.data < node.data){
if(this.right == null){
this.right = node;
}else{
this.right.add(node);
}
}
}
public void zhongxu() {
if(this.left != null){
this.left.zhongxu();
}
if(this.right != null){
this.right.zhongxu();
}
System.out.println(this.data);
}
}
public void add(int data){
Node node = new Node();
node.data = data;
if (root==null){
root = node;
}else {
root.add(node);
}
}
public void zhongxu() {
root.zhongxu();
}
}
java 实现简单树结构
最新推荐文章于 2023-05-16 21:03:32 发布