【问题标题】:Forming a tree from an expression从表达式形成树
【发布时间】:2016-09-11 14:38:17
【问题描述】:

问题一棵树
可以用带括号的字符串表达式表示
(D(B(A)(C))(E()(F(G()(H)))))
注意:没有值的大括号,如上面表达式中的 (),表示一个空子指针,左或右,取决于符号的位置。

这是我目前所拥有的

static public Stack list = new Stack();
        static void Main(string[] args)
        {

            //ArrayList list = new ArrayList();
            StreamReader RW = new StreamReader("Trees.txt");
            //Stack checkList = new Stack();
            while(!RW.EndOfStream)
            {
                string tmp = RW.ReadLine();
                BTNode T = null;
                doTree(tmp, 0, ref T);
                //doTree(tmp, T, 0, null, checkList);
                //list.Add(T);


            }
        }
        static private void doTree(string str,int num, ref BTNode T)
        {
            if(num>=str.Length)
            {
                return;
            }

            if (str[num] == '(')
            {
                if (num == 0)
                {
                    T = new BTNode(str[num + 1]);

                    list.Push(T);
                    doTree(str, num + 2, ref T);
                }
                else if ((str[num + 1] != ')')&&(str[num+3]!='('))
                {
                    BTNode temp = new BTNode(str[num + 1]);
                    T.setLeft(temp);
                    temp.setParent(T);
                    list.Push(temp);
                    doTree(str, num + 2, ref temp);
                }
                else
                {
                    doTree(str, num + 1,ref T);


                }
            }
            else if ((((str[num] == ')'))&&(str[num+2]!=')')))
            {


                BTNode par = (BTNode)list.Pop();
                BTNode temp = new BTNode(str[num +2]);
                par.setRight(temp);
                temp.setParent(par);
                doTree(str, num + 4, ref temp);
            }
            else
            {
                doTree(str, num + 6, ref T);
                list.Pop();
            }


        }

谁能帮忙

【问题讨论】:

    标签: java tree binary-tree


    【解决方案1】:

    类似这样的:

    public static class Node {
    
        String value;
        Node[] children = null;
    }
    
    // Recursive method getTree
    // s is the binary-tree expresion.
    // index[0] is the curren position to build the tree.
    public static Node getTree(String s, int[] index) { 
        int i = index[0] + 1;
        if (s.length() <= i || s.charAt(i) == ')') {
            // empty node found
            index[0]+=2;
            return null;
        };
        Node n = new Node();
        char c = s.charAt(i);
        while (i < s.length()) {
            c = s.charAt(i);
            if (c == '(' || c == ')'){
                break;
            }
            i++;
        } 
        // this found the end of value to get it.
        n.value = s.substring(index[0] + 1, i);
        // increment index to next child or end node definition.
        index[0] = i;
        if (c == '(') {// the current node has children
            n.children = new Node[2];
            n.children[0] = getTree(s, index);
            n.children[1] = getTree(s, index);
        }
        // increment index for the last ')'
        index[0]++;
        return n;
    }
    

    使用它:

    public static void main(String[] args) {
        String s = "(D(B(A)(C))(E()(F(G)(H)))))";
        Node n = getTree(s, new int[]{0});
        System.out.println(n);
    }
    

    具有覆盖 toString 方法的节点:

    public static class Node {
        ...
        public String toString() {
            if (children == null){
                return "(" + value + ")";
            }
            return "(" + value + (children[0] == null ? "()" : children[0].toString()) + (children[1] == null ? "()" : children[1].toString()) + ")";
        }
    }
    

    打印出来:

    (D(B(A)(C))(E()(F(G)(H))))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多