C語言實現(xiàn)二叉樹遍歷的迭代算法
本文實例講述了C語言實現(xiàn)二叉樹遍歷的迭代算法,是數(shù)據(jù)結(jié)構(gòu)算法中非常經(jīng)典的一類算法。分享給大家供大家參考。
具體實現(xiàn)方法如下:
二叉樹中序遍歷的迭代算法:
#include <iostream> #include <stack> using namespace std; struct Node { Node(int i, Node* l = NULL, Node* r = NULL) : item(i), left(l), right(r) {} int item; Node* left; Node* right; }; Node* construct() { Node* node6 = new Node(16); Node* node5 = new Node(12); Node* node4 = new Node(8); Node* node3 = new Node(4); Node* node2 = new Node(14, node5, node6); Node* node1 = new Node(6, node3, node4); Node* node0 = new Node(10, node1, node2); return node0; } //遞歸算法 void inorder(Node *root) { if (root == NULL) return; inorder(root->left); cout << root->item << " "; inorder(root->right); } void preorder(Node *root) { if(root == NULL) return; cout << root->item << " "; preorder(root->left); preorder(root->right); } void postorder(Node *root) { if (root == NULL) return; postorder(root->left); postorder(root->right); cout << root->item << " "; } void postorder2(Node *root) { if (root == NULL) return; stack<Node *> nstack; Node *pre = NULL; nstack.push(root); Node *node = NULL; while (!nstack.empty()) { node = nstack.top(); if (pre != node->left && pre != node->right) { if (node->right) nstack.push(node->right); if (node->left) nstack.push(node->left); } if (node->left == NULL && node->right == NULL || pre == node->left || pre == node->right) { cout << node->item << " "; nstack.pop(); } pre = node; } } void preorder2(Node *root) { if(root == NULL) return; stack<Node *> nstack; Node *node = root; while (node != NULL || !nstack.empty()) { while(node != NULL) { cout << node->item << " "; nstack.push(node); node = node->left; } node = nstack.top(); nstack.pop(); node = node->right; } } void preorder3(Node *root) { if (root == NULL) return; stack<Node *> nstack; nstack.push(root); Node *node = NULL; while (!nstack.empty()) { node = nstack.top(); nstack.pop(); cout << node->item << " "; if (node->right) nstack.push(node->right); if (node->left) nstack.push(node->left); } } //迭代算法 void inorder2(Node *root) { if(root == NULL) return; stack<Node *> nstack; nstack.push(root); Node *next = root->left; while (next != NULL || !nstack.empty()) { while (next != NULL) { nstack.push(next); next = next->left; } next = nstack.top(); nstack.pop(); cout << next->item << " "; next = next->right; } } int main() { Node *root = construct(); cout << "---------中序遍歷遞歸---------" << endl; inorder(root); cout << endl; cout << "---------中序遍歷迭代---------" << endl; inorder2(root); cout << endl; cout << "---------先序遍歷遞歸---------" << endl; preorder(root); cout << endl; cout << "---------先序遍歷迭代1---------" << endl; preorder2(root); cout << endl; cout << "---------先序遍歷迭代2---------" << endl; preorder3(root); cout << endl; cout << "---------后序遍歷遞歸---------" << endl; postorder(root); cout << endl; cout << "---------后序遍歷迭代---------" << endl; postorder2(root); }
關(guān)于前序遍歷,后來又寫的算法如下,供大家參考:
void preOrderIterator(Node *root) { if (root == NULL) return; stack<Node*> nstack; nstack.push(root); while (!nstack.empty()) { Node *top = nstack.top(); while (top != NULL) { if (top->left) nstack.push(top->left); cout << top->data << " "; top = top->left; } while (top == NULL && !nstack.empty()) { top = nstack.top()->right; nstack.pop(); } if (top != NULL) nstack.push(top); } }
相信本文所述對大家C程序算法設(shè)計的學習有一定的借鑒價值。
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結(jié)
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什