根据二叉树的前序(先序)遍历和中序遍历,求二叉树的后序遍历结果。使用Java语言实现。

 
  
  1. // TODO: Auto-generated Javadoc  
  2. class BTreeNode {  
  3.     public char data; // 数据  
  4.     public BTreeNode left; // 左子树  
  5.     public BTreeNode right;// 右子树  
  6.  
  7.     /**  
  8.      * Instantiates a new b tree node.  
  9.      *  
  10.      * @param data  
  11.      *            the data  
  12.      */ 
  13.     public BTreeNode(char data) {  
  14.         this.data = data;  
  15.         this.left = null;  
  16.         this.right = null;  
  17.     }  
  18. }  
  19.  
  20. /**  
  21.  * The Class Btree.  
  22.  */ 
  23. public class Btree {  
  24.  
  25.     /**  
  26.      * The main method.  
  27.      *  
  28.      * @param args the arguments  
  29.      */ 
  30.     public static void main(String[] args) {  
  31.         char pre[] = "ABDGCEFH".toCharArray();  
  32.         char in[] = "DGBAECHF".toCharArray();  
  33.         if (args.length == 2) {  
  34.             pre = args[0].toCharArray();  
  35.             in = args[1].toCharArray();  
  36.         } else if (args.length != 0) {  
  37.             System.out.println("USAGE : java Btree \n ");  
  38.             System.out.println("        or \n ");  
  39.             System.out.println("        java Btree preorder midorder\n ");  
  40.             System.exit(1);  
  41.         }  
  42.         BTreeNode btree = null;  
  43.         btree = createBTree(btree, pre, in);  
  44.         System.out.println("Max Level :" + getDepth(btree));  
  45.         System.out.println("    Before:" + String.valueOf(pre));  
  46.         System.out.println("    Middle:" + String.valueOf(in));  
  47.         System.out.println("Our After :" + getAfterOrder(btree));  
  48.         System.out.println("Our Before:" + getBeforeOrder(btree));  
  49.         System.out.println("Our Middle:" + getMiddleOrder(btree));  
  50.         printBTree(btree, 0);  
  51.     }  
  52.  
  53.     /**  
  54.      * 打印二叉树,当不存在数据时打印*,以根->左子树->右子树(先序遍历)方式打印.  
  55.      *  
  56.      * @param btree the btree  
  57.      * @param level the level  
  58.      */ 
  59.     public static void printBTree(BTreeNode btree, int level) {  
  60.         for (int i = 0; i < level; i++) {  
  61.             System.out.print("--");  
  62.         }  
  63.         if (btree != null) {  
  64.             System.out.println(btree.data);  
  65.         } else {  
  66.             System.out.println("*");  
  67.             return;  
  68.         }  
  69.         printBTree(btree.left, level + 1);  
  70.         printBTree(btree.right, level + 1);  
  71.     }  
  72.  
  73.     /**  
  74.      * 创建二叉树.  
  75.      *  
  76.      * @param btree  
  77.      *            the btree  
  78.      * @param pre  
  79.      *            the pre  
  80.      * @param in  
  81.      *            the in  
  82.      * @return the b tree node  
  83.      */ 
  84.     public static BTreeNode createBTree(BTreeNode btree, char[] pre, char[] in) {  
  85.         if (in == null || in.length == 0 || pre == null || pre.length == 0) {  
  86.             return null;  
  87.         }  
  88.         char root = pre[0];  
  89.         char[] leftin = getLeftin(root, in);  
  90.         char[] rightin = getRightin(root, in);  
  91.         char[] leftpre = getLeftPre(pre, leftin);  
  92.         char[] rightpre = getRightPre(pre, rightin);  
  93.         if (btree == null) {  
  94.             btree = new BTreeNode(root);  
  95.         }  
  96.         if (leftpre.length > 0)  
  97.             btree.left = createBTree(btree.left, leftpre, leftin);  
  98.         if (rightpre.length > 0)  
  99.             btree.right = createBTree(btree.right, rightpre, rightin);  
  100.         return btree;  
  101.     }  
  102.  
  103.     /**  
  104.      * 取得左子树的先序遍历结果.  
  105.      *  
  106.      * @param pre  
  107.      *            the pre  
  108.      * @param in  
  109.      *            the in  
  110.      * @return the left pre  
  111.      */ 
  112.     public static char[] getLeftPre(char[] pre, char[] in) {  
  113.         String str = String.valueOf(pre);  
  114.         return str.substring(11 + in.length).toCharArray();  
  115.     }  
  116.  
  117.     /**  
  118.      * 取得右子树的先序遍历结果.  
  119.      *  
  120.      * @param pre  
  121.      *            the pre  
  122.      * @param in  
  123.      *            the in  
  124.      * @return the right pre  
  125.      */ 
  126.     public static char[] getRightPre(char[] pre, char[] in) {  
  127.         String str = String.valueOf(pre);  
  128.         return str.substring(pre.length - in.length).toCharArray();  
  129.     }  
  130.  
  131.     /**  
  132.      * 取得左子树的中序遍历结果.  
  133.      *  
  134.      * @param root  
  135.      *            the root  
  136.      * @param in  
  137.      *            the in  
  138.      * @return the leftin  
  139.      */ 
  140.     public static char[] getLeftin(char root, char[] in) {  
  141.         String str = String.valueOf(in);  
  142.         int pos = str.indexOf(root);  
  143.         if (pos == -1) {  
  144.             return null;  
  145.         }  
  146.         return str.substring(0, pos).toCharArray();  
  147.     }  
  148.  
  149.     /**  
  150.      * 取得右子树的中序遍历结果.  
  151.      *  
  152.      * @param root  
  153.      *            the root  
  154.      * @param in  
  155.      *            the in  
  156.      * @return the rightin  
  157.      */ 
  158.     public static char[] getRightin(char root, char[] in) {  
  159.         String str = String.valueOf(in);  
  160.         int pos = str.indexOf(root);  
  161.         if (pos == -1)  
  162.             return null;  
  163.         if (pos == str.length())  
  164.             return null;  
  165.         return str.substring(pos + 1).toCharArray();  
  166.     }  
  167.  
  168.     /**  
  169.      * 得到二叉树的后序遍历结果.  
  170.      *  
  171.      * @param btree  
  172.      *            the btree  
  173.      * @return the after order  
  174.      */ 
  175.     public static String getAfterOrder(BTreeNode btree) {  
  176.         String str = "";  
  177.         if (btree.left == null && btree.right == null) {  
  178.             str = str + btree.data;  
  179.         } else if (btree.left == null) {  
  180.             str = str + getAfterOrder(btree.right) + btree.data;  
  181.         } else if (btree.right == null) {  
  182.             str = str + getAfterOrder(btree.left) + btree.data;  
  183.         } else {  
  184.             str = str + getAfterOrder(btree.left) + getAfterOrder(btree.right)  
  185.                     + btree.data;  
  186.         }  
  187.         return str;  
  188.     }  
  189.  
  190.     /**  
  191.      * 得到二叉树的先序遍历结果.  
  192.      *  
  193.      * @param btree  
  194.      *            the btree  
  195.      * @return the after order  
  196.      */ 
  197.     public static String getBeforeOrder(BTreeNode btree) {  
  198.         String str = "";  
  199.         if (btree.left == null && btree.right == null) {  
  200.             str = str + btree.data;  
  201.         } else if (btree.left == null) {  
  202.             str = str + btree.data + getBeforeOrder(btree.right);  
  203.         } else if (btree.right == null) {  
  204.             str = str + btree.data + getBeforeOrder(btree.left);  
  205.         } else {  
  206.             str = str + btree.data + getBeforeOrder(btree.left)  
  207.                     + getBeforeOrder(btree.right);  
  208.         }  
  209.         return str;  
  210.     }  
  211.  
  212.     /**  
  213.      * 得到二叉树的中序遍历结果.  
  214.      *  
  215.      * @param btree  
  216.      *            the btree  
  217.      * @return the after order  
  218.      */ 
  219.     public static String getMiddleOrder(BTreeNode btree) {  
  220.         String str = "";  
  221.         if (btree.left == null && btree.right == null) {  
  222.             str = str + btree.data;  
  223.         } else if (btree.left == null) {  
  224.             str = str + btree.data + getMiddleOrder(btree.right);  
  225.         } else if (btree.right == null) {  
  226.             str = str + getMiddleOrder(btree.left) + btree.data;  
  227.         } else {  
  228.             str = str + getMiddleOrder(btree.left) + btree.data  
  229.                     + getMiddleOrder(btree.right);  
  230.         }  
  231.         return str;  
  232.     }  
  233.  
  234.     /**  
  235.      * 取得二叉树的深度.  
  236.      *  
  237.      * @param btree  
  238.      *            the btree  
  239.      * @return the depth  
  240.      */ 
  241.     public static int getDepth(BTreeNode btree) {  
  242.         int rtnleft = 1;  
  243.         int rtnright = 1;  
  244.         if (btree == null) {  
  245.             return 0;  
  246.         }  
  247.         rtnleft = getDepth(btree.left);  
  248.         rtnright = getDepth(btree.right);  
  249.  
  250.         return rtnleft > rtnright ? rtnleft + 1 : rtnright + 1;  
  251.     }  
  252.  
  253. }