- #include <iostream>
- #include <vector>
- using namespace std;
- class BiTree
- {
- private:
- struct NODE
- {
- int data;
- NODE* left_child;
- NODE* right_child;
- NODE(int data, NODE* left, NODE* right)
- {
- this->data = data;
- this->left_child = left;
- this->right_child = right;
- }
- };
- NODE* root;
- //first 和 middle 分别是同一颗二叉树的先序遍历和中序遍历 first和middle不能为空
- NODE* getRootNode(const vector<int> &first, const vector<int> &middle )
- {
- vector<int> left_child_of_first;
- vector<int> left_child_of_middle;
- vector<int> right_child_of_first;
- vector<int> right_child_of_middle;
- int data_ = first[0];
- vector<int>::const_iterator iter_target;
- for (iter_target = middle.begin(); iter_target != middle.end(); ++iter_target)
- if (data_ == (*iter_target))
- break;
- for (vector<int>::const_iterator it = middle.begin(); it != iter_target; ++it)
- left_child_of_middle.push_back(*it);
- for (vector<int>::const_iterator it = iter_target+1; it != middle.end(); ++it)
- right_child_of_middle.push_back(*it);
- vector<int>::const_iterator it = first.begin() + 1;
- while (left_child_of_first.size() < left_child_of_middle.size())
- {
- left_child_of_first.push_back(*it);
- it++;
- }
- while (right_child_of_first.size() < right_child_of_middle.size())
- {
- right_child_of_first.push_back(*it);
- it++;
- }
- NODE* ptr;
- if (1 == first.size())
- return ptr = new NODE(data_, 0, 0);
- if ((0 == left_child_of_first.size())&&(0 != right_child_of_first.size()))
- return ptr = new NODE(data_, 0 ,getRootNode(right_child_of_first, right_child_of_middle));
- else if ((0 != left_child_of_first.size())&&(0 == right_child_of_first.size()))
- return ptr = new NODE(data_, getRootNode(left_child_of_first, left_child_of_middle), 0);
- else
- return ptr = new NODE(data_, getRootNode(left_child_of_first, left_child_of_middle), getRootNode(right_child_of_first, right_child_of_middle));
- //等效于上面的return语句,不过对于上面的语句,不同的编译器,getRootNode的计算顺序并不相同 ,在VS2003上,先调用后面的getRootNode函数
- /*
- {
- NODE* p_left;
- NODE* p_right;
- p_left = getRootNode(left_child_of_first, left_child_of_middle);
- p_right = getRootNode(right_child_of_first, right_child_of_middle);
- return ptr = new NODE(data_, p_left, p_right);
- }
- */
- }
- //释放内存
- void delete_ptr(NODE* p)
- {
- if (p->left_child)
- delete p->left_child;
- if (p->right_child)
- delete p->right_child;
- delete p;
- }
- //打印后序遍历
- void last_display(NODE* p)
- {
- if (p->left_child && p->right_child)
- {
- last_display(p->left_child);
- last_display(p->right_child);
- cout << (p->data) << "----";
- }
- if (p->left_child && (!p->right_child))
- {
- last_display(p->left_child);
- cout << (p->data) << "----";
- }
- if (! p->left_child && (p->right_child))
- {
- last_display(p->right_child);
- cout << (p->data) << "----";
- }
- if (!(p->left_child) && !(p->right_child))
- cout << (p->data) << "----";
- }
- public:
- //构造函数(用先序遍历和中序遍历构造一个二叉树)
- BiTree()
- {
- cout << "input the number of the bitree:";
- int n=0;
- cin >> n;
- cout << "input first sort:";
- vector<int> first;
- int data_;
- for (int i=0; i < n; ++i)
- {
- cin >> data_;
- first.push_back(data_);
- }
- cout << "input middle sort";
- vector<int> middle;
- for (int i=0; i < n; ++i)
- {
- cin >> data_;
- middle.push_back(data_);
- }
- root = getRootNode(first, middle);
- }
- void display()
- {
- NODE* p = root;
- last_display(p);
- }
- //析构函数
- ~BiTree()
- {
- delete_ptr(root);
- }
- };
- int main()
- {
- BiTree bitree;
- bitree.display();
- system("PAUSE");
- return 0;
- }
根据先序遍历和中序遍历创建二叉树
最新推荐文章于 2023-05-15 18:22:08 发布