二叉树的遍历与二叉树的深度

    1 二叉树的遍历包含前序遍历,中序遍历,后序遍历。三种遍历均包含递归方式和非递归方式的求解。

	//递归形式的前序遍历
	public static void pre(BinaryTree root){
		    if(root!=null){
			System.out.print(root.data);
			pre(root.lchild);
			pre(root.rchild);
		    }
	}
	//递归形式进行中序遍历
	public static void in(BinaryTree root){
		   if(root!=null){
			  in(root.lchild);
			  System.out.print(root.data);
			  in(root.rchild);
		   }
	}
	
	//递归的形式进行后序遍历
	public static void after(BinaryTree root){
		   if(root!=null){
		   after(root.lchild);
		   after(root.rchild);
			  System.out.print(root.data);
		   }
	}
	
	//非递归形式下的先序遍历
	public static void preTraverse(BinaryTree root){
		Stack<BinaryTree> strack=new Stack<BinaryTree>();
		strack.push(root);
		while(!strack.isEmpty()){
			BinaryTree bt=strack.pop();//可以理解成为了将自己弹出来,将自己的右孩子先丢进去,然后将左孩子丢进去  因为堆栈是先进后出的
			System.out.print(bt.data);
			if(bt.rchild!=null) strack.push(bt.rchild);
			if(bt.lchild!=null) strack.push(bt.lchild);
		}
	}
	
	//非递归形式下的中序遍历
	public static void inTraverse(BinaryTree root){
		  Stack<BinaryTree> s = new Stack<BinaryTree>();
		  BinaryTree p = root;
		  while(p!=null || !s.isEmpty()) {//如果指针不指向空,就继续指向其左子树,否则在堆栈中进行弹出数据,直至堆栈为空
		   if(p!=null) {
		    s.push(p);
		    p = p.lchild;
		   }
		   else {
		    p = (BinaryTree)s.pop();
		    System.out.print(p.data);
		    p = p.rchild;
		   }
		  }
		}
	
	public static void afterTranverse(BinaryTree root){
		Stack<BinaryTree> s = new Stack<BinaryTree>();
		  BinaryTree p = root;
		  //pre标记最近出栈的节点,用于判断是否是p节点的右孩子,如果是的话,就可以访问p节点
		  //意思就是说:一个根节点在两种情况可以被访问 1没有右子树,且当前左子树已被访问
		  //2存在右子树 且该右子树已被访问(已被访问等价于出栈)
		  BinaryTree pre = p;
		  while(p!=null || !s.isEmpty()) {
          if(p!=null){
        	 s.push(p);
        	 p=p.lchild;
          }
          else{//如果p节点已经遍历到数的最左下  取出栈顶元素 判断其是否具有右子树或者右子树是否已经遍历
        	  p=s.peek();//peek是用于查看栈顶部的元素,但并不移除它
        	  if(p.rchild==null||p.rchild==pre){//p点满足可以被访问的条件
        		  p=s.pop();
        		  System.out.print(p.data);
        		  pre=p;//当元素从栈中弹出,就将其设置为当前已访问元素
        		  p=null;//这一句意思是这个子树已经遍历完毕 需要满足条件,使元素从栈中弹出
        	  }
        	  else{
        		  p=p.rchild;
        		  		  
        	  }
                  }
          
		  }
		 }
2 求树的深度

第一种思路是利用树的深度=max(左子树的深度,右子树的深度)+1   因为二叉树是递归定义的,就可以按照该表达式进行递归求解

        //求树的深度
	public static int getDepth(node root){
	int depth=0;
	int lDepth=0;
	int rDepth=0;
	if(root.lchild!=null) lDepth=getDepth(root.lchild);
	if(root.rchild!=null) rDepth=getDepth( root.rchild);
	depth=lDepth>=rDepth?lDepth+1:rDepth+1;
	return depth;
	}
第二种思路借鉴的是对二叉树进行控制台打印的思想,利用两个队列辅助进行

	//第二种方法展示树的深度 
	public static int getDepth1(node root){
	Queue<node> queue=new LinkedList<BST.node>();
	Queue<node> queue1=new LinkedList<BST.node>();
	queue.add(root);
	int length=0;
	node temp=new BST().new node();
	while((!queue.isEmpty())||(!queue1.isEmpty())){
		if(!queue.isEmpty()){
		while(!queue.isEmpty()){
			temp=queue.poll();
			if(temp.lchild!=null) queue1.add(temp.lchild);
			if(temp.rchild!=null) queue1.add(temp.rchild);
		}
		length++;//当一个队列的所有数据出队,意味着一层结点已经遍历完毕 树的深度加1
		}
		if(!queue1.isEmpty()){
		while(!queue1.isEmpty()){
			temp=queue1.poll();
			if(temp.lchild!=null) queue.add(temp.lchild);
			if(temp.rchild!=null) queue.add(temp.rchild);
			}	
		length++;
		}
	}
	return length;
	}

     请多多批评指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值