C语言实现二叉树遍历的迭代算法

 更新时间:2014年09月17日 11:59:49   投稿:shichen2014  
这篇文章主要介绍了C语言实现二叉树遍历的迭代算法,包括二叉树的中序遍历、先序遍历及后序遍历等,是非常经典的算法,需要的朋友可以参考下

本文实例讲述了C语言实现二叉树遍历的迭代算法,是数据结构算法中非常经典的一类算法。分享给大家供大家参考。

具体实现方法如下:

二叉树中序遍历的迭代算法:

#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);
}

关于前序遍历,后来又写的算法如下,供大家参考:

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程序算法设计的学习有一定的借鉴价值。

相关文章

  • C语言深入探索之单链表与typedef的用法

    C语言深入探索之单链表与typedef的用法

    typedef为C语言的关键字,作用是为一种数据类型定义一个新名字,单链表是后面要学的双链表以及循环链表的基础,要想继续深入了解数据结构以及C语言,我们就要奠定好这块基石!接下来就和我一起学习吧
    2022-05-05
  • C语言实现井字棋游戏

    C语言实现井字棋游戏

    这篇文章主要为大家详细介绍了C语言实现井字棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • 详解C++中的常量

    详解C++中的常量

    这篇文章主要介绍了C++中的常量的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • Qt数据库应用之实现数据分组导出

    Qt数据库应用之实现数据分组导出

    这篇文章主要为大家详细介绍了如何利用Qt实现数据库数据分组导出,文中的示例代码讲解详细,对我们学习或工作有一定参考价值,需要的可以了解一下
    2022-06-06
  • C++封装远程注入类CreateRemoteThreadEx实例

    C++封装远程注入类CreateRemoteThreadEx实例

    这篇文章主要介绍了C++封装远程注入类CreateRemoteThreadEx实例,详细讲述了注入DLL到指定的地址空间以及从指定的地址空间卸载DLL的方法,需要的朋友可以参考下
    2014-10-10
  • C语言实现循环双链表

    C语言实现循环双链表

    这篇文章主要为大家详细介绍了C语言实现循环双链表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • C++实现俄罗斯方块游戏

    C++实现俄罗斯方块游戏

    这篇文章主要为大家详细介绍了C++实现俄罗斯方块游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09
  • C++常用函数总结(algorithm 头文件)

    C++常用函数总结(algorithm 头文件)

    本文给大家详细介绍了algorithm 头文件中最常用的函数及其使用方法,当然这只是其中的一部分,algorithm 头文件中还有很多其他的函数,感兴趣的朋友一起看看吧
    2023-12-12
  • C++中需要注意的细节你知道吗

    C++中需要注意的细节你知道吗

    这篇文章主要介绍了C++ 需要注意的几点细节,帮助大家更好的理解和学习C++,感兴趣的朋友可以了解下,希望能够给你带来帮助
    2021-09-09
  • OpenCV实现马赛克和毛玻璃滤镜效果

    OpenCV实现马赛克和毛玻璃滤镜效果

    这篇文章主要为大家详细介绍了OpenCV实现马赛克和毛玻璃滤镜效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01

最新评论