栈实现二叉树非递归先序遍历
来源:互联网 发布:逐浪cms oracle版 编辑:程序博客网 时间:2024/05/12 12:11
#include "stdio.h"
#include "stdlib.h"
typedef struct TreeNode *Tree;
typedef char ElementType;
typedef struct stack *Stack;
typedef Tree ElementTypeOfStack;
struct TreeNode
{
ElementType Data;
Tree Left;
Tree Right;
};
struct stack
{
ElementTypeOfStack *DataArray;
int Top;
int Size;
int Capacity;
};
Tree CreatTree(Tree T);
void PrintTreeWithStack(Tree T);
void PrintTree(Tree T);
Stack CreatStack(int Capacity);
int Push(ElementTypeOfStack X, Stack S);
ElementTypeOfStack Pop(Stack S);
int IsEmpty(Stack S);
int main(int argc, char const *argv[])
{
Tree T = NULL;
T = CreatTree(T);
printf("用栈先序输出:");
PrintTreeWithStack(T);
printf("递归先序输出:");
PrintTree(T);
return 0;
}
Tree CreatTree(Tree T)
{
char ch;
scanf("%c",&ch);
if(ch == '#')
{
return NULL;
}else
{
T = (Tree)malloc(sizeof(struct TreeNode));
T->Data = ch;
T->Left = CreatTree(T->Left);
T->Right = CreatTree(T->Right);
return T;
}
}
void PrintTreeWithStack(Tree T)
{
if (T == NULL)
{
printf("树为空\n");
}
Stack S = CreatStack(50);//创建容量为50的栈
while(T || !IsEmpty(S))
{
while(T)
{
Push(T,S);
printf("%c ", T->Data);
T = T->Left;
}
T = Pop(S);
T = T->Right;
}
printf("\n");
}
void PrintTree(Tree T)
{
if(T)
{
printf("%c ",T->Data);
PrintTree(T->Left);
PrintTree(T->Right);
}
}
Stack CreatStack(int Capacity)
{
Stack S = (Stack)malloc(sizeof(struct stack));
S->DataArray = (ElementTypeOfStack*)malloc(sizeof(ElementTypeOfStack)*Capacity);
S->Top = -1;
S->Size = 0;
S->Capacity = Capacity;
return S;
}
int Push(ElementTypeOfStack X, Stack S)
{
if (S->Size == S->Capacity)
{
return 0;
}
S->Top++;
S->DataArray[S->Top] = X;
S->Size++;
return 1;
}
ElementTypeOfStack Pop(Stack S)
{
if (S->Size == 0)
{
return NULL;
}
S->Size--;
return S->DataArray[S->Top--];
}
int IsEmpty(Stack S)
{
return S->Size == 0;
}
#include "stdlib.h"
typedef struct TreeNode *Tree;
typedef char ElementType;
typedef struct stack *Stack;
typedef Tree ElementTypeOfStack;
struct TreeNode
{
ElementType Data;
Tree Left;
Tree Right;
};
struct stack
{
ElementTypeOfStack *DataArray;
int Top;
int Size;
int Capacity;
};
Tree CreatTree(Tree T);
void PrintTreeWithStack(Tree T);
void PrintTree(Tree T);
Stack CreatStack(int Capacity);
int Push(ElementTypeOfStack X, Stack S);
ElementTypeOfStack Pop(Stack S);
int IsEmpty(Stack S);
int main(int argc, char const *argv[])
{
Tree T = NULL;
T = CreatTree(T);
printf("用栈先序输出:");
PrintTreeWithStack(T);
printf("递归先序输出:");
PrintTree(T);
return 0;
}
Tree CreatTree(Tree T)
{
char ch;
scanf("%c",&ch);
if(ch == '#')
{
return NULL;
}else
{
T = (Tree)malloc(sizeof(struct TreeNode));
T->Data = ch;
T->Left = CreatTree(T->Left);
T->Right = CreatTree(T->Right);
return T;
}
}
void PrintTreeWithStack(Tree T)
{
if (T == NULL)
{
printf("树为空\n");
}
Stack S = CreatStack(50);//创建容量为50的栈
while(T || !IsEmpty(S))
{
while(T)
{
Push(T,S);
printf("%c ", T->Data);
T = T->Left;
}
T = Pop(S);
T = T->Right;
}
printf("\n");
}
void PrintTree(Tree T)
{
if(T)
{
printf("%c ",T->Data);
PrintTree(T->Left);
PrintTree(T->Right);
}
}
Stack CreatStack(int Capacity)
{
Stack S = (Stack)malloc(sizeof(struct stack));
S->DataArray = (ElementTypeOfStack*)malloc(sizeof(ElementTypeOfStack)*Capacity);
S->Top = -1;
S->Size = 0;
S->Capacity = Capacity;
return S;
}
int Push(ElementTypeOfStack X, Stack S)
{
if (S->Size == S->Capacity)
{
return 0;
}
S->Top++;
S->DataArray[S->Top] = X;
S->Size++;
return 1;
}
ElementTypeOfStack Pop(Stack S)
{
if (S->Size == 0)
{
return NULL;
}
S->Size--;
return S->DataArray[S->Top--];
}
int IsEmpty(Stack S)
{
return S->Size == 0;
}
阅读全文
1 0
- 栈实现二叉树非递归先序遍历
- 如何实现二叉树的非递归先序遍历
- 【二叉树遍历】先序---非递归实现
- 二叉树的先序遍历非递归实现方法
- 非递归先序遍历二叉树
- 非递归先序遍历二叉树
- 先序遍历二叉树 [非递归]
- 非递归先序遍历二叉树
- 二叉树非递归先序遍历
- 二叉树非递归先序遍历
- 非递归先序遍历二叉树
- 带栈的二叉树非递归先序遍历
- 使用栈,非递归先序遍历二叉树T
- [二叉树专题]:先序遍历二叉树的递归实现与非递归实现
- 二叉树的先序递归以及非递归遍历
- 二叉树的先序递归,非递归遍历
- C++ 二叉树的遍历---先序遍历(非递归)
- 二叉树(一) 先序遍历、中序遍历、后续遍历、层次遍历的递归与非递归实现
- npm install 不成功,安装cnpm教程
- 加一乘2平方
- 安卓开发创建ImageLoaderConfiguration实例及获取(全局配置)
- 继承知识汇总
- 线段树、树状数组题目专题
- 栈实现二叉树非递归先序遍历
- 由排序算法浅析C++函数模板
- 将带引用的公式复制到另一个工作表时如何去掉原文件的引用
- ImageLoader框架初始化参数并展示到Listview上
- jquery 动态加载css,js文件
- 如何创建 几何画笔
- MYSQL获取当前表自增值
- Spring AOP是什么意思,通俗讲解
- 【期刊】基于 Python 的新浪微博数据爬虫