时间:2021-05-20
本文实例讲述了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程序算法设计的学习有一定的借鉴价值。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C语言实现二叉树的搜索及相关算法。分享给大家供大家参考,具体如下:二叉树(二叉查找树)是这样一类的树,父节点的左边孩子的key都小于它,右边孩子的
本文实例讲述了C语言实现线索二叉树的定义与遍历。分享给大家供大家参考,具体如下:#include#includetypedefcharTElemType;//二
本文实例讲述了php实现的二叉树遍历算法。分享给大家供大家参考,具体如下:今天使用php来实现二叉树的遍历创建的二叉树如下图所示php代码如下所示:value.
问题如何遍历一个二叉树遍历二叉树就是访问二叉树的每一个节点二叉树父结点下先左访问,先序遍历(根左右)例如:遍历以下的二叉树遍历结果:ABDECFPython代码
本文实例讲述了PHP基于非递归算法实现先序、中序及后序遍历二叉树操作。分享给大家供大家参考,具体如下:概述:二叉树遍历原理如下:针对上图所示二叉树遍历:1.前序