SOS!!!建树难题!!困扰好几天了,帮忙解决者,给500分!
我要根据用户的输入情况,建立一棵二叉树,用户可选择输入的操作符有: and, or, xor, not, "(", ")"等
其中and, or, xor优先级相同, not优先级高,
由于用户输入的情况非常复杂, 下面我根据自己的想象列出了以下29种情况; 请问该如何建立二叉树啊??? 有没有什么现成的原码处理这样的情况的!
(说明1:建树时, 等到的表达时的顺序为自然顺序(各原子条件和操作符是按照表达式的自然顺序一个个读取,再来建树的,就是说如果表达式为 (not A) and B, 那么建树过程中, 读取的顺序就是:
"(", "not", "A", ")", "and", "B" ---一边读,一边建树)
(说明2: not操作符是这样的, 它只有右child, 无左child; 例如: not A, 那么树是这样的: 根为not, 一个右child是A)
(说明3: 以下A,B, C,D,E等具体意义可以不管, 就当是原子条件好了)
(说明4: 左右括号在树上不用表现出来, 如(not A) and B, 那么树应该如下的样子:
and
/ \
not B
\
A
1. A and B or C
2. A and B or C xor D
3. A and ( B or C)
4. (A or B) and C
5. (A and B) or ( C xor D)
6. A and ( B or ( C xor D))
7. ((A and B) or C) xor D
8. (A and (B or C)) xor D
9. not A
10. A
11. A and B
12. (not A) and B
13. (not A)
14. not (A and B)
15. A and (not B)
16. A and ( not (B or C))
17. A and ((B or C) xor D)
18. A and (((B or C) xor D) and E)
19. (((A and B) or C) xor D) and E
20. not A and B
21. not not A
22. not not not A and B or C
23. not (not A) and B or C
24. (not A and not B) or C
25. ((not A) or (not B)) xor C
26. (not (A and B) or C) xor (not D and not not E)
27. not A and ((not B) or C) xor D
28. not (not ((not A) and B) or not C) xor D
29. A and B or not not not not C xor not (not D) or E