二叉樹的非遞迴前序、中序以及後序遍歷C++模版類實現
阿新 • • 發佈:2019-02-08
#include <iostream> using namespace std; /////////////////////////////////////////////////////////////////////// //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; }