随笔练习
import java.util.Stack;
public class ExpressionTree {
private TreeNode root;
private ExpressionTree() {
}
public static ExpressionTree createFromSuffix(String suffix) {
Stack<TreeNode> stack = new Stack<>();
char[] ops = suffix.toCharArray();
for (char op : ops) {
if (!isBasicOperator(op)) {
stack.push(new TreeNode(op + "", null, null));
} else {
TreeNode right = stack.pop();
TreeNode left = stack.pop();
TreeNode root = new TreeNode(op + "", left, right);
stack.push(root);
}
}
ExpressionTree expressionTree = new ExpressionTree();
expressionTree.root = stack.pop();
return expressionTree;
}
public String preOrder() {
StringBuilder res = new StringBuilder();
if (root == null) {
return res.toString();
}
TreeNode cur = root;
while (cur != null) {
if (cur.left != null) {
TreeNode mostRight = cur.left;
while (mostRight.right != null && mostRight.right != cur) {
mostRight = mostRight.right;
}
if (mostRight.right == null) {
res.append(cur.data);
mostRight.right = cur;
cur = cur.left;
continue;
} else {
mostRight.right = null;
}
} else {
res.append(cur.data);
}
cur = cur.right;
}
return res.toString();
}
private TreeNode reverseWithRight(TreeNode root) {
TreeNode pre = null;
TreeNode cur = root;
TreeNode next;
while (cur != null) {
next = cur.right;
cur.right = pre;
pre = cur;
cur = next;
}
return pre;
}
public String postOrder() {
StringBuilder res = new StringBuilder();
if (root == null) {
return res.toString();
}
TreeNode cur = root;
while (cur != null) {
if (cur.left != null) {
TreeNode mostRight = cur.left;
while (mostRight.right != null && mostRight.right != cur) {
mostRight = mostRight.right;
}
if (mostRight.right == null) {
mostRight.right = cur;
cur = cur.left;
continue;
} else {
mostRight.right = null;
record(cur.left, res);
}
}
cur = cur.right;
}
record(root, res);
return res.toString();
}
private void record(TreeNode root, StringBuilder res) {
TreeNode reverse = reverseWithRight(root);
TreeNode cur = reverse;
while (cur != null) {
res.append(cur.data);
cur = cur.right;
}
reverseWithRight(reverse);
}
public String inOrder() {
StringBuilder res = new StringBuilder();
if (root == null) {
return res.toString();
}
TreeNode cur = root;
while (cur != null) {
if (cur.left != null) {
TreeNode mostRight = cur.left;
while (mostRight.right != null && mostRight.right != cur) {
mostRight = mostRight.right;
}
if (mostRight.right == null) {
mostRight.right = cur;
cur = cur.left;
continue;
} else {
res.append(cur.data);
mostRight.right = null;
}
} else {
res.append(cur.data);
}
cur = cur.right;
}
return res.toString();
}
private static boolean isBasicOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
private static class TreeNode {
public String data;
public TreeNode left;
public TreeNode right;
public TreeNode(String data, TreeNode left, TreeNode right) {
this.data = data;
this.left = left;
this.right = right;
}
}
public static ExpressionTree rebuildExpressionTree(String preOrder, String inOrder) {
TreeNode root = rebuildHelper(preOrder.toCharArray(),
0,
preOrder.length() - 1,
inOrder.toCharArray(),
0,
inOrder.length());
ExpressionTree expressionTree = new ExpressionTree();
expressionTree.root = root;
return expressionTree;
}
private static TreeNode rebuildHelper(char[] preOrder,
int preLeft,
int preRight,
char[] inOrder,
int inLeft,
int inRight) {
TreeNode root = new TreeNode(preOrder[preLeft] + "", null, null);
if (preLeft == preRight) {
return root;
}
int pos = Integer.MIN_VALUE;
for (int i = inLeft; i <= inRight; i++) {
if (inOrder[i] == preOrder[preLeft]) {
pos = i;
break;
}
}
if (pos == Integer.MIN_VALUE) {
throw new RuntimeException("构建失败");
}
int leftChildSize = pos - inLeft;
root.left = rebuildHelper(
preOrder,
preLeft + 1,
preLeft + leftChildSize,
inOrder,
inLeft,
pos - 1);
root.right = rebuildHelper(preOrder,
preLeft + leftChildSize + 1,
preRight,
inOrder,
pos + 1,
inRight);
return root;
}
public static void main(String[] args) {
ExpressionTree expressionTree = ExpressionTree.createFromSuffix("ab+cde+**");
System.out.println(expressionTree.preOrder());
System.out.println(expressionTree.inOrder());
System.out.println(expressionTree.postOrder());
ExpressionTree rebuild = ExpressionTree.rebuildExpressionTree("*+ab*c+de", "a+b*c*d+e");
System.out.println("重构:");
System.out.println(rebuild.preOrder());
System.out.println(rebuild.inOrder());
System.out.println(rebuild.postOrder());
}
}