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

对象建模

/**
 * DEMO
 * 一棵树的节点对象
 * @param <T> 树持有的数据对象
 * @author zx
 */
public class TreeNode<T> {
  
    /**树本身所携带的数据*/
    private T data;
    /**树本身所携带的数据 所占大小*/
    private Integer dataSize;
    /**父节点的指针【引用对象地址】*/
    private TreeNode<T> parent;
    /**子节点的指针【引用对象地址】*/
    private List<TreeNode<T>> childList;

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


    /** GET AND SET */
    public T getData() {
        return data;
    }

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

    public TreeNode<T> getParent() {
        return parent;
    }

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

    public List<TreeNode<T>> getChildList() {
        return childList;
    }

    public void setChildList(List<TreeNode<T>> 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<T> parent, List<TreeNode<T>> childList) {
        this.data = data;
        this.parent = parent;
        this.childList = childList;
    }

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

相关的方法

/**
 * 一棵树的节点对象
 *
 * @param <T> 树持有的数据对象
 * @author zx
 */
public class TreeNode<T> {

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

    }

    /**
     * 遍历一棵树-先序遍历
     * 每当遍历一个节点,打印本节点的内容
     * @param node   节点对象
     */
    public static void displayChildNodeForward(TreeNode<String> node){
        if(node == null){
            return;
        }
        List<TreeNode<String>> childList = node.getChildList();
        if(childList == null || childList.size() < 1 ){
            System.out.println(node.getData());
            return;
        }
        System.out.println(node.getData());
        for (TreeNode<String> stringTreeNode : childList) {
            // 每遍历一个节点,打印它的名字
            displayChildNodeForward(stringTreeNode);
        }
    }

    /**
     * 遍历一棵树-后序遍历
     * 统计所有node节点size值的和
     * @param node  节点对象
     * @return 当前节点对象的size + 所有它的子节点对象的size之和
     */
    public static Integer countChildNodeSizeBackward(TreeNode<String> node){
        if(node == null){
            return 0;
        }
        List<TreeNode<String>> childList = node.getChildList();
        Integer dataSizeCurrNode = node.getDataSize();
        if(childList == null || childList.size() < 1 ){
            return dataSizeCurrNode;
        }

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

    /**
     * 手动构建一棵树,样子如下
     *        root
     *        sun1 / sun2 / sun3
     *        sun1_1 sun1_2 sun1_3 sun1_4 / sun2_1 sun2_2 sun2_3 / sun3_1 sun3_2 sun3_3
     */
    public static TreeNode<String> buildTree(){
        // ROOT 根节点
        TreeNode<String> root = new TreeNode<>("root");
        root.setDataSize(1);

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

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

        // 二代目

        // SUN1_1
        TreeNode<String> sun1_1 = new TreeNode<>("sun1_1",1,sun1,null);
        TreeNode<String> sun1_2 = new TreeNode<>("sun1_2",1,sun1,null);
        TreeNode<String> sun1_3 = new TreeNode<>("sun1_3",1,sun1,null);
        TreeNode<String> 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<String> sun2_1 = new TreeNode<>("sun2_1",1,sun2,null);
        TreeNode<String> sun2_2 = new TreeNode<>("sun2_2",1,sun2,null);
        TreeNode<String> sun2_3 = new TreeNode<>("sun2_3",1,sun2,null);
        sun2ChildList.add(sun2_1);
        sun2ChildList.add(sun2_2);
        sun2ChildList.add(sun2_3);

        // SUN3_1
        TreeNode<String> sun3_1 = new TreeNode<>("sun3_1",1,sun3,null);
        TreeNode<String> sun3_2 = new TreeNode<>("sun3_2",1,sun3,null);
        TreeNode<String> 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<T> parent;
    /**子节点的指针【引用对象地址】*/
    private List<TreeNode<T>> childList;

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


    /** GET AND SET */
    public T getData() {
        return data;
    }

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

    public TreeNode<T> getParent() {
        return parent;
    }

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

    public List<TreeNode<T>> getChildList() {
        return childList;
    }

    public void setChildList(List<TreeNode<T>> 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<T> parent, List<TreeNode<T>> childList) {
        this.data = data;
        this.parent = parent;
        this.childList = childList;
    }

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

运行结果

root
sun1
sun1_1
sun1_2
sun1_3
sun1_4
sun2
sun2_1
sun2_2
sun2_3
sun3
sun3_1
sun3_2
sun3_3
totalSize14

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值