算法6.3
来源:互联网 发布:网络账号被盗报警 编辑:程序博客网 时间:2024/05/22 06:26
#include<stdio.h>
#include<string.h>
#include<malloc.h>//in order to include the malloc function
#include <stdlib.h>//in order to include the exit function
#define OVERFLOW 1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
//-----------------Binary Tree Node-----------------------
typedef struct BiTNode{
char name[20];
struct BiTNode *lchild, *rchild;
}BiTNode,*BiTree;
//-----------------Binary Tree Node------------------------------------------
//-----------------SqStack Node----------------------------------------------
typedef struct{
BiTree *base;
BiTree *top;
int stacksize;
}SqStack;
//because the element in the stack is pointer,so we use the pointer of pointer
//-----------------SqStack Node----------------------------------------------
//------------------quote the functions--------------------------------------
SqStack InitStack(void);
BiTree GetTop(SqStack *);
int Push(SqStack *,BiTree );
int StackEmpty(SqStack *);
BiTree Pop(SqStack *);
int InOrder(BiTree,SqStack *);
//------------------quote the functions--------------------------------------
//-------------------------MAIN FUNCTION-------------------------------------
int main(void){
// BiTNode a1,b1,c1,d1,e1,f1,g1;
BiTree head,a,b,c,d,e,f,g;//a is the root
// a=&a1;b=&b1;c=&c1;d=&d1;e=&e1;f=&f1;g=&g1;
// head->lchild=NULL;head->rchild=a;//the head pointer point to the root
head=(BiTree)malloc(sizeof(BiTNode));
a=(BiTree)malloc(sizeof(BiTNode));
b=(BiTree)malloc(sizeof(BiTNode));
c=(BiTree)malloc(sizeof(BiTNode));
d=(BiTree)malloc(sizeof(BiTNode));
e=(BiTree)malloc(sizeof(BiTNode));
f=(BiTree)malloc(sizeof(BiTNode));
g=(BiTree)malloc(sizeof(BiTNode));
strcpy(a->name,"Aname");strcpy(b->name,"Bname");strcpy(c->name,"Cname");
strcpy(d->name,"Dname");strcpy(e->name,"Ename");strcpy(f->name,"Fname");
strcpy(g->name,"Gname");
a->lchild=b;a->rchild=NULL;
b->lchild=c;b->rchild=d;
c->lchild=NULL;c->rchild=NULL;
d->lchild=e;d->rchild=f;
e->lchild=NULL;e->rchild=g;
f->lchild=NULL;f->rchild=NULL;
g->lchild=NULL;g->rchild=NULL;
InOrderTraverse(a);
return 0;
}
//-------------------------MAIN FUNCTION-------------------------------------
//---------------------------InOrderTraverse----------------------------------
int InOrderTraverse(BiTree T){
SqStack S,*S1;BiTree p;
S= InitStack();S1=&S;
p=T;
while(p || !StackEmpty(S1)){
if(p) {Push(S1,p);p=p->lchild;}
else {
p=Pop(S1);
printf("%s\n",p->name);
p=p->rchild;
}
}
return 1;
}
//---------------------------InOrderTraverse----------------------------------
//--------------------------some execuation about the stack--------------------
SqStack InitStack(void){
SqStack s;
s.base=(BiTree *)malloc(STACK_INIT_SIZE*sizeof(BiTree));
if(!s.base) exit(OVERFLOW);
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
return s;
}
BiTree GetTop(SqStack *s){
BiTree e;
// if (s.top==s.base) return 1;
e=*(s->top-1);
return e;
}
int Push(SqStack *s,BiTree e){
if(s->top-s->base>=s->stacksize){
s->base=(BiTree *)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(BiTree));
if(!s->base) exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++ =e;
return 0;
}
int StackEmpty(SqStack *s){
if(s->top==s->base) return 1;
else return 0;
}
BiTree Pop(SqStack *s){
BiTree e;;
// if(s.top=s.base) return 1;
e=*--(s->top);
return e;
}
//--------------------------some execuation about the stack--------------------
#include<string.h>
#include<malloc.h>//in order to include the malloc function
#include <stdlib.h>//in order to include the exit function
#define OVERFLOW 1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
//-----------------Binary Tree Node-----------------------
typedef struct BiTNode{
char name[20];
struct BiTNode *lchild, *rchild;
}BiTNode,*BiTree;
//-----------------Binary Tree Node------------------------------------------
//-----------------SqStack Node----------------------------------------------
typedef struct{
BiTree *base;
BiTree *top;
int stacksize;
}SqStack;
//because the element in the stack is pointer,so we use the pointer of pointer
//-----------------SqStack Node----------------------------------------------
//------------------quote the functions--------------------------------------
SqStack InitStack(void);
BiTree GetTop(SqStack *);
int Push(SqStack *,BiTree );
int StackEmpty(SqStack *);
BiTree Pop(SqStack *);
int InOrder(BiTree,SqStack *);
//------------------quote the functions--------------------------------------
//-------------------------MAIN FUNCTION-------------------------------------
int main(void){
// BiTNode a1,b1,c1,d1,e1,f1,g1;
BiTree head,a,b,c,d,e,f,g;//a is the root
// a=&a1;b=&b1;c=&c1;d=&d1;e=&e1;f=&f1;g=&g1;
// head->lchild=NULL;head->rchild=a;//the head pointer point to the root
head=(BiTree)malloc(sizeof(BiTNode));
a=(BiTree)malloc(sizeof(BiTNode));
b=(BiTree)malloc(sizeof(BiTNode));
c=(BiTree)malloc(sizeof(BiTNode));
d=(BiTree)malloc(sizeof(BiTNode));
e=(BiTree)malloc(sizeof(BiTNode));
f=(BiTree)malloc(sizeof(BiTNode));
g=(BiTree)malloc(sizeof(BiTNode));
strcpy(a->name,"Aname");strcpy(b->name,"Bname");strcpy(c->name,"Cname");
strcpy(d->name,"Dname");strcpy(e->name,"Ename");strcpy(f->name,"Fname");
strcpy(g->name,"Gname");
a->lchild=b;a->rchild=NULL;
b->lchild=c;b->rchild=d;
c->lchild=NULL;c->rchild=NULL;
d->lchild=e;d->rchild=f;
e->lchild=NULL;e->rchild=g;
f->lchild=NULL;f->rchild=NULL;
g->lchild=NULL;g->rchild=NULL;
InOrderTraverse(a);
return 0;
}
//-------------------------MAIN FUNCTION-------------------------------------
//---------------------------InOrderTraverse----------------------------------
int InOrderTraverse(BiTree T){
SqStack S,*S1;BiTree p;
S= InitStack();S1=&S;
p=T;
while(p || !StackEmpty(S1)){
if(p) {Push(S1,p);p=p->lchild;}
else {
p=Pop(S1);
printf("%s\n",p->name);
p=p->rchild;
}
}
return 1;
}
//---------------------------InOrderTraverse----------------------------------
//--------------------------some execuation about the stack--------------------
SqStack InitStack(void){
SqStack s;
s.base=(BiTree *)malloc(STACK_INIT_SIZE*sizeof(BiTree));
if(!s.base) exit(OVERFLOW);
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
return s;
}
BiTree GetTop(SqStack *s){
BiTree e;
// if (s.top==s.base) return 1;
e=*(s->top-1);
return e;
}
int Push(SqStack *s,BiTree e){
if(s->top-s->base>=s->stacksize){
s->base=(BiTree *)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(BiTree));
if(!s->base) exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++ =e;
return 0;
}
int StackEmpty(SqStack *s){
if(s->top==s->base) return 1;
else return 0;
}
BiTree Pop(SqStack *s){
BiTree e;;
// if(s.top=s.base) return 1;
e=*--(s->top);
return e;
}
//--------------------------some execuation about the stack--------------------
0 0
- 算法6.3
- 6.3.3 最短路径算法--Dijkstra算法,Bellmanford算法,Floyd算法,Johnson算法
- 6.3.1 强连通分支算法--Kosaraju算法、Tarjan算法和Gabow算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- Jboss自动关闭详解
- STL源码阅读-hashtable
- rqnoj PID26 / 合唱队形 ☆
- 最小生成树之kruscal算法
- C#中。简述 private、 protected、 public、 internal 修饰符的访问权限
- 算法6.3
- 黑马程序员_ 注解
- 职场分享:成功则的职场态度
- docker入门指引之在ceontos6.5下搭建docker的测试环境
- [ACM] POJ 1061青蛙的约会(扩展欧几里得求模线性方程)
- STL中map、set的数据结构及底层实现
- rsync
- Linux下的信号灯semaphore的应用
- 解决UINavigationController+UITabbar跳转页面隐藏tabbar的一种方法