首页 > 其他分享 >二叉树的前序,中序,后序,顺序遍历

二叉树的前序,中序,后序,顺序遍历

时间:2023-02-28 18:45:58浏览次数:51  
标签:node 中序 System BinaryTree void 二叉树 new 前序 out

实体类:

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());
        }
    }
}

 

标签:node,中序,System,BinaryTree,void,二叉树,new,前序,out
From: https://www.cnblogs.com/super-chao/p/17165573.html

相关文章