java前序遍历,中序遍历,后序遍历二叉树

最近总结了下,二叉树的遍历的非递归实现过程,以下是整个java实现过程

package com.test.dayumianshi;

import java.util.*;

public class BinaryTreeRank {


   //存储除根结点所有节点的集合
   private static List<Node> list = new ArrayList<Node>();

   private static Node root;

   static class Node{
        private int data;
        private Node left;

        private Node right;
    }

    //通过构造函数初始化根结点
    public BinaryTreeRank(Node node){
        node.data = 9;

    }

    //构造二叉树
    static  BinaryTreeRank structureBinaryTree(){

       root = new BinaryTreeRank.Node();

        BinaryTreeRank binaryTree = new BinaryTreeRank(root);

        for(int i=0;i<6;i++){
            Node node = new Node();

            switch (i){

                case 0:
                    node.data=7;
                    root.left = node;
                    list.add(node);
                    break;

                case 1:
                    node.data=10;
                    root.right = node;
                    list.add(node);
                    break;

                case 2:
                    node.data = 5;
                    list.get(0).left = node;
                    list.add(node);
                    break;

                case 3:
                    node.data = 8;
                    list.get(0).right = node;
                    list.add(node);
                    break;
                case 4:
                    node.data = 11;
                    list.get(1).right = node;
                    list.add(node);
                    break;

                case 5:
                    node.data = 3;
                    list.get(2).left = node;
                    list.add(node);
                    break;

            }
        }

        return binaryTree;
    }

    //中序遍历
    static void middleTravelWithoutRecursion(Node root){


       System.out.print("中序遍历:\t");
        Stack<Node> stack = new Stack<Node>();

        Node node = root;

        while(node!=null||stack.size()>0){

            while(node!=null){

                stack.push(node);
                node = node.left;
            }

            node = stack.pop();
            System.out.print(node.data+"\t");

            node = node.right;
        }


    }

    //前序遍历
    static void beforeTravelWithoutRecursion(Node root){

         System.out.print("\n前序遍历:\t");
         Stack<Node> stack = new Stack<Node>();

         Node node = root;

        while(node!=null||!stack.isEmpty()){

            while(node!=null){

                stack.push(node);
                System.out.print(node.data+"\t");
                node = node.left;

            }

            node = stack.pop();
            node = node.right;

        }

    }

    //后序遍历
    static void afterTravelWithoutRecursion(Node root){
       System.out.print("\n后序遍历:\t");

       Stack<Node> stack = new Stack<Node>();


       Node node = root;
       while(true){

           if(node!=null){
               stack.push(node);
               node = node.left;
           }else{

               if(stack.isEmpty()){
                   break;
               }

               if(null==stack.lastElement().right){
                   node = stack.pop();

                   System.out.print(node.data+"\t");

                   while(node == stack.lastElement().right){

                       System.out.print(stack.lastElement().data+"\t");

                       node = stack.pop();

                       if(stack.isEmpty()){
                           break;
                       }

                   }
               }

               if(!stack.isEmpty()){
                   node = stack.lastElement().right;
               }else{
                   node = null;      //用于处理最后一个节点
               }

           }


       }

    }

    public static void main(String[] args){

         structureBinaryTree();

         middleTravelWithoutRecursion(root);

         beforeTravelWithoutRecursion(root);

         afterTravelWithoutRecursion(root);


    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值