二叉树的三种遍历的递归实现都很简单,但是在面试中,面试官一般都不会问你递归的实现,所以学习二叉树的非递归实现还是很重要的。
#include <iostream> using namespace std; //Author: 过往记忆 //Blog: www.iteblog.com //Email: wyphao.2007@163.com /////////////////////////////////////////////////////////////////////// //stack template <class T> class Stack{ public: Stack(int size = 50); ~Stack(); void push(T* data); T* pop(); bool isEmpty(); T* peek(); private: int size; int top; bool isFull(); T **data; }; template <class T> Stack<T>::Stack(int size){ if(size <= 0){ cout << "分配的内存太小了" << endl; } data = new T*[size]; top = -1; this->size = size; } template <class T> Stack<T>::~Stack(){ delete []data; } template <class T> void Stack<T>::push(T* data){ ++top; if(isFull()){ cout << "貌似栈满了耶" << endl; exit(1); } this->data[top] = data; } template <class T> T* Stack<T>::pop(){ if(isEmpty()){ cout << "栈为空,不可以再出元素了!" << endl; exit(1); } return data[top--]; } template <class T> T* Stack<T>::peek(){ if(isEmpty()){ cout << "栈为空" << endl; exit(1); } return data[top]; } template <class T> bool Stack<T>::isFull(){ if(top == size){ return true; } return false; } template <class T> bool Stack<T>::isEmpty(){ if(top < 0){ return true; } return false; } /////////////////////////////////////////////////////////////////////// //tree template <class T> class BTree{ public: BTree *left; BTree *right; T data; BTree() : left(NULL), right(NULL), data(NULL){}; ~BTree(){}; }; /////////////////////////////////////////////////////////////////////// template <class T> void PreOrder(BTree<T> *root){ if(root != NULL){ Stack<BTree<T> > stack ; BTree<T> *ptr = root; BTree<T> *temp; stack.push(ptr); while(!stack.isEmpty()) { temp = stack.pop(); cout << temp->data << " "; if(temp->right != NULL){ stack.push(temp->right); } if(temp->left != NULL){ stack.push(temp->left); } } cout << endl; } } /////////////////////////////////////////////////////////////////////// template <class T> void InOrder(BTree<T> *root){ if(root != NULL){ Stack<BTree<T> > stack ; BTree<T> *ptr = root; while(!stack.isEmpty() || ptr != NULL){ while(ptr != NULL){ stack.push(ptr); ptr = ptr->left; } if(!stack.isEmpty()){ ptr = stack.pop(); cout << ptr->data << " "; ptr = ptr->right; } } cout << endl; } } /////////////////////////////////////////////////////////////////////// template <class T> void PostOrder(BTree<T> *root){ if(root != NULL){ Stack<BTree<T> > stack; BTree<T> *ptr = root; BTree<T> *temp; bool flags; do{ while(ptr != NULL){ stack.push(ptr); ptr = ptr->left; } temp = NULL; flags = true; while(flags && !stack.isEmpty()){ ptr = stack.peek(); if(ptr->right == temp){ cout << ptr->data << " "; stack.pop(); temp = ptr; }else{ ptr = ptr->right; flags = false; } } }while(!stack.isEmpty()); cout << endl; } } /////////////////////////////////////////////////////////////////////// template <class T> void PreOrder1(BTree<T> * root){ if(root != NULL){ cout << root->data << " "; PreOrder1(root->left); PreOrder1(root->right); } } /////////////////////////////////////////////////////////////////////// template <class T> void InOrder1(BTree<T> * root){ if(root != NULL){ InOrder1(root->left); cout << root->data << " "; InOrder1(root->right); } } /////////////////////////////////////////////////////////////////////// template <class T> void PostOrder1(BTree<T> * root){ if(root != NULL){ PostOrder1(root->left); PostOrder1(root->right); cout << root->data << " "; } } /////////////////////////////////////////////////////////////////////// int main(){ BTree<int> *root = new BTree<int>; BTree<int> *A, *B, *C, *D, *E; A = new BTree<int>; B = new BTree<int>; C = new BTree<int>; D = new BTree<int>; E = new BTree<int>; A->data = 5; B->data = 6; C->data = 4; D->data = 2; E->data = 7; root = A; A->left = B; A->right = E; B->left = C; B->right = D; /*C->left = NULL; C->right = NULL; D->left = NULL; D->right = NULL; E->left = NULL; E->right = NULL;*/ cout << "非递归: " << endl; PreOrder(root); InOrder(root); PostOrder(root); cout << "递归: " << endl; PreOrder1(root); cout << endl; InOrder1(root); cout << endl; PostOrder1(root); cout << endl; return 0; }本博客文章除特别声明,全部都是原创!
原创文章版权归过往记忆大数据(过往记忆)所有,未经许可不得转载。
本文链接: 【二叉树的非递归前序、中序以及后序遍历C++模版类实现】(https://www.iteblog.com/archives/308.html)