c语言实现 非递归先序遍历二叉链树

 1 #include<stdio.h>
 2 #include<conio.h>
 3 #include<malloc.h>
 4 typedef char datatype;
 5 typedef struct node{
 6     datatype data;
 7     struct node *lchild,*rchild;
 8 }bitree;                     //二叉链树数据结构定义
 9 void Preorder(bitree *p)     //先序遍历算法
10 {
11     bitree *Stack[100];      //用一个“栈数组”存放二叉树节点bitree指针,假定最多能盛放100个bitree节点
12     bitree *s=p;             //s节点为当前节点
13     int top=-1;
14     while(top!=-1||s!=NULL)    {       while(s!=NULL)      {
16             if(top==99)         {    //栈满,上溢出提示
17                 printf("overflow");
18                 return;
19             }
20             else         {
21                 top++;      //当前节点入栈,并且访问(打印内容)
22                 Stack[top]=s;
23                 printf("%c",s->data);
24                 s=s->lchild; //先序要求先访问左孩子,为空时跳出循环
25             }
26         }
27         s=Stack[top]; //s回到当前空左孩子的双亲节点
28         top--;        //退栈
29         s=s->rchild;  //遍历右孩子
30     }
31 }
32 bitree *CreatTree(){     //建立二叉链树的函数,从左到右,从上到下依次赋值,空值给‘@’,退出在结尾给‘@’
33     char ch;
34     bitree *Q[100];      //建立一个“队列数组”存放bitree指针节点
35     int front,rear;
36     bitree *root,*s;     //root是根节点,s还是代表当前建立的节点
37     root=NULL;           //头结点置空
38     front=1,rear=0;
39     while((ch=getchar())!=‘#‘) //定义输入‘#’符号表示停止输入节点元素,建立结束
40     {
41         s=NULL;
42         if(ch!=‘@‘){      //定义输入‘@’符号表示输入的树节点为空
43             s=(bitree*)malloc(sizeof(bitree));
44             s->data=ch;
45             s->lchild=NULL;
46             s->rchild=NULL;
47         }
48         rear++;       //当前节点建立完成后入队
49         Q[rear]=s;  
50         if(rear==1)
51             root=s;
52         else{
53             if(s&&Q[front]){
54                 if(rear%2==0) //左孩子节点编号为偶数
55                     Q[front]->lchild=s;
56                 else      //右孩子节点编号为奇数
57                     Q[front]->rchild=s;
58             }
59             if(rear%2==1)    //双亲节点右孩子建立结束后,双亲节点出队,建立下一位紧邻的双亲节点的孩子
60                 front++;
61         }
62     }
63     return root;             //函数返回建立完成的二叉链树的根节点指针
64 }
65 int main()
66 {
67     bitree *root;
68     root=CreatTree();
69     Preorder(root);
70     return 0;
71 }
时间: 2024-08-01 18:21:20

c语言实现 非递归先序遍历二叉链树的相关文章

c语言实现按层次(广度优先)非递归遍历二叉链树

1 #include<stdio.h> 2 #include<conio.h> 4 #include<malloc.h> 5 typedef char datatype; //字符类型 内容 6 typedef struct node{ //二叉链树数据结构 7 datatype data; 8 struct node *lchild,*rchild; 9 }bitree; 10 bitree *CreatTree(){ //上一篇的建立二叉链树的函数和这里的一模一样,

非递归后序遍历二叉树(1)

1 void postOrder3(BinTree *root) //非递归后序遍历 2 { 3 stack<BinTree*> s; 4 BinTree *cur; //当前结点 5 BinTree *pre=NULL; //前一次访问的结点 6 s.push(root); 7 while(!s.empty()) 8 { 9 cur=s.top(); 10 if((cur->lchild==NULL&&cur->rchild==NULL)|| 11 (pre!=N

完全二叉树的链式存储结构的转化 &amp; 非递归中序遍历二叉树

1 /* 2 * 二叉树 3 * 4 * (将完全二叉树的数组形式改为链表形式) 5 * 6 * 1 7 * 2 3 8 * 4 5 6 7 9 * 8 10 * 11 */ 12 #include <iostream> 13 #define MAX 10 14 using namespace std; 15 16 typedef struct btnode{ 17 int data; 18 struct btnode * lchild; 19 struct btnode * rchild;

【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】

[145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive soluti

非递归 后序遍历二叉树

方法有很多,这里只举一种,先定义栈结点的数据结构typedef struct{Node * p; int rvisited;}SNode //Node 是二叉树的结点结构,rvisited==1代表p所指向的结点的右结点已被访问过. lastOrderTraverse(BiTree bt){ //首先,从根节点开始,往左下方走,一直走到头,将路径上的每一个结点入栈. p = bt; while(bt){ push(bt, 0); //push到栈中两个信息,一是结点指针,一是其右结点是否被访问过

94.Binary Tree Inorder Traversal(非递归中序遍历)

Given a binary tree, return the inorder traversal of itsnodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution istrivial, could you do it iteratively? confused what "{1,#,2,3}" means? > rea

LeetCode:二叉树的非递归中序遍历

第一次动手写二叉树的,有点小激动,64行的if花了点时间,上传leetcode一次点亮~~~ 1 /* inorder traversal binary tree */ 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 6 struct TreeNode { 7 int val; 8 struct TreeNode *left; 9 struct TreeNode *right; 10 }; 11 12 int* inorderTrave

Python实现二叉树的非递归中序遍历

思路: 1. 使用一个栈保存结点(列表实现): 2. 如果结点存在,入栈,然后将当前指针指向左子树,直到为空: 3. 当前结点不存在,则出栈栈顶元素,并把当前指针指向栈顶元素的右子树: 4. 栈不为空,循环2.3部. 代码如下,解决了leetcode94. Binary Tree Inorder Traversal: # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): #

Python实现二叉树的非递归先序遍历

思路: 1. 使用列表保存结果: 2. 使用栈(列表实现)存储结点: 3. 当根结点存在,保存结果,根结点入栈: 4. 将根结点指向左子树: 5. 根结点不存在,栈顶元素出栈,并将根结点指向栈顶元素的右子树: 6. 重复步骤3-6,直到栈空. LeetCode: 144. Binary Tree Preorder Traversal # Definition for a binary tree node. # class TreeNode(object): # def __init__(self