栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【数据结构】最基本的树的遍历

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【数据结构】最基本的树的遍历

对象建模
public class TreeNode {
  
    
    private T data;
    
    private Integer dataSize;
    
    private TreeNode parent;
    
    private List> childList;

    public List> listAll(){
        return this.childList;
    }


    
    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public List> getChildList() {
        return childList;
    }

    public void setChildList(List> childList) {
        this.childList = childList;
    }

    public TreeNode(T data) {
        this.data = data;
    }

    public Integer getDataSize() {
        return dataSize;
    }

    public void setDataSize(Integer dataSize) {
        this.dataSize = dataSize;
    }

    public TreeNode(T data, TreeNode parent, List> childList) {
        this.data = data;
        this.parent = parent;
        this.childList = childList;
    }

    public TreeNode(T data, Integer dataSize, TreeNode parent,
        List> childList) {
        this.data = data;
        this.dataSize = dataSize;
        this.parent = parent;
        this.childList = childList;
    }
}
相关的方法
package org.example.tree;

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


public class TreeNode {

    public static void main(String[] args) {
        // 构建一棵树
        TreeNode root = buildTree();
        // 先序遍历一棵树,打印所有node的name
        displayChildNodeForward(root);
        // 后序遍历,递归计算统计所有节点的size的和
        Integer totalSize = countChildNodeSizeBackward(root);
        System.out.println("totalSize" + totalSize);

    }

    
    public static void displayChildNodeForward(TreeNode node){
        if(node == null){
            return;
        }
        List> childList = node.getChildList();
        if(childList == null || childList.size() < 1 ){
            return;
        }
        for (TreeNode stringTreeNode : childList) {
            // 每遍历一个节点,打印它的名字
            System.out.println(stringTreeNode.getData());
            displayChildNodeForward(stringTreeNode);
        }
    }

    
    public static Integer countChildNodeSizeBackward(TreeNode node){
        if(node == null){
            return 0;
        }
        List> childList = node.getChildList();
        Integer dataSizeCurrNode = node.getDataSize();
        System.out.println(dataSizeCurrNode);
        if(childList == null || childList.size() < 1 ){
            return dataSizeCurrNode;
        }

        for (TreeNode stringTreeNode : childList) {
            // 每遍历一个节点,将所有下层节点的值累加返回
            dataSizeCurrNode += countChildNodeSizeBackward(stringTreeNode);
        }
        return dataSizeCurrNode;
    }

    
    public static TreeNode buildTree(){
        // ROOT 根节点
        TreeNode root = new TreeNode<>("root");
        root.setDataSize(1);

        // SUN 一代目
        List> sunChildList = new ArrayList<>();
        List> sun1ChildList = new ArrayList<>();
        List> sun2ChildList = new ArrayList<>();
        List> sun3ChildList = new ArrayList<>();

        TreeNode sun1 = new TreeNode<>("sun1",1,root,sun1ChildList);
        TreeNode sun2 = new TreeNode<>("sun2",1,root,sun2ChildList);
        TreeNode sun3 = new TreeNode<>("sun3",1,root,sun3ChildList);
        sunChildList.add(sun1);
        sunChildList.add(sun2);
        sunChildList.add(sun3);
        root.setChildList(sunChildList);

        // 二代目

        // SUN1_1
        TreeNode sun1_1 = new TreeNode<>("sun1_1",1,sun1,null);
        TreeNode sun1_2 = new TreeNode<>("sun1_2",1,sun1,null);
        TreeNode sun1_3 = new TreeNode<>("sun1_3",1,sun1,null);
        TreeNode sun1_4 = new TreeNode<>("sun1_4",1,sun1,null);
        sun1ChildList.add(sun1_1);
        sun1ChildList.add(sun1_2);
        sun1ChildList.add(sun1_3);
        sun1ChildList.add(sun1_4);
        sun1.setChildList(sun1ChildList);

        // SUN2_1
        TreeNode sun2_1 = new TreeNode<>("sun2_1",1,sun2,null);
        TreeNode sun2_2 = new TreeNode<>("sun2_2",1,sun2,null);
        TreeNode sun2_3 = new TreeNode<>("sun2_3",1,sun2,null);
        sun2ChildList.add(sun2_1);
        sun2ChildList.add(sun2_2);
        sun2ChildList.add(sun2_3);

        // SUN3_1
        TreeNode sun3_1 = new TreeNode<>("sun3_1",1,sun3,null);
        TreeNode sun3_2 = new TreeNode<>("sun3_2",1,sun3,null);
        TreeNode sun3_3 = new TreeNode<>("sun3_3",1,sun3,null);
        sun3ChildList.add(sun3_1);
        sun3ChildList.add(sun3_2);
        sun3ChildList.add(sun3_3);

        return root;

    }

    
    private T data;
    private Integer dataSize;
    
    private TreeNode parent;
    
    private List> childList;

    public List> listAll(){
        return this.childList;
    }


    
    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public List> getChildList() {
        return childList;
    }

    public void setChildList(List> childList) {
        this.childList = childList;
    }

    public TreeNode(T data) {
        this.data = data;
    }

    public Integer getDataSize() {
        return dataSize;
    }

    public void setDataSize(Integer dataSize) {
        this.dataSize = dataSize;
    }

    public TreeNode(T data, TreeNode parent, List> childList) {
        this.data = data;
        this.parent = parent;
        this.childList = childList;
    }

    public TreeNode(T data, Integer dataSize, TreeNode parent,
        List> childList) {
        this.data = data;
        this.dataSize = dataSize;
        this.parent = parent;
        this.childList = childList;
    }
}
运行结果
sun1
sun1_1
sun1_2
sun1_3
sun1_4
sun2
sun2_1
sun2_2
sun2_3
sun3
sun3_1
sun3_2
sun3_3
totalSize14

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/704250.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号