java二叉树

public class Node {
    int id;   // 索引值
	int data;
	Node leftNode;
	Node rightNode;
	
	Node(){
		
	}
	
}



package tree;

public class BiTree {
	private static  Node root;

	public BiTree(){
		root=null;
	}

	public Node insertNode(int index,int val){
		Node newNode=new Node();
		newNode.id=index;
		newNode.data=val;
		boolean tr=true;
		if(root==null) root=newNode;
		else{
			Node current=root;
			
			while(tr){
				Node parent=current;
				if(index>current.id){
					current=current.rightNode;
					if(current==null){
						parent.rightNode=newNode;              //树的连接
						tr=false;
						return current;
					}
				}
				else{
					current=current.leftNode;
					if(current==null){
						parent.leftNode=newNode;
						tr=false;
						return current;
					}
				}
		
			}
		}
		return null;

	}
	public Node getNode(int index){
		Node current =root;
		boolean tr=true;
		if(root==null)
			return null;
		while(tr){
		
			if(current.id==index){
				System.out.println("get it : "+current.data);
				tr=false;
				return current;
			}
			if(current.id<index){
				current=current.rightNode;
			}
			else{
				current=current.leftNode;
			}
			if(current==null){
				System.out.println("null ,can not get it");
				tr=false;
				return null;
			}
		

		}
		return null;
	}

	/*
	 * 删除掉包括index在内和与index连接 的后面的点
	 */
	public  Node deleteNode(int index){
		Node current=root;
		Node parent = null;
		boolean tr=true;
		if(root==null) return null;
		if(root.leftNode==null&&root.rightNode==null) {
			root=null;
			return null;
		}
		while(tr){
			parent=current;
			if(current.id<index){
				
				current=current.rightNode;
				if(current.id==index){
					parent.rightNode=null;
					tr=false;
					return current;
				}
				if(current==null){
					tr=false;
					return current;
				}
			}
			else{
		     		current=current.leftNode;
				if(current.id==index){				
					parent.leftNode=null;
					tr=false;
					return current;
				}
				if(current==null){
					tr=false;
					return current;
				}
			}

		}
		return null;
	}

	public void inOrder(Node node){
      

		if(node!=null){
			inOrder(node.leftNode);
			System.out.print(node.data+ " ");
			inOrder(node.rightNode);
		}
	}
	
	public void postOrder(Node node){
	      

		if(node!=null){
			postOrder(node.leftNode);
			postOrder(node.rightNode);
			System.out.print(node.data+ " ");
		}
	}
	
	public void preOrder(Node node){
	      
		if(node!=null){
			System.out.print(node.data+ " ");
			preOrder(node.leftNode);
			preOrder(node.rightNode);
		}
	}

	public static void main(String[] args){
		BiTree bit=new BiTree();
		bit.insertNode(25, 3);
		bit.insertNode(3, 4);
		bit.insertNode(52, 33);
		bit.insertNode(523, 333);
		bit.insertNode(35, 13);
		bit.insertNode(532, 30);
		bit.insertNode(1, 113);
		bit.insertNode(15, 23);
		bit.insertNode(10, 3323);
		bit.getNode(10);
//		bit.deleteNode(523);
		bit.postOrder(root);
		System.out.println("");
		bit.preOrder(root);
		System.out.println("");
		bit.inOrder(root);

	}

}

你可能感兴趣的:(java)