树的二叉链表
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#include <malloc.h>
#include<conio.h>
//那就等现实给你一巴掌,醒了。就不沉沦了…
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define MAXSIZE 100
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INEFASIBLE -1
#define OVERFLOW -2
typedef int Status;
#define CHAR 1//只可是字符型
#if CHAR
typedef char TElemType;
TElemType Nil=' ';//设字符型以空格符为空
#define form "%c"
#else
typedef int TElemType;
TElemType Nil=0;
#define form "%d"
#endif
typedef struct CSNode{
TElemType data; //顶点信息
struct CSNode *firstchild,*nextsibling;//第一个孩子,第一个的孩子的兄弟
}CSNode,*CSTree;
typedef CSTree QElemType;//
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,* QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
void visit(TElemType e){
printf(form"",e);
}
Status InitQueue(LinkQueue &Q){
//队列初始化
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));//开辟Q头尾
if(!Q.front) exit(OVERFLOW);//未开辟Q.front,exit
Q.front->next=NULL;
return OK;
}
Status EnQueue(LinkQueue &Q,QElemType e){
//入队
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));//分配空间
if(!p) exit(OVERFLOW);//未成功,exit
(p->data)=e;
p->next=NULL;
Q.rear->next=p;//
Q.rear=p;//fornt,数据(rear)
return OK;
}
Status DeQueue(LinkQueue &Q,QElemType &e){
//出队
QueuePtr p;
if(Q.front==Q.rear) return ERROR;//队空
p=Q.front->next;
e=(p->data);
Q.front->next=p->next;
if(Q.rear==p) Q.front=Q.rear;
free(p);
return OK;
}
Status QueueEmpty(LinkQueue Q){
//
if(Q.front->next==NULL)//如果Q.frint指向的下一个地址为空
return TRUE;
else
return FALSE;
}
int QueueLength(LinkQueue Q){
//
int i;//计数器
QueuePtr p=Q.front;
while(Q.rear!=p){
i++;
p=p->next;
}
return i;
}
//#define 标识符 常量
#define ClearTree DestroyTree
void InitTree(CSTree &T){
//初始化树
T=NULL;
}
void DestoryTree(CSTree &T){
//
if(T){
DestoryTree(T->firstchild);
DestoryTree(T->nextsibling);
free(T);
T=NULL;
}
}
//#include<LinkQueue.h>
///#include<LinkQueue.cpp>
void CreateTree(CSTree &T){//创建有问题 //创建树的二叉链表
char c[20];
CSTree p,p1;
LinkQueue q;
int i,m;
InitQueue(q);
printf("请输入根结点(字符型,空格为空):");
scanf("%c %*c",&c[0]);
//%*c是什么意思?
if(c[0]!=Nil){//非空树
T=(CSTree)malloc(sizeof(CSNode));
T->data=c[0];
T->nextsibling=NULL;
EnQueue(q,T);
while(!QueueEmpty(q)){
DeQueue(q,p);
printf("请按长幼顺序输入结点%c的所有孩子:",p->data);
gets(c);//将结点的所有孩子作为字符串输入
//当格式符为"c"时,对应输出的变量值为字符
// 函数名: gets
//功 能: 从流中取一字符串
//用 法: char *gets(char *string);
m=strlen(c);
if(m>0){
p1->firstchild=(CSTree)malloc(sizeof(CSNode));
p1->data=c[0];
EnQueue(q,p1);
for(i=1;i<m;i++){
p->nextsibling=(CSTree)malloc(sizeof(CSNode));
p1=p->firstchild;
p1->data=c[i];
EnQueue(q,p1);
}
p1->nextsibling=NULL;
}
else
p->firstchild=NULL;
}
}
else
T=NULL;
}
Status TreeEmpty(CSTree T){
//判断树
if(T)//非空树
return FALSE;
else
return TRUE;
}
int TreeDepth(CSTree T){
CSTree p;
int depth,max;
if(!T)
return 0;
for(p=T->firstchild;p;p->nextsibling){
//
depth=TreeDepth(p);
if(depth>max)
max=depth;
}
return max+1;
}
TElemType Value(CSTree p){
return p->data;
}
TElemType Root(CSTree T){
if(T)
return Value(T);
else
return Nil;
}
CSTree Point(CSTree T,TElemType s){
//初始条件:树T,结点信息s
//操作结果:返回结点信息是s的树结点
LinkQueue q;//队
QElemType a;//队结点信息
if(T){
InitQueue(q);
EnQueue(q,T);
while(!QueueEmpty(q)){
EnQueue(q,a);
if(a->data==s)
return a;
if(a->firstchild)
EnQueue(q,a->firstchild);
if(a->nextsibling)
EnQueue(q,a->nextsibling);
}
}
return NULL;
}
Status Assign(CSTree &T,TElemType cur_e,TElemType value){
//
CSTree p;
if(T){
p=Point(p,cur_e);
if(p){
p->data=value;
return OK;
}
}
return ERROR;
}