zoukankan      html  css  js  c++  java
  • Java实现二叉树先序,中序,后序遍历

    以下是我要解析的一个二叉树的模型形状

    接下来废话不多直接上代码

    一种是用递归的方法,另一种是用堆栈的方法:

    首先创建一棵树:

      

    public class Node {  
        private int data;  
        private Node leftNode;  
        private Node rightNode;  
        public Node(int data, Node leftNode, Node rightNode){  
            this.data = data;  
            this.leftNode = leftNode;  
            this.rightNode = rightNode;  
        }  
      
        public int getData() {  
            return data;  
        }  
        public void setData(int data) {  
            this.data = data;  
        }  
        public Node getLeftNode() {  
            return leftNode;  
        }  
        public void setLeftNode(Node leftNode) {  
            this.leftNode = leftNode;  
        }  
        public Node getRightNode() {  
            return rightNode;  
        }  
        public void setRightNode(Node rightNode) {  
            this.rightNode = rightNode;  
        }  
    }  

    递归:

    public class BinaryTree {  
        /** 
         * @author yaobo
         * 二叉树的先序中序后序排序 
         */  
        public Node init() {//注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错  
            Node J = new Node(8, null, null);  
            Node H = new Node(4, null, null);  
            Node G = new Node(2, null, null);  
            Node F = new Node(7, null, J);  
            Node E = new Node(5, H, null);  
            Node D = new Node(1, null, G);  
            Node C = new Node(9, F, null);  
            Node B = new Node(3, D, E);  
            Node A = new Node(6, B, C);  
            return A;   //返回根节点  
        }
        
        public void printNode(Node node){  
            System.out.print(node.getData());  
        }  
        public void theFirstTraversal(Node root) {  //先序遍历  
            printNode(root);  
            if (root.getLeftNode() != null) {  //使用递归进行遍历左孩子  
                theFirstTraversal(root.getLeftNode());  
            }  
            if (root.getRightNode() != null) {  //递归遍历右孩子  
                theFirstTraversal(root.getRightNode());  
            }  
        }  
        public void theInOrderTraversal(Node root) {  //中序遍历  
            if (root.getLeftNode() != null) {  
                theInOrderTraversal(root.getLeftNode());  
            }  
            printNode(root);  
            if (root.getRightNode() != null) {  
                theInOrderTraversal(root.getRightNode());  
            }  
        }
        
        
        public void thePostOrderTraversal(Node root) {  //后序遍历  
            if (root.getLeftNode() != null) {  
                thePostOrderTraversal(root.getLeftNode());  
            }  
            if(root.getRightNode() != null) {  
                thePostOrderTraversal(root.getRightNode());  
            }  
            printNode(root);  
        }  
          
        public static void main(String[] args) {  
            BinaryTree tree = new BinaryTree();  
            Node root = tree.init();  
            System.out.println("先序遍历");  
            tree.theFirstTraversal(root);  
            System.out.println("");  
            System.out.println("中序遍历");  
            tree.theInOrderTraversal(root);  
            System.out.println("");  
            System.out.println("后序遍历");  
            tree.thePostOrderTraversal(root);  
            System.out.println("");  
        }  
    }  

    堆栈:

     

    public class BinaryTree1 { 
         public Node init() {//注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错  
                Node J = new Node(8, null, null);  
                Node H = new Node(4, null, null);  
                Node G = new Node(2, null, null);  
                Node F = new Node(7, null, J);  
                Node E = new Node(5, H, null);  
                Node D = new Node(1, null, G);  
                Node C = new Node(9, F, null);  
                Node B = new Node(3, D, E);  
                Node A = new Node(6, B, C);  
                return A;   //返回根节点  
            } 
        
        public void printNode(Node node){  
            System.out.print(node.getData());  
        }
        
        
        public void theFirstTraversal_Stack(Node root) {  //先序遍历  
            Stack<Node> stack = new Stack<Node>();  
            Node node = root;  
            while (node != null || stack.size() > 0) {  //将所有左孩子压栈  
                if (node != null) {   //压栈之前先访问  
                    printNode(node);  
                    stack.push(node);  
                    node = node.getLeftNode();  
                } else {  
                    node = stack.pop();  
                    node = node.getRightNode();  
                }  
            }  
        }  
          
        public void theInOrderTraversal_Stack(Node root) {  //中序遍历  
            Stack<Node> stack = new Stack<Node>();  
            Node node = root;  
            while (node != null || stack.size() > 0) {  
                if (node != null) {  
                    stack.push(node);   //直接压栈  
                    node = node.getLeftNode();  
                } else {  
                    node = stack.pop(); //出栈并访问  
                    printNode(node);  
                    node = node.getRightNode(); 
                }  
            }  
        }  
          
        public void thePostOrderTraversal_Stack(Node root) {   //后序遍历  
            Stack<Node> stack = new Stack<Node>();  
            Stack<Node> output = new Stack<Node>();//构造一个中间栈来存储逆后序遍历的结果  
            Node node = root;  
            while (node != null || stack.size() > 0) {  
                if (node != null) {  
                    output.push(node);  
                    stack.push(node);                 
                    node = node.getRightNode();  
                } else {  
                    node = stack.pop();               
                    node = node.getLeftNode();
                }  
            }  
            System.out.println(output.size());
            while (output.size() > 0) {
                
                printNode(output.pop());  
            }  
        }
        
        public static void main(String[] args) {  
            BinaryTree1 tree = new BinaryTree1();  
            Node root = tree.init();  
            System.out.println("先序遍历");  
            tree.theFirstTraversal_Stack(root);  
            System.out.println("");  
            System.out.println("中序遍历");  
            tree.theInOrderTraversal_Stack(root);  
            System.out.println("");  
            System.out.println("后序遍历");  
            tree.thePostOrderTraversal_Stack(root);  
            System.out.println("");  
        }
    }
  • 相关阅读:
    从点滴看管理之新生代员工培养方式的思考
    手机应用PC端演示工具介绍
    Python Day 66 Django框架、Auth认证模块、Auth模块常用方法、扩展默认的auth_user表、补充orm 模型类中releatename属性和 自关联
    Python Day 65 Django框架、Django生命周期、Django中间件、中间件执行流程、Django中MTV模式 和 MVC模式
    Python Day 64 Django框架、cookie和session、Django中Session相关方法、Django中支持Session5种存储介质
    Python Day 63 Django框架、Django模板系统(渲染页面的作用)
    Python Day 62 Django框架、Django框架中分页 、 网页攻击
    Python Day 61 Django框架、Django框架ORM一对一表操作、Django列类型(重点)、自定义列类型、Django-amdin自带管理后台
    Python Day 60 Django框架、ORM高级查询、级联删除、增加多条数据、Django中Xss攻击、事务
    Python Day 59 Django框架、Django中ORM多对多表操作(联合唯一索引)
  • 原文地址:https://www.cnblogs.com/yaobolove/p/6213936.html
Copyright © 2011-2022 走看看