从表达式形成一棵树

时间:2016-09-11 14:38:17

标签: java tree binary-tree

问题树树 可以用带括号的字符串表达式来表示 (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();
            }


        }

有人可以提供帮助

1 个答案:

答案 0 :(得分:0)

这样的事情:

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))))