数据结构BinaryTree实例(二):二叉树的中序遍历(递归与非递归)

本文介绍了一种实现二叉树中序遍历的方法,包括递归与非递归两种方式。递归方法直接利用函数自身进行左右节点的遍历,而非递归方法则通过栈来辅助完成遍历过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    本例实现二叉树的中序遍历。

/**  

*@Title: InOrder.java

*@Package binarytree

*@Description: TODO

*@author peidong 

*@date 2017-4-28 上午8:33:25

*@version V1.0  

*/

packagebinarytree;

importjava.util.Stack;

importbinarytree.PreOrderBinaryTree.TreeNode;

 

/**

 * @ClassName: InOrder

 * @Description:实现二叉树的中序遍历

 * @date 2017-4-28 上午8:33:25

 * 

 */

publicclass InOrder {

 

       /**

              *@ClassName: TreeNode

       * @Description: 构建树结点

       * @date 2017-4-28 上午8:34:06

       */

       public static class TreeNode{

              public int data;

              public TreeNode left;

              public TreeNode right;

             

              public TreeNode(int data){

                     this.data = data;

                     left = null;

                     right = null;

              }

       }

      

       /**

        *

       * @Title: InOrder

       * @Description: 递归中序遍历二叉树

       * @param @param root   

       * @return void   

       * @throws

        */

       public void inOrderRec(TreeNode root){

              //边界条件

              if(root ==null){

                     return;

              }else{

                     inOrderRec(root.left);

                     System.out.println(root.data);

                     inOrderRec(root.right);

              }

       }

      

       /**

        *

       * @Title: InOrder

       * @Description: 非递归中序遍历二叉树

       * @param @param root   

       * @return void   

       * @throws

        */

       public void inOrder(TreeNode root) {

              // 边界条件

              if (root == null) {

                     return;

              }

              Stack<TreeNode> tnSatck =new Stack<TreeNode>();

              while (root != null ||tnSatck.empty() == false) {

                     while (root != null) {

                            tnSatck.push(root);   //左子树入栈

                            root = root.left;

                     }

                     if (tnSatck.empty() ==false) {

                            root =tnSatck.pop();           //弹出栈顶元素

                            System.out.println(root.data);  //输出

                            root =root.right;              //遍历右子树

                     }

              }

       }

      

       /**

        *@Title: main

        *@Description: TODO

        *@param @param args   

        *@return void   

        *@throws

        */

       public static void main(String[] args) {

              // TODO Auto-generated method stub

             

              InOrder tree = new InOrder();

              TreeNode root = new TreeNode(10);

              root.left = new TreeNode(8);

              root.right = new TreeNode(2);

              root.left.left = new TreeNode(3);

              root.left.right = new TreeNode(5);

              root.right.left = new TreeNode(2);

             

              tree.inOrder(root);  

 

       } 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值