关于树的一些代码整理

  • 首先是创建树(先序):
    //先序创建树
    public static TreeNode firCreateTree(TreeNode root,String str){
        char ch = str.charAt(count++);
        if(ch == '#'){
            root = null;
            return root;
        }
        root = new TreeNode();
        root.val = ch;
        root.left = firCreateTree(root.left,str);
        root.right = firCreateTree(root.right,str);
        return root;
    }
  • 遍历树(递归、非递归 (先序、中序、后序)):非递归运用了栈。
//先序递归
    public static void firstSearch(TreeNode root){
        if(root == null)return;
        System.out.print(root.val+" ");
        firstSearch(root.left);
        firstSearch(root.right);
    }

    //先序非递归
    public static void firstSearch1(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();
        while(root != null || !stack.isEmpty()){
            while(root != null){
                stack.push(root);//入栈
                System.out.print(root.val+" ");
                root = root.left;
            }
            if(!stack.isEmpty()){
                root = stack.pop();//弹栈
                root = root.right;
            }
        }
    }

    //中序递归
    public static void midSearch(TreeNode root){
        if(root == null)return;
        midSearch(root.left);
        System.out.print(root.val+" ");
        midSearch(root.right);
    }

    //中序非递归
    public static void midSearch1(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();
        while(root != null || !stack.isEmpty()){
            while(root != null){
                stack.push(root);
                root = root.left;
            }
            if(!stack.isEmpty()){
                root = stack.pop();
                System.out.print(root.val+" ");
                root = root.right;
            }
        }
    }

    //后序递归
    public static void lastSearch(TreeNode root){
        if(root == null)return;
        lastSearch(root.left);
        lastSearch(root.right);
        System.out.print(root.val+" ");
    }

    //后序非递归
    public static void lastSearch1(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();
        TreeNode temp = null;//标记右子树是否被访问过

        while(root != null || !stack.isEmpty()){
            while(root != null){
                stack.push(root);
                root = root.left;
            }
            if(!stack.isEmpty()){
                root = stack.peek();//取栈顶
                if(root.right == null || root.right == temp){
                    root = stack.pop();
                    System.out.print(root.val+" ");
                    temp = root;
                    root = null;//处理最后一个结点,以便跳出大循环
                }else{
                    root = root.right;
                }
            }

        }
    }
  • 层次遍历:运用了队列的思想,和广度优先搜索一样。
    //层次遍历
    public static void levelSearch(TreeNode root){
        if(root == null)return;
        Queue<TreeNode> queue = new LinkedList<>();

        queue.offer(root);//入队
        while(!queue.isEmpty()){
            root = queue.poll();//出队
            System.out.print(root.val+" ");
            if(root.left != null)queue.offer(root.left);
            if(root.right != null)queue.offer(root.right);
        }
    }
  • 求树的高度或深度:使用了成员变量height来进行更新,且传入的temp初始化是1。
    //树的高度:有成员变量
    public static void getHeight(TreeNode root,int temp){
        if(root == null)return;
        if(height < temp)height = temp;
        getHeight(root.left,temp+1);
        getHeight(root.right,temp+1);
    }
  • 求指定结点的双亲:有递归和非递归两种,非递归形式是在先序非递归的遍历基础上进行改造的,递归形式是在先序递归遍历的基础上改造的。end即为当前结点的双亲结点。
    //求指定结点的双亲:非递归
    public static TreeNode getParent(TreeNode root,char ch){
        TreeNode temp = root;//记录双亲结点
        Stack<TreeNode> stack = new Stack<>();
        while(root != null || !stack.isEmpty()){
            while(root != null){
                stack.push(root);
                temp = root;
                root = root.left;
                if(root != null && root.val == ch && isParAndSon(temp,root)){
                    return temp;
                }
            }
            if(!stack.isEmpty()){
                root = stack.pop();
                temp = root;
                root = root.right;
                if(root != null && root.val == ch && isParAndSon(temp,root)){
                    return temp;
                }
            }
        }
        return null;
    }

    //求指定结点的双亲:递归
    public static void getParent1(TreeNode root,char ch,TreeNode end){
        if(root == null)return;
        if(root.val == ch){
            if(end != null) System.out.println("该结点的双亲为:"+end.val);
            else System.out.println("该结点的双亲为:"+end);
        }
        getParent1(root.left,ch,root);
        getParent1(root.right,ch,root);
    }


    //判断是否是双亲孩子关系
    public static boolean isParAndSon(TreeNode parent,TreeNode son){
        if( (parent.left != null && parent.left.val == son.val) || (parent.right != null && parent.right.val == son.val))return true;
        return false;
    }
  • 判断两个树是否是同一个数(结构,数据均相同):
    //递归做法,返回true相似,返回false不相似
    public static boolean isSameTree(TreeNode tree1,TreeNode tree2){
        if(tree1 == null && tree2 == null)return true;
        if(tree1 == null || tree2 == null)return false;
        return isSameTree(tree1.left,tree2.left)&&isSameTree(tree1.right,tree2.right);
    }
  • 根据先序中序创建二叉树:由先序确定根的位置,再在中序中分割左右子树。
    //根据先序中序序列创建二叉树
    public static TreeNode firAndMidCreate(char[] pre,char[] mid,int pres,int mids,int mide){
        if( mide - mids <= 0){
            return null;
        }
        TreeNode root = new TreeNode(pre[pres]);//取先序根
        System.out.println(pre[pres]);
        int i;
        for(i = mids;i < mide; i++){
            if(pre[pres] == mid[i])break;//找到中序中的先序根,一分为二
        }
        ++pres;
        root.left = firAndMidCreate(pre,mid,pres,mids,i);//递归左子树
        root.right = firAndMidCreate(pre,mid,pres+i-mids,i+1,mide); //递归右子树
        return root;
    }
  • 根据中序、后序创建二叉树:由后序确定根的位置,再在中序中分割左右子树。
    //根据中序后序序列创建二叉树
    public static TreeNode midAndLastCreate(char[] mid,char[] last,int lasts,int mids,int mide){
        if((mide - mids) <= 0){
            return null;
        }
        TreeNode root = new TreeNode(last[lasts+mide-mids-1]);
        System.out.println(last[lasts+mide-mids-1]);
        int i;
        for(i = mids;i < mide; i++){
            if(mid[i] == last[lasts+mide-mids-1])break;
        }
        root.left = midAndLastCreate(mid,last,lasts,mids,i);//递归左子树
        root.right = midAndLastCreate(mid,last,lasts+i-mids,i+1,mide);//递归右子树
        return root;
    }
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值