树结构

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();
	}
	
}

你可能感兴趣的:(树)