二叉树的生成和遍历
2016-03-22 21:37
309 查看
#include<stdio.h> #include<malloc.h> #include<stdlib.h> #include<string.h> #define MAX_SIZE 10 typedef 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; }
相关文章推荐
- Android中AsyncTask的使用
- poj_1163
- Global Variable Default Initialized 0
- Opengles绘图
- bzoj 3531 [SDOI2014]旅行
- 对三层架构的浅层理解
- 第十五讲-方法参数
- JQuery的Ajax跨域请求的
- 20160322servlet学习笔记Referer的防盗链应用
- RAID各级别特性详解
- 缅怀
- 酷欧天气实战
- 本周日记
- Better History(历史记录)
- 华硕笔记本使用老毛桃工具迅速重装系统(动作快估计就二十分钟,高手勿喷)
- 十大监视SQL Server性能的计数器
- 网易面试题 (第24题) 对字符串的一种编码
- CentOS 6.4下Squid代理服务器的安装与配置
- 1031. Hello World for U (20)
- 扩展欧几里得算法