欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

树结构

程序员文章站 2022-03-03 10:29:17
...

public class BiTree {
class Node{
private int data;
private Node left;
private Node right;

public Node(int i){
data = i;
left = right = null;
}

public void add(Node node){
if(node.data<data){
if(this.left == null){
this.left = node;
}else{
left.add(node);
}
}else{
if(this.right == null){
this.right = node;
}else{
this.right.add(node);
}
}
}
public void zhongXu(){
if(left!=null)left.zhongXu();
System.out.println(data);
if(right!=null)right.zhongXu();
}
}

private Node root;

public void add(int i){
Node node = new Node(i);
if(root==null){
root=node;
return;
}
root.add(node);
}

public void zhongXu(){
if(root==null)return;
root.zhongXu();
}

}

相关标签: