先中後順に二叉木(再帰、非再帰)を巡る(Java)

4349 ワード

考え方:
再帰、非再帰の2つの方式、再帰、非再帰は二叉木を遍歴する
 
コード:
package com.my.test.datastructure.tree;

import java.util.Stack;

/**
 *          (  、   )
 */
public class BinaryTreeVisit
{

    static class Node {
        int value;
        Node left;
        Node right;
        Node(int value) {
            this.value = value;
        }
    }
    
    /**
     *     
     */
    public static void preOrder(Node root) {
        if (root == null) {
            return;
        }
        System.out.print(root.value + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    
    /**
     *     
     */
    public static void preOrderFast(Node root) {
        if (root == null) {
            return;
        }
        Stack s = new Stack<>();
        s.push(root);
        Node curNode;
        while (!s.isEmpty()) {
            curNode = s.pop();
            System.out.print(curNode.value + " ");
            //      ,         ,       ,       
            if (curNode.right != null) {
                s.push(curNode.right);
            }
            if (curNode.left != null) {
                s.push(curNode.left);
            }
        }
    }
    
    /**
     *     
     */
    public static void inOrder(Node root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.value + " ");
        inOrder(root.right);
    }
    
    /**
     *     
     */
    public static void inOrderFast(Node root) {
        if (root == null) {
            return;
        }
        Stack s = new Stack<>();
        Node curNode = root;
        while (!s.isEmpty() || curNode != null) {
            //              
            while (curNode != null) {
                s.push(curNode);
                curNode = curNode.left;
            }
            
            //      
            curNode = s.pop();
            System.out.print(curNode.value + " ");
            //    ,          
            curNode = curNode.right;
        }
    }
    
    /**
     *     
     */
    public static void postOrder(Node root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.value + " ");
    }
    
    /**
     *     
     */
    public static void postOrderFast(Node root) {
        if (root == null) {
            return;
        }
        
        Stack s1 = new Stack<>();
        Stack s2 = new Stack<>();
        
        s1.push(root);
        
        Node curNode;
        while(!s1.isEmpty()) {

            curNode = s1.pop();
            //  、 、       
            s2.push(curNode);
            
            //   s1     ,   、 、     s2 
            if (curNode.left != null) {
                s1.push(curNode.left);
            }
            if (curNode.right != null) {
                s1.push(curNode.right);
            }
        }
        
        while (!s2.isEmpty()) {
            System.out.print(s2.pop().value + " ");
        }
    }
    
    public static void main(String[] args)
    {
        Node root = createBinaryTree();
        
        System.out.println("PreOrder: ");
        preOrder(root);
        System.out.println("
PreOrder: "); preOrderFast(root); System.out.println("
InOrder: "); inOrder(root); System.out.println("
InOrder: "); inOrderFast(root); System.out.println("
PostOrder: "); postOrder(root); System.out.println("
PostOrder: "); postOrderFast(root); } private static Node createBinaryTree() { Node root = new Node(0); Node node1 = new Node(1); Node node2 = new Node(2); root.left = node1; root.right = node2; Node node3 = new Node(3); node1.left = node3; Node node4 = new Node(4); node3.left = node4; return root; } }

 
参照先:
二叉木の先、中、後順遍歴とは何ですか.https://blog.csdn.net/qq_33243189/article/details/80222629
いくつかの二叉木が遍歴しています.https://blog.csdn.net/silence1772/article/details/83623336