二叉樹的前序,中序,後序,順序遍歷

實體類:

package com.test.知識點.數據結構.樹.二叉樹;

import lombok.Data;

/**
 * Created by Administrator on 2023/2/28.
 */
@Data
public class BinaryTree {
    private Integer value;

    public BinaryTree(Integer value){
        this.value = value;
    }

    private BinaryTree leftNode;
    private BinaryTree rightNode;
}

測試類:

package com.test.知識點.數據結構.樹.二叉樹;

import java.util.ArrayList;
import java.util.List;

/**
 * 二叉樹的遍歷(前中後序)
 * Created by Administrator on 2023/2/28.
 */
public class Test {
    public static void main(String[] args) {
        BinaryTree root = new BinaryTree(0);
        BinaryTree _1lnode = new BinaryTree(1);
        BinaryTree _1rnode = new BinaryTree(2);
        root.setLeftNode(_1lnode);
        root.setRightNode(_1rnode);
        BinaryTree _1l_2lnode = new BinaryTree(3);
        BinaryTree _1l_2rnode = new BinaryTree(4);
        BinaryTree _1r_2lnode = new BinaryTree(5);
        BinaryTree _1r_2rnode = new BinaryTree(6);
        _1lnode.setLeftNode(_1l_2lnode);
        _1lnode.setRightNode(_1l_2rnode);
        _1rnode.setLeftNode(_1r_2lnode);
        _1rnode.setRightNode(_1r_2rnode);

        System.out.println();
        System.out.println("前序遍歷:");
        preSearch(root);
        System.out.println();
        System.out.println("中序遍歷:");
        midSearch(root);
        System.out.println();
        System.out.println("後序遍歷:");
        behindSearch(root);
        System.out.println();
        System.out.println("順序遍歷:");
        normalSearch(root);
        System.out.println();
    }

    /**
     *         0
     *    1         2
     * 3    4     5    6
     * 前序:0134256
     * 中序:3140526
     * 後序:341 562 0
     * 順序遍歷:0123456
     */

    /**
     * 順序遍歷
     * @param node
     * @return void
     */
    private static void normalSearch(BinaryTree node) {
        System.out.print(node.getValue());
        List<BinaryTree> nodeList = new ArrayList<>();
        nodeList.add(node.getLeftNode());
        nodeList.add(node.getRightNode());
        search(nodeList);
    }

    private static void search(List<BinaryTree> nodeList) {
       List<BinaryTree> nodeListSub = new ArrayList<>();
       for(BinaryTree node : nodeList){
           if(node != null){
               System.out.print(node.getValue());
               nodeListSub.add(node.getLeftNode());
               nodeListSub.add(node.getRightNode());
           }
       }
       if(nodeListSub.size() > 0){
           search(nodeListSub);
       }
    }

    /**
     * 後序:左右根
     * @param node
     * @return void
     */
    private static void behindSearch(BinaryTree node) {
        if(node != null){
            behindSearch(node.getLeftNode());
            behindSearch(node.getRightNode());
            System.out.print(node.getValue());
        }
    }

    /**
     * 中序,左根右
     * @param node
     * @return void
     */
    private static void midSearch(BinaryTree node) {
        if(node != null){
            midSearch(node.getLeftNode());
            System.out.print(node.getValue());
            midSearch(node.getRightNode());
        }
    }

    /**
     * 前序,根左右
     * @param node
     * @return void
     */
    private static void preSearch(BinaryTree node) {
        if(node != null){
            System.out.print(node.getValue());
            preSearch(node.getLeftNode());
            preSearch(node.getRightNode());
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章