• 关于树的基本操作


    1.二叉树的比较常见的数据结构,下面用java实现简单的树的一下操作

    package tree;
    
    /**
     * @Author lizhilong
     * @create 2019/11/11 14:20
     * @desc
     */
    public class Tree<T> {
    
        T value;
    
        Tree<T> leftChild;
    
        Tree<T> rightChild;
    
        public Tree(T value) {
            this.value = value;
        }
    
        public Tree addLeft(T value) {
            Tree<T> left = new Tree<>(value);
            this.leftChild = left;
            return left;
        }
    
        public Tree addRight(T value) {
            Tree<T> right = new Tree<>(value);
            this.rightChild = right;
            return right;
        }
    
    
        public <T> int getNodeNum(Tree<T> tree) {
            if (tree == null) {
                return 0;
            }
            return (getNodeNum(tree.leftChild) + getNodeNum(tree.rightChild)+1);
        }
    
    
        public<T>  int getDeepNum(Tree<T> tree){
            if(tree == null){
                return 0;
            }
            int maxright = getDeepNum(tree.rightChild)+1;
            int maxleft = getDeepNum(tree.leftChild)+1;
            return  Math.max(maxleft,maxright);
    
        }
    
    
        /**
         * 前序遍历
         * 根->左->右
         * @param tree
         */
        public  void preVisit(Tree tree){
            if(tree==null){
                return;
            }
            print(tree);
            preVisit(tree.leftChild);
            preVisit(tree.rightChild);
        }
    
        /**
         * 中序遍历
         * 左->根->右
         * @param tree
         */
        public void midVist(Tree tree){
            if(tree == null){
                return;
            }
            midVist(tree.leftChild);
            print(tree);
            midVist(tree.rightChild);
        }
    
        /**
         * 后续遍历
         * 左->右->根
         * @param tree
         */
        public void  afterVisit(Tree tree){
            if(tree == null){
                return;
            }
            afterVisit(tree.leftChild);
            afterVisit(tree.rightChild);
            print(tree);
        }
    
    
        public void print(Tree tree){
            System.out.print(tree.value+"   ");
        }
    
    }
  • 相关阅读:
    打开虚拟机导致电脑蓝屏
    jmeter访问接口后返回的数据乱码
    使用jmeter批量对新增账号初始化操作
    使用jmeter参数化时在数据中匹配11位的手机号并分组操作
    vsphere vdp备份情况导出
    grep的完全匹配(不是-w)
    win10右键新建md文件,亲测有效
    信步漫谈之Wiki知识库——搭建dokuwiki
    vim操作学习
    writev函数
  • 原文地址:https://www.cnblogs.com/li-zhi-long/p/11842635.html
Copyright © 2020-2023  润新知