Reconstruct a binary tree through its pre- and mid-order traversal sequences

When first learned binary tree at school, given a binary tree, there are three basic orders to traverse all its nodes (in fact there are more than three orders): pre-order, mid-order, and post-order. Suppose that we have pre-order and mid-order at hand, the binary tree can be reconstructed and its post-order traversal can be generated (Notice that post-order and mid-order can also provide enough information to determine the binary tree whereas only pre-order and post-order cannot, because such situation that a non-leaf node has only one subtree exists and it cannot be determined whether the subtree is left child or right child). Below is the c++ implementation.

 

  1. #include <iostream>
  2. #include <assert.h>
  3. using namespace std;
  4. template <typename T>
  5. struct Node {
  6.   T data;
  7.   Node * left;
  8.   Node * right;
  9. };
  10. // Find the index of an element in a sequence within a certain length.
  11. template <typename T>
  12. int IndexOf(T* sequence, int length, T element) {
  13.   for(int i = 0; i < length; ++i) {
  14.     if(element == sequence[i]) {
  15.       return i;
  16.     }
  17.   }
  18.   return -1;
  19. }
  20. // Reconstruct a binary tree through its pre- and mid-order traversal.
  21. template <typename T>
  22. Node<T>* TreeReconstruction(T* preOrder, T* midOrder, int length) {
  23.   if(length == 0) {
  24.     return NULL;
  25.   }
  26.   Node<T>* root = new Node<T>();
  27.   root->data = preOrder[0];
  28.   int index = IndexOf(midOrder, length, preOrder[0]); // Find index of root in mid-order sequence.
  29.   assert(index != -1);
  30.   root->left = TreeReconstruction(preOrder + 1, midOrder, index); // Reconstruct root's left child.
  31.   root->right = TreeReconstruction(preOrder + 1 + index, midOrder +1 + index, length - 1 - index); //Reconstruct root's right child.
  32.   return root;
  33. }
  34. // Print the post-order traversal of a binary tree.
  35. template <typename T>
  36. void PrintPostOrder(Node<T>* root) {
  37.   if(root == NULL) {
  38.     return;
  39.   }
  40.   PrintPostOrder(root->left);
  41.   PrintPostOrder(root->right);
  42.   cout << root->data;
  43. }
  44. // Test code: main.
  45. int main(int argc, char ** argv) {
  46.   int pre[8] = {1,2,4,5,7,8,3,6};
  47.   int mid[8] = {4,2,7,5,8,1,3,6};
  48.   Node<int>* tree = TreeReconstruction(pre, mid, 8);
  49.   PrintPostOrder(tree);
  50.   cout << endl;
  51.   return 0;
  52. }

The command line prints:

47852631

 

It works. As you see, recursive programming can be very helpful and useful when a data structure can be viewed as consisting of some similar substructures.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值