二叉树的生成和遍历
来源:互联网 发布:地下城组队网络冲突 编辑:程序博客网 时间:2024/06/05 14:20
#include<stdio.h>#include<malloc.h> #include<stdlib.h>#include<string.h>#define MAX_SIZE 10typedef int ElemType;typedef enum{false,true}bool;struct node{ElemType data;struct node* left;struct node* right;};typedef struct node* bintree;struct Node{bintree data[MAX_SIZE];int top;};typedef struct Node* seqstack;struct quene{bintree data[MAX_SIZE];int front;int rear;};typedef struct quene* SeqQueue;//栈基本操作seqstack Stackinit(seqstack stack);bool Stackempty(seqstack stack);bool Stackfull(seqstack stack);void Push(seqstack stack,bintree e);bintree Pop(seqstack stack);bintree Top(seqstack stack);//队列基本操作SeqQueue QueueInit(SeqQueue queue);void EnQueue(SeqQueue queue,bintree e);bintree DeQueue(SeqQueue queue);bool QueueEmpty(SeqQueue queue);//二叉树的生成void BintreeInit(bintree* T);//前序递归遍历void PreTraverse(bintree T);//前序堆栈遍历void StackPre(bintree T);//中序递归遍历void InTraverse(bintree T); //中序堆栈遍历void StackInOrder(bintree T);//后序递归遍历void PostTraverse(bintree T);//后序堆栈遍历void StackPostOrder(bintree T);//广度优先队列遍历void QueueLevelOrder(bintree T);seqstack Stackinit(seqstack stack){stack = (seqstack)malloc(sizeof(struct Node));if(!stack){printf("Error in malloc");exit(0);}stack->top = -1;return stack;}bool Stackempty(seqstack stack){if(stack->top == -1){return true;}else return false;}bool Stackfull(seqstack stack){if(stack->top == MAX_SIZE-1){return true;}else return false;}void Push(seqstack stack,bintree e){if(Stackfull(stack) == true){printf("Stack is full");exit(0);} stack->data[++stack->top] = e;return;}bintree Pop(seqstack stack){if(Stackempty(stack) == true){printf("Stack is empty");exit(0);}bintree temp = stack->data[stack->top--];return temp;}bintree Top(seqstack stack){if(Stackempty(stack) == true){printf("Stack is empty");exit(0);}return stack->data[stack->top];}SeqQueue QueueInit(SeqQueue queue){queue = (SeqQueue)malloc(sizeof(struct quene));queue->front = -1;queue->rear = -1;return queue;}void EnQueue(SeqQueue queue,bintree e){if(queue->rear == (MAX_SIZE-1)){printf("queue is full");exit(0);}queue->data[++queue->rear] = e;}bintree DeQueue(SeqQueue queue){if(queue->rear == queue->front){printf("queue is empty");exit(0);}return queue->data[++queue->front];}bool QueueEmpty(SeqQueue queue){if(queue->rear == queue->front){return true;}else return false;}void QueueLevelOrder(bintree T){SeqQueue queue = QueueInit(queue);EnQueue(queue,T);while(!QueueEmpty(queue)){T = DeQueue(queue);printf("%d",T->data);if(T->left){EnQueue(queue,T->left);}if(T->right){EnQueue(queue,T->right);}}}void BintreeInit(bintree* T){ElemType data;scanf("%d",&data);if(data == -1){*T = NULL;}else{*T = (bintree)malloc(sizeof(struct node));if(!(*T)){printf("Error in malloc");exit(0);}(*T)->data = data;printf("输入%d的左子节点:",data);BintreeInit(&(((*T)->left)));printf("输入%d的右子节点:",data);BintreeInit(&(((*T)->right)));}}void PreTraverse(bintree T){if(T){printf("%d",T->data);PreTraverse(T->left);PreTraverse(T->right);}}void InTraverse(bintree T){if(T){InTraverse(T->left);printf("%d",T->data);InTraverse(T->right);}}void PostTraverse(bintree T){if(T){PostTraverse(T->left);PostTraverse(T->right);printf("%d",T->data);}}void StackPreOrder(bintree T){seqstack stack = Stackinit(stack);while(T || !Stackempty(stack)){while(T){printf("%d",T->data);Push(stack,T);T = T->left;}if(!Stackempty(stack)){T = Pop(stack);T = T->right;}}}void StackInOrder(bintree T){seqstack stack = Stackinit(stack);while(T || !Stackempty(stack)){if(T){Push(stack,T);T = T->left;}else{T = Pop(stack);printf("%d",T->data);T = T->right;}}}void StackPostOrder(bintree T){seqstack stack = Stackinit(stack);bintree Q = NULL;while(T || !Stackempty(stack)){while(T){Push(stack,T);T = T->left;}if(!Stackempty(stack)){T = Top(stack);if(T->right== NULL || T->right== Q){T = Pop(stack);printf("%d",T->data);Q = T;T = NULL;}else T = T->right;}}}int main(){printf("请输入第一个节点的值,-1表示没有叶节点:\n");bintree T;BintreeInit(&T);/*PreTraverse(T);InTraverse(T);PostTraverse(T);StackPreOrder(T);StackInOrder(T);StackPostOrder(T);*/QueueLevelOrder(T);return 0;}
0 0
- 二叉树的生成和遍历
- 线索二叉树的生成和遍历
- 二叉树的生成和遍历
- 二叉树的生成和遍历
- 二叉树的生成和递归遍历
- 二叉树的遍历和生成——Pascal实现
- 已知二叉树先序和中序遍历,生成二叉树
- 由二叉树前序和中序遍历生成二叉树
- 二叉树的生成与遍历
- 二叉树的生成与遍历
- 线索二叉树的生成及遍历
- 线索二叉树的生成及遍历
- 二叉树的生成与遍历
- 由二叉树先序遍历/后序遍历和中序遍历生成二叉树
- 二叉树的遍历(层遍历和深度遍历)
- 在二叉树的生成和遍历中讨论C++的参数传递
- java写的二叉搜索树(生成和前序遍历)
- 由二叉树的先序和中序遍历生成二叉树的层序和后序遍历结构
- Jsp
- java:double四舍五入并保留两位小数的方法
- 商业即生活,一些思考和感悟
- Problem-1016
- Android原理探究:Activity任务栈Task的简单探讨
- 二叉树的生成和遍历
- Android RecyclerView 使用完全解析 体验艺术般的控件
- Python学习笔记(一)
- Ubuntu14.04下“E: 无法找到软件包”的解决
- ubuntu14.04LTS丢失登录界面
- 一人一狗,那一年
- JSONObject 解析json的步骤
- 机器学习中的特征工程总结一
- DAO层,Service层,Controller层、View层