国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

C語言實現(xiàn)二叉樹遍歷的迭代算法

2020-01-26 15:18:23
字體:
供稿:網(wǎng)友

本文實例講述了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è)計的學(xué)習(xí)有一定的借鑒價值。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 临朐县| 南川市| 台南市| 邢台市| 商城县| 阳原县| 房山区| 四子王旗| 子洲县| 祥云县| 柳河县| 红安县| SHOW| 习水县| 瑞丽市| 宜宾县| 全南县| 逊克县| 湾仔区| 韶关市| 汤原县| 措美县| 柘荣县| 潼关县| 德惠市| 鄱阳县| 扶风县| 北京市| 清涧县| 临桂县| 巫山县| 红桥区| 大化| 娄烦县| 同德县| 章丘市| 宁都县| 眉山市| 沁阳市| 中西区| 临沧市|