根据二叉树的前序(先序)遍历和中序遍历,求二叉树的后序遍历结果。使用Java语言实现。
- // TODO: Auto-generated Javadoc
- class BTreeNode {
- public char data; // 数据
- public BTreeNode left; // 左子树
- public BTreeNode right;// 右子树
- /**
- * Instantiates a new b tree node.
- *
- * @param data
- * the data
- */
- public BTreeNode(char data) {
- this.data = data;
- this.left = null;
- this.right = null;
- }
- }
- /**
- * The Class Btree.
- */
- public class Btree {
- /**
- * The main method.
- *
- * @param args the arguments
- */
- public static void main(String[] args) {
- char pre[] = "ABDGCEFH".toCharArray();
- char in[] = "DGBAECHF".toCharArray();
- if (args.length == 2) {
- pre = args[0].toCharArray();
- in = args[1].toCharArray();
- } else if (args.length != 0) {
- System.out.println("USAGE : java Btree \n ");
- System.out.println(" or \n ");
- System.out.println(" java Btree preorder midorder\n ");
- System.exit(1);
- }
- BTreeNode btree = null;
- btree = createBTree(btree, pre, in);
- System.out.println("Max Level :" + getDepth(btree));
- System.out.println(" Before:" + String.valueOf(pre));
- System.out.println(" Middle:" + String.valueOf(in));
- System.out.println("Our After :" + getAfterOrder(btree));
- System.out.println("Our Before:" + getBeforeOrder(btree));
- System.out.println("Our Middle:" + getMiddleOrder(btree));
- printBTree(btree, 0);
- }
- /**
- * 打印二叉树,当不存在数据时打印*,以根->左子树->右子树(先序遍历)方式打印.
- *
- * @param btree the btree
- * @param level the level
- */
- public static void printBTree(BTreeNode btree, int level) {
- for (int i = 0; i < level; i++) {
- System.out.print("--");
- }
- if (btree != null) {
- System.out.println(btree.data);
- } else {
- System.out.println("*");
- return;
- }
- printBTree(btree.left, level + 1);
- printBTree(btree.right, level + 1);
- }
- /**
- * 创建二叉树.
- *
- * @param btree
- * the btree
- * @param pre
- * the pre
- * @param in
- * the in
- * @return the b tree node
- */
- public static BTreeNode createBTree(BTreeNode btree, char[] pre, char[] in) {
- if (in == null || in.length == 0 || pre == null || pre.length == 0) {
- return null;
- }
- char root = pre[0];
- char[] leftin = getLeftin(root, in);
- char[] rightin = getRightin(root, in);
- char[] leftpre = getLeftPre(pre, leftin);
- char[] rightpre = getRightPre(pre, rightin);
- if (btree == null) {
- btree = new BTreeNode(root);
- }
- if (leftpre.length > 0)
- btree.left = createBTree(btree.left, leftpre, leftin);
- if (rightpre.length > 0)
- btree.right = createBTree(btree.right, rightpre, rightin);
- return btree;
- }
- /**
- * 取得左子树的先序遍历结果.
- *
- * @param pre
- * the pre
- * @param in
- * the in
- * @return the left pre
- */
- public static char[] getLeftPre(char[] pre, char[] in) {
- String str = String.valueOf(pre);
- return str.substring(1, 1 + in.length).toCharArray();
- }
- /**
- * 取得右子树的先序遍历结果.
- *
- * @param pre
- * the pre
- * @param in
- * the in
- * @return the right pre
- */
- public static char[] getRightPre(char[] pre, char[] in) {
- String str = String.valueOf(pre);
- return str.substring(pre.length - in.length).toCharArray();
- }
- /**
- * 取得左子树的中序遍历结果.
- *
- * @param root
- * the root
- * @param in
- * the in
- * @return the leftin
- */
- public static char[] getLeftin(char root, char[] in) {
- String str = String.valueOf(in);
- int pos = str.indexOf(root);
- if (pos == -1) {
- return null;
- }
- return str.substring(0, pos).toCharArray();
- }
- /**
- * 取得右子树的中序遍历结果.
- *
- * @param root
- * the root
- * @param in
- * the in
- * @return the rightin
- */
- public static char[] getRightin(char root, char[] in) {
- String str = String.valueOf(in);
- int pos = str.indexOf(root);
- if (pos == -1)
- return null;
- if (pos == str.length())
- return null;
- return str.substring(pos + 1).toCharArray();
- }
- /**
- * 得到二叉树的后序遍历结果.
- *
- * @param btree
- * the btree
- * @return the after order
- */
- public static String getAfterOrder(BTreeNode btree) {
- String str = "";
- if (btree.left == null && btree.right == null) {
- str = str + btree.data;
- } else if (btree.left == null) {
- str = str + getAfterOrder(btree.right) + btree.data;
- } else if (btree.right == null) {
- str = str + getAfterOrder(btree.left) + btree.data;
- } else {
- str = str + getAfterOrder(btree.left) + getAfterOrder(btree.right)
- + btree.data;
- }
- return str;
- }
- /**
- * 得到二叉树的先序遍历结果.
- *
- * @param btree
- * the btree
- * @return the after order
- */
- public static String getBeforeOrder(BTreeNode btree) {
- String str = "";
- if (btree.left == null && btree.right == null) {
- str = str + btree.data;
- } else if (btree.left == null) {
- str = str + btree.data + getBeforeOrder(btree.right);
- } else if (btree.right == null) {
- str = str + btree.data + getBeforeOrder(btree.left);
- } else {
- str = str + btree.data + getBeforeOrder(btree.left)
- + getBeforeOrder(btree.right);
- }
- return str;
- }
- /**
- * 得到二叉树的中序遍历结果.
- *
- * @param btree
- * the btree
- * @return the after order
- */
- public static String getMiddleOrder(BTreeNode btree) {
- String str = "";
- if (btree.left == null && btree.right == null) {
- str = str + btree.data;
- } else if (btree.left == null) {
- str = str + btree.data + getMiddleOrder(btree.right);
- } else if (btree.right == null) {
- str = str + getMiddleOrder(btree.left) + btree.data;
- } else {
- str = str + getMiddleOrder(btree.left) + btree.data
- + getMiddleOrder(btree.right);
- }
- return str;
- }
- /**
- * 取得二叉树的深度.
- *
- * @param btree
- * the btree
- * @return the depth
- */
- public static int getDepth(BTreeNode btree) {
- int rtnleft = 1;
- int rtnright = 1;
- if (btree == null) {
- return 0;
- }
- rtnleft = getDepth(btree.left);
- rtnright = getDepth(btree.right);
- return rtnleft > rtnright ? rtnleft + 1 : rtnright + 1;
- }
- }
转载于:https://blog.51cto.com/3353175/980906