首页 > 代码库 > 二叉树的建立,非递归前序、中序、后续遍历
二叉树的建立,非递归前序、中序、后续遍历
算法网上很多,这里只是我手写的可执行的代码,备份用。
#include <iostream> #include <vector> #include<queue> using namespace std; struct node { char element; struct node * left; struct node * right; //struct node * parent; node (char a) { element = a; } }; struct node * rebuild ( char* preOrder, char* midOrder, int len) { if (len <= 0) { return NULL; } node * root = new node(preOrder[0]); int i = 0; for (; i<len; i++) { if (midOrder[i] == preOrder[0]) { cout << "i:" << i << " root:" << midOrder[i] << endl; break; } } node * left = rebuild(preOrder+1, midOrder, i); node * right = rebuild(preOrder+1+i, midOrder+1+i, len-1-i); root->left = left; root->right = right; return root; } void visit (node * root) { if (root) { cout << root->element << " "; } return; } void preOrder_travel(node* root) { if (!root) { return; } vector<node*> stack; while (root || !stack.empty()) { while (root) { visit(root); stack.push_back(root); root=root->left; } if (!stack.empty()) { root = *(stack.rbegin()); stack.pop_back(); root = root->right; } } return; } void midOrder_travel(node* root) { if (!root) { return; } vector<node*> stack; while (root || !stack.empty()) { while (root) { stack.push_back(root); root=root->left; } if (!stack.empty()) { root = *(stack.rbegin()); visit(root); stack.pop_back(); root = root->right; } } return; } void postOrder_travel(node* root) { if (!root) { return; } vector<node*> stack; stack.push_back(root); node * pre = root; while (!stack.empty()) { root = *(stack.rbegin()); if ((root->left == NULL && root->right == NULL) || root->left == pre|| root->right == pre) { pre = root; visit(root); stack.pop_back(); } else { if (root->right) { stack.push_back(root->right); } if (root->left) { stack.push_back(root->left); } } } return; } void level_travel(node* root) { if (!root) { return; } queue<node*> q; q.push(root); int level = 0; int cur = 0, last = 1; while (cur < q.size()) { cout << "level_" << level << ": "; while (cur < last) { root = q.front(); visit(root); if (root->left) { q.push(root->left); } if (root->right) { q.push(root->right); } q.pop(); cur++; } cur = 0; last = q.size(); level++; cout << endl; } } void select_level_travel(node* root, int select_level) { if (!root || select_level < 0) { return; } queue<node*> q; q.push(root); int level = 0; int cur = 0, last = 1; while (cur < q.size()) { if (select_level == level) { cout << "level_" << level << ": "; } while (cur < last) { root = q.front(); if (select_level == level) { visit(root); } if (root->left) { q.push(root->left); } if (root->right) { q.push(root->right); } q.pop(); cur++; } if (select_level == level) { cout << endl; } cur = 0; last = q.size(); level++; } } int main () { char preOrder[] = {‘a‘, ‘b‘, ‘d‘, ‘c‘, ‘e‘, ‘f‘}; char midOrder[] = {‘d‘, ‘b‘, ‘a‘, ‘e‘, ‘c‘, ‘f‘}; node * root = rebuild(preOrder, midOrder, sizeof(midOrder)); cout << endl; cout << "preOrder:"; preOrder_travel(root); cout << endl; cout << "midOrder:"; midOrder_travel(root); cout << endl; cout << "postOrder:"; postOrder_travel(root); cout << endl; cout << "levelOrder:" << endl;; level_travel(root); cout << endl; cout << "selectlevelOrder:" << endl;; select_level_travel(root, 2); cout << endl; return 0; }
本文出自 “天眼神童的新家” 博客,请务必保留此出处http://tianyanshentong.blog.51cto.com/3356396/1556752
二叉树的建立,非递归前序、中序、后续遍历
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。