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

来源:互联网 发布:淘宝装修日记哪里去了 编辑:程序博客网 时间:2024/06/04 22:38
    本例实现二叉树的中序遍历。

/**  

*@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);  

 

       } 

}

阅读全文
0 0
原创粉丝点击