C語言實(shí)現(xiàn)二叉樹遍歷的迭代算法
本文實(shí)例講述了C語言實(shí)現(xiàn)二叉樹遍歷的迭代算法,是數(shù)據(jù)結(jié)構(gòu)算法中非常經(jīng)典的一類算法。分享給大家供大家參考。
具體實(shí)現(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);
}
}
相信本文所述對(duì)大家C程序算法設(shè)計(jì)的學(xué)習(xí)有一定的借鑒價(jià)值。
相關(guān)文章
Qt數(shù)據(jù)庫應(yīng)用之實(shí)現(xiàn)數(shù)據(jù)分組導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了如何利用Qt實(shí)現(xiàn)數(shù)據(jù)庫數(shù)據(jù)分組導(dǎo)出,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定參考價(jià)值,需要的可以了解一下2022-06-06
C++封裝遠(yuǎn)程注入類CreateRemoteThreadEx實(shí)例
這篇文章主要介紹了C++封裝遠(yuǎn)程注入類CreateRemoteThreadEx實(shí)例,詳細(xì)講述了注入DLL到指定的地址空間以及從指定的地址空間卸載DLL的方法,需要的朋友可以參考下2014-10-10
C++常用函數(shù)總結(jié)(algorithm 頭文件)
本文給大家詳細(xì)介紹了algorithm 頭文件中最常用的函數(shù)及其使用方法,當(dāng)然這只是其中的一部分,algorithm 頭文件中還有很多其他的函數(shù),感興趣的朋友一起看看吧2023-12-12
OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡效果
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01

