zoukankan      html  css  js  c++  java
  • 数据结构-二叉树相关问题及解答【3】

    1,根据字符串输出一个【前,中,后,层】二叉排序树

      在某个存储介质以如下的形式保存一颗二叉树

    1(2(3,4(,5)),6(7,))

    观察后发现,每个节点的格式为

    X,X可以为空

    或者X(Y,Z),其中X不可以为空

    请输出上述二叉树的前、中、后、层遍历。

    package com.cnblogs.mufasa.demo1;
    
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Queue;
    
    class Node{
        int l;
        int r;
        public Node(int l,int r){
            this.l=l;
            this.r=r;
        }
    }
    
    public class myTree {
        String str;
        String ans;
        Queue<Node> queue= new LinkedList<Node>();
        public myTree(String str){
            this.str=str;
        }
        public String normalOut(){
            ans="";
            int len=str.length();
            deal(0,len-1);
            return ans;
        }
    
        private void deal(int l,int r){
            if(l>r){
                return;
            }
            int cont=0,mid=-1;
            for(int i=l+2;i<r;i++){//示例:1(2(3,4(,5)),6(7,))
                if(str.charAt(i)=='(')
                    cont++;
    
                if(str.charAt(i)==','&& cont==0){
                    mid=i;
                    break;
                }
    
                if(str.charAt(i) == ')')
                    cont--;
            }
            if(mid!=-1){
                ans=ans+str.charAt(l);//前序遍历
                deal(l+2,mid-1);
    //            ans=ans+str.charAt(l);//中序遍历
                deal(mid+1,r-1);
    //            ans=ans+str.charAt(l);//后续遍历
            }else {
                ans=ans+str.charAt(l);
            }
        }
    
        public String leveOut(){
            ans="";
            int len=str.length();
            queue.add(new Node(0,str.length()-1));
            while (queue.size()!=0){
                deal1();
            }
            return ans;
        }
    
        private void deal1(){
            Node pre=queue.poll();
            int l=pre.l,r=pre.r;
            if(l>r){
                return;
            }
            int cont=0,mid=-1;
            for(int i=l+2;i<r;i++){//示例:1(2(3,4(,5)),6(7,))
                if(str.charAt(i)=='(')
                    cont++;
    
                if(str.charAt(i)==','&& cont==0){
                    mid=i;
                    break;
                }
    
                if(str.charAt(i) == ')')
                    cont--;
            }
            if(mid!=-1){
                ans=ans+str.charAt(l);//层级遍历
                queue.add(new Node(l+2,mid-1));
                queue.add(new Node(mid+1,r-1));
            }else {
                ans=ans+str.charAt(l);
            }
        }
    
    
        public static void main(String[] args){
            String str="1(2(3,4(,5)),6(7,))";
            myTree tree=new myTree(str);
            System.out.println(tree.normalOut());
            System.out.println(tree.leveOut());
        }
    }
    View Code

    2,其他问题待

  • 相关阅读:
    [USACO18OPEN]Talent Show
    Linux关机、重启命令
    [SHOI2014]概率充电器
    mount新磁盘
    [JLOI2012]时间流逝
    创建、删除swap分区
    牛客网NOIP赛前集训营-普及组(第二场)
    从show slave status 中1062错误提示信息找到binlog的SQL
    [USACO18OPEN]Out of Sorts P 冒泡排序理解之二
    ORA-28040: No matching authentication protocol
  • 原文地址:https://www.cnblogs.com/Mufasa/p/11478464.html
Copyright © 2011-2022 走看看