c++二叉树的非递归建立和遍历
2014-09-04 20:37
148 查看
#include <iostream> #include <stack> using namespace std; template<typename T> struct BiTreeNode { T data; BiTreeNode *Lchild, *Rchild; BiTreeNode() :Lchild(NULL), Rchild(NULL){} BiTreeNode(const T& d, BiTreeNode *lchild = NULL, BiTreeNode *rchild = NULL) :data(d), Lchild(lchild), Rchild(rchild){} }; template<typename T> class BiTree { public: BiTree(){ root = NULL; } ~BiTree(){ /*makeEmpty();*/ } //void makeEmpty(BiTreeNode<T>* &root); void createBiTree(BiTreeNode<T>* &root); void output(BiTreeNode<T> *root); void preOrder(BiTreeNode<T>* &root); void preOrder_2(BiTreeNode<T>* &root); void levelOrder(BiTreeNode<T>* &root); void inOrder(BiTreeNode<T>* &root); void postOrder(BiTreeNode<T>* &root); BiTreeNode<T> * root; }; template<typename T> void BiTree<T>::createBiTree(BiTreeNode<T>* &root) { //按照广义表方式输入 stack<BiTreeNode<T>* >s; root = NULL; BiTreeNode<T> *p, *t; int k; char ch; cin >> ch; while (ch != '#') { switch (ch) { case '(': s.push(p); k = 1; break; case ')': t = s.top(); s.pop(); break; case ',': k = 2; break; default: p = new BiTreeNode<T>(ch); if (root == NULL) { root = p; } else if (k==1) { t=s.top(); t->Lchild = p; } else { t=s.top(); t->Rchild = p; } } cin >> ch; } cout << endl; } template<typename T> void BiTree<T>::output(BiTreeNode<T> *root) { if (root) { cout << root->data << " "; output(root->Lchild); output(root->Rchild); } } template<typename T> void BiTree<T>::preOrder(BiTreeNode<T>* &root) { //先存右结点,再存做结点,出栈真好相反:先出左结点,后出右结点 stack<BiTreeNode<T>* > s; BiTreeNode<T> * current; s.push(root); while (!s.empty()) { current = s.top(); s.pop(); cout << current->data << " "; if (current->Rchild) { s.push(current->Rchild); } if (current->Lchild) { s.push(current->Lchild); } } cout << endl; } template<typename T> void BiTree<T>::preOrder_2(BiTreeNode<T>* &root) { //只存右结点 stack<BiTreeNode<T>* > s; BiTreeNode<T>* current = root; s.push(NULL); while (current) { cout << current->data << " "; if (current->Rchild) { s.push(current->Rchild); } if (current->Lchild) { current = current->Lchild; } else { current = s.top(); s.pop(); } } cout << endl; } template<typename T> void BiTree<T>::levelOrder(BiTreeNode<T>* &root) { } template<typename T> void BiTree<T>::inOrder(BiTreeNode<T>* &root) { stack<BiTreeNode<T>* > s; BiTreeNode<T> *current = root; do { while (current) { s.push(current); current = current->Lchild; } if (!s.empty()) { current = s.top(); cout << current->data << " "; s.pop(); current = current->Rchild; } } while (current||!s.empty()); cout << endl; } /************************************************************************ //后序遍历(非递归形式) //思想:从根节点开始,向左走到尽头,并入栈,同时设置标志位为1. //出栈时如果这个节点有右子树,则判断是第几次访问,如果是第1次访问, //则不出栈,将标志位改为2;如果是第二次访问,则出栈。 ************************************************************************/ template<typename T> void BiTree<T>::postOrder(BiTreeNode<T>* &root) { stack<BiTreeNode<T>* > s; stack<int> flag_s; unsigned flag; BiTreeNode<T>* current = root; s.push(current); flag_s.push(1); while (!s.empty()) { while ( current && current->Lchild ) { current = current->Lchild; s.push(current); flag_s.push(1); } if (!s.empty()) { flag = flag_s.top(); flag_s.pop(); current = s.top(); if (current->Rchild && flag==1) { flag_s.push(2); current = current->Rchild; s.push(current); flag_s.push(1); } else { s.pop(); cout << current->data << " "; current = current->Rchild; } } } cout << endl; } void main() { BiTree<char>* bt = new BiTree<char>(); cout << "广义表的方式非递归建立二叉树:eg:A(B(D,E),C(,F)):" << endl; bt->createBiTree(bt->root); /*cout << endl << endl; bt->output(bt->root);*/ cout << "非递归先序(方法1):" << endl; bt->preOrder(bt->root); cout << "非递归先序(方法2):" << endl; bt->preOrder_2(bt->root); cout << "非递归中序遍历:" << endl; bt->inOrder(bt->root); cout << "非递归后序遍历:" << endl; bt->postOrder(bt->root); while (1) { } }
相关文章推荐
- 数据结构_树_二叉树的建立、遍历、复制与移除_二叉链表存储_C++实现
- C++数据结构--二叉树的建立,前序遍历,中序遍历和后序遍历
- 二叉树的建立与遍历(二)(c++实现)
- 二叉树的建立以及遍历C/C++
- 二叉树的建立与遍历(一)(c++实现)
- c++实现二叉树的非递归创建以及非递归先序、中序、后序遍历
- [置顶] 二叉树的非递归前序、中序以及后序遍历C++模版类实现
- (C++)二叉树的建立与递归方式遍历
- 二叉树的建立,以及非递归遍历
- C++二叉树的建立与遍历
- c++ 二叉树 的递归建立和递归遍历
- C++实现二叉树的建立和三种递归遍历
- C++二叉树之构造拷贝赋值,递归和非递归的前序遍历,中序遍历和后序遍历,以及层序遍历
- 二叉树的建立与递归遍历C++版
- C语言二叉树建立,遍历(递归与非递归),交换子树
- 数据结构与算法问题 二叉树的建立与遍历(含非递归)
- C++实现链式二叉树,采用非递归的方式先序,中序,后序遍历二叉树
- 二叉树建立,遍历和二叉排序树的判断【c++】
- 二叉树的先序建立与非递归遍历C++版
- C++二叉树的建立、前序、中序、后序遍历