根据先序遍历和中序遍历创建二叉树

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. class BiTree
  5. {
  6. private:
  7.     struct NODE
  8.     {
  9.         int data;
  10.         NODE* left_child;
  11.         NODE* right_child;
  12.         NODE(int data, NODE* left, NODE* right)
  13.         {
  14.             this->data = data;
  15.             this->left_child = left;
  16.             this->right_child = right;
  17.         }
  18.     };
  19.     NODE* root;
  20.     //first 和 middle 分别是同一颗二叉树的先序遍历和中序遍历  first和middle不能为空
  21.     NODE* getRootNode(const vector<int> &first, const vector<int> &middle )
  22.     {
  23.         vector<int> left_child_of_first;
  24.         vector<int> left_child_of_middle;
  25.         vector<int> right_child_of_first;
  26.         vector<int> right_child_of_middle;
  27.         int data_ = first[0];
  28.         vector<int>::const_iterator iter_target;
  29.         for (iter_target = middle.begin(); iter_target != middle.end(); ++iter_target)
  30.             if (data_ == (*iter_target))
  31.                 break;
  32.         for (vector<int>::const_iterator it = middle.begin(); it != iter_target; ++it)
  33.             left_child_of_middle.push_back(*it);
  34.         for (vector<int>::const_iterator it = iter_target+1; it != middle.end(); ++it)
  35.             right_child_of_middle.push_back(*it);
  36.         vector<int>::const_iterator it = first.begin() + 1;
  37.         while (left_child_of_first.size() < left_child_of_middle.size())
  38.         {
  39.             left_child_of_first.push_back(*it);
  40.             it++;
  41.         }
  42.         
  43.         while (right_child_of_first.size() < right_child_of_middle.size())
  44.         {
  45.             right_child_of_first.push_back(*it);
  46.             it++;
  47.         }
  48.         NODE* ptr;
  49.         if (1 == first.size())
  50.             return ptr = new NODE(data_, 0, 0);
  51.         if ((0 == left_child_of_first.size())&&(0 != right_child_of_first.size()))
  52.             return ptr = new NODE(data_, 0 ,getRootNode(right_child_of_first, right_child_of_middle));
  53.         else if ((0 != left_child_of_first.size())&&(0 == right_child_of_first.size()))
  54.             return ptr = new NODE(data_, getRootNode(left_child_of_first, left_child_of_middle), 0);
  55.         else 
  56.             return ptr = new NODE(data_, getRootNode(left_child_of_first, left_child_of_middle), getRootNode(right_child_of_first, right_child_of_middle));
  57.         //等效于上面的return语句,不过对于上面的语句,不同的编译器,getRootNode的计算顺序并不相同 ,在VS2003上,先调用后面的getRootNode函数
  58.         /*
  59.         {
  60.             NODE* p_left;
  61.             NODE* p_right;
  62.             p_left = getRootNode(left_child_of_first, left_child_of_middle);
  63.             p_right = getRootNode(right_child_of_first, right_child_of_middle);
  64.             
  65.             return ptr = new NODE(data_, p_left, p_right);
  66.         }
  67.         */
  68.     }
  69.     //释放内存
  70.     void delete_ptr(NODE* p)
  71.     {
  72.         if (p->left_child)
  73.             delete p->left_child;
  74.         if (p->right_child)
  75.             delete p->right_child;
  76.         delete p;
  77.     }
  78.     //打印后序遍历
  79.     void last_display(NODE* p)
  80.     {
  81.         if (p->left_child && p->right_child)
  82.         {
  83.             last_display(p->left_child);
  84.             last_display(p->right_child);
  85.             cout << (p->data) << "----";
  86.         }
  87.         if (p->left_child && (!p->right_child))
  88.         {
  89.             last_display(p->left_child);
  90.             cout << (p->data) << "----";
  91.         }
  92.         if (! p->left_child && (p->right_child))
  93.         {
  94.             last_display(p->right_child);
  95.             cout << (p->data) << "----";
  96.         }
  97.         if (!(p->left_child) && !(p->right_child))
  98.             cout << (p->data) << "----";
  99.     }
  100. public:
  101.     //构造函数(用先序遍历和中序遍历构造一个二叉树)
  102.     BiTree()
  103.     {
  104.         cout << "input the number of the bitree:";
  105.         int n=0; 
  106.         cin >> n;
  107.         cout << "input first sort:";
  108.         vector<int> first;
  109.         int data_;
  110.         for (int i=0; i < n; ++i)
  111.         {
  112.             cin >> data_;
  113.             first.push_back(data_);
  114.         }
  115.         cout << "input middle sort";
  116.         vector<int> middle;
  117.         for (int i=0; i < n; ++i)
  118.         {
  119.             cin >> data_;
  120.             middle.push_back(data_);
  121.         }
  122.         root = getRootNode(first, middle);  
  123.     }
  124.     void display()
  125.     {
  126.         NODE* p = root;
  127.         last_display(p);
  128.     }
  129.     
  130.     //析构函数
  131.     ~BiTree()
  132.     {
  133.         delete_ptr(root);
  134.     }
  135. };
  136. int main()
  137. {
  138.     BiTree bitree;
  139.     bitree.display();
  140.     system("PAUSE");
  141.     return 0;
  142. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值