今天覆習了一下二叉樹的前序遍歷、中序遍歷、後序遍歷的遞歸與非遞歸算法,順便記錄一下:node
//TreeTest.h #include <iostream> struct TreeNode { int value; TreeNode* leftChild; TreeNode* rightChild; void print() { printf("%d ",value); } }; class MyTree { private: TreeNode* m_root; TreeNode* create(TreeNode*& root); void destroy(TreeNode* root); void preOrderRecursive(TreeNode* root); void inOrderRecursive(TreeNode* root); void postOrderRecursive(TreeNode* root); void levelOrderRecursive(TreeNode* root); public: MyTree(); ~MyTree(); TreeNode* getTreeNode(int value); void createTreeForPreOrder(); void preOrderRecursive(); void inOrderRecursive(); void postOrderRecursive(); void levelOrderRecursive(); //非遞歸遍歷 void preOrderUnRecursive(); void inOrderUnRecursive(); void postOrderUnRecursive(); }; //TreeTest.cpp #include "TreeTest.h" #include <iostream> #include <queue> #include <stack> using namespace std; MyTree::MyTree() : m_root(NULL) { } MyTree::~MyTree() { this->destroy(m_root); m_root = NULL; } void MyTree::destroy(TreeNode* root) { if (!root) return; TreeNode* left=NULL, *right=NULL; left = root->leftChild; right = root->rightChild; delete root; if (left) destroy(left); if (right) destroy(right); } TreeNode* MyTree::create(TreeNode*& root) { int value; cin>>value; if (value <= 0) return NULL; //小於或等於0表明NULL if (!root) root = this->getTreeNode(value); if (!root->leftChild) this->create(root->leftChild); if (!root->rightChild) this->create(root->rightChild); return root; } TreeNode* MyTree::getTreeNode(int value) { TreeNode* p = new TreeNode(); p->value = value; p->leftChild = p->rightChild = NULL; return p; } void MyTree::createTreeForPreOrder() { this->create(m_root); } void MyTree::preOrderRecursive(TreeNode* root) { if (!root) return; printf("%d ",root->value); if (root->leftChild) this->preOrderRecursive(root->leftChild); if (root->rightChild) this->preOrderRecursive(root->rightChild); } void MyTree::inOrderRecursive(TreeNode* root) { if (!root) return; if (root->leftChild) this->inOrderRecursive(root->leftChild); printf("%d ",root->value); if (root->rightChild) this->inOrderRecursive(root->rightChild); } void MyTree::postOrderRecursive(TreeNode* root) { if (!root) return; if (root->leftChild) this->postOrderRecursive(root->leftChild); if (root->rightChild) this->postOrderRecursive(root->rightChild); printf("%d ", root->value); } void MyTree::levelOrderRecursive(TreeNode* root) { queue<TreeNode*> Q; if (root) Q.push(root); while (!Q.empty()) { TreeNode* node = Q.front(); Q.pop(); node->print(); if (node->leftChild) Q.push(node->leftChild); if (node->rightChild) Q.push(node->rightChild); } } void MyTree::preOrderRecursive() { printf("遞歸前序遍歷:"); this->preOrderRecursive(m_root); printf("\n"); } void MyTree::inOrderRecursive() { printf("遞歸中序遍歷:"); this->inOrderRecursive(m_root); printf("\n"); } void MyTree::postOrderRecursive() { printf("遞歸後序遍歷:"); this->postOrderRecursive(m_root); printf("\n"); } void MyTree::levelOrderRecursive() { printf("層序遍歷:"); this->levelOrderRecursive(m_root); printf("\n"); } void MyTree::preOrderUnRecursive() { if (!m_root) return; printf("非遞歸先序遍歷:"); stack<TreeNode*> S; S.push(m_root); while (!S.empty()) { TreeNode* node = S.top(); S.pop(); node->print(); if (node->rightChild) S.push(node->rightChild); if (node->leftChild) S.push(node->leftChild); } printf("\n"); } void MyTree::inOrderUnRecursive() { if (!m_root) return; printf("非遞歸中序遍歷:"); stack<TreeNode*> S; TreeNode* p = m_root; while (p || !S.empty()) { while (p) { S.push(p); p = p->leftChild; } p = S.top(); S.pop(); p->print(); /*因爲是中序遍歷,那麼訪問了當前節點後, 下一步一定要訪問該節點的右子樹*/ p = p->rightChild; } printf("\n"); } void MyTree::postOrderUnRecursive() { if (!m_root) return; printf("非遞歸後序遍歷:"); stack<TreeNode*> S; TreeNode* p = m_root; TreeNode* q = NULL; while (p || !S.empty()) { while (p) { S.push(p); p = p->leftChild; } p = S.top(); /* 判斷表示當前節點的右節點沒有或已經訪問過 q表示上一次訪問的節點,因爲是後續遍歷, 若當前節點有右節點,那麼上一次訪問的節點必定是當前節點的右節點 */ if (!p->rightChild || p->rightChild == q) { p->print(); q = p; p = NULL; S.pop(); } else { p = p->rightChild; } } printf("\n"); } //main.cpp #include "TreeTest.h" int main() { MyTree tree; tree.createTreeForPreOrder(); tree.preOrderRecursive(); tree.preOrderUnRecursive(); tree.inOrderRecursive(); tree.inOrderUnRecursive(); tree.postOrderRecursive(); tree.postOrderUnRecursive(); tree.levelOrderRecursive(); getchar(); getchar(); return 0; }
測試用例:ios
1 2 3 -1 -1 4 -1 -1 5 6 -1 -1 7 8 -1 -1 9 -1 -1算法