java二叉树的创建与遍历

public class BTreeNode {

	BTreeNode left,right;
	char element;
	public BTreeNode(char obj){
		element=obj;
	}
}

public interface BTreeInterface {

	void createBTree(String str);
	void printBTree(BTreeNode root);
	void inOrder(BTreeNode root);
	void preOrder(BTreeNode root);
	void postOrder(BTreeNode root);
	void depthFirstSearch(BTreeNode root);
	void breadFirstSearch(BTreeNode root);
}

public class BinaryTree implements BTreeInterface{
	static BTreeNode root;
	public BinaryTree(){
		root=null;
	}
	public static void main(String[] args){
		BinaryTree bt=new BinaryTree();
		String str="A(B(D,E),C(,F))";
		bt.createBTree(str);
		System.out.print("中序遍历:");
		bt.inOrder(root);
		System.out.println();
		
	    System.out.print("先序遍历:");
		bt.preOrder(root);
		System.out.println();
		
		System.out.print("后序遍历:");
		bt.postOrder(root);
		System.out.println();
		
		System.out.print("深度遍历:");
		bt.depthFirstSearch(root);
		System.out.println();
		
		System.out.print("广度遍历:");
		bt.breadFirstSearch(root);
		System.out.println();
		//bt.printBTree(root);
	
	}

	public void createBTree(String str) {
		// TODO Auto-generated method stub
		Stack stack=new Stack();
		int k=1;
		BTreeNode p=null;
		char[] ch=str.toCharArray();
		for(int i=0;i<ch.length;i++){
			switch (ch[i]) {
			case ' ':
				break;
			case ',':
				k=2;
				break;
			case '(':
				k=1;
				stack.push(p);
				break;
			case ')':
				if (stack.isEmpty()) {
					System.out.println("输入的字符串广义表不能描述一颗二叉树,因为括号不匹配");
					System.exit(1);
				}
				stack.pop();
				break;

			default:
				p=new BTreeNode(ch[i]);
				if(root==null){
					root=p;
				}else{
					if(k==1){
				         ((BTreeNode)stack.peek()).left=p;
				   }else {
					     ((BTreeNode)stack.peek()).right=p;
				   }
				}
					
				break;
			}
		}
	}

	public void printBTree(BTreeNode root) {
		// TODO Auto-generated method stub
		while(root!=null){
			System.out.print(root.element);
			if(root.left!=null){
				System.out.print("(");
				System.out.print(root.left.element);
				root=root.left;
			}else {
				System.out.print(",");
				if(root.right!=null){
					System.out.print(root.right.element);
				    root=root.right;
				}
				
			}
			
		}
	}

	public void inOrder(BTreeNode root) {
		// TODO Auto-generated method stub
		if (root!=null) {
			inOrder(root.left);
			System.out.print(root.element);
			inOrder(root.right);
		}
	}

	public void preOrder(BTreeNode root) {
		// TODO Auto-generated method stub
		if(root!=null){
			System.out.print(root.element);
			preOrder(root.left);
			preOrder(root.right);
		}
	}

	public void postOrder(BTreeNode root) {
		// TODO Auto-generated method stub
		if(root!=null){
			preOrder(root.left);
			preOrder(root.right);
			System.out.print(root.element);
		}
	}
	public void depthFirstSearch(BTreeNode root) {
		// TODO Auto-generated method stub
		if(root!=null)
			System.out.print(root.element);
		if(root.left!=null)
		depthFirstSearch(root.left);
		if(root.right!=null)
		depthFirstSearch(root.right);
		
	}
	public void breadFirstSearch(BTreeNode root) {
		// TODO Auto-generated method stub
		Queue queue=new LinkedList();
		BTreeNode p;
		if(root!=null){
			System.out.print(root.element);
			queue.add(root);
		}
		while (!queue.isEmpty()) {
			p=(BTreeNode)queue.remove();
			if(p.left!=null){
				System.out.print(p.left.element);
			    queue.add(p.left);
			   
			}
			if(p.right!=null){
				System.out.print(p.right.element);
			    queue.add(p.right);
			
			}	
		}
	}


}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值