树的二叉链表

L_thread 2009-02-11 06:40:19
#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;
}

...全文
116 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
L_thread 2009-02-11
  • 打赏
  • 举报
回复

TElemType Parent(CSTree T,TElemType cur_e){
//初始条件:树T存在,cur_e是T中某个结点
//操作结果:若cur_e是T的非根结点,则返回的他的双亲;否则函数值为NULL
CSTree p,t;
LinkQueue q;
InitQueue(q);
if(T){
if(Value(T)==cur_e)
return Nil;
EnQueue(q,T);
while(!QueueEmpty(q)){
DeQueue(q,p);
if(p->firstchild){
if(p->firstchild->data==cur_e)
return Value(p);
t=p;
p=p->firstchild;
EnQueue(q,p);
while(p->nextsibling){
p=p->nextsibling;
if(Value(p)==cur_e)
return Value(t);
EnQueue(q,p);
}
}
}
}
return Nil;
}



TElemType LeftChild(CSTree T,TElemType cur_e){
//
//
CSTree f;
f=Point(T,cur_e);
if(f&&f->firstchild)
return f->firstchild->data;
else
return Nil;
}


TElemType RightSibling(CSTree T,TElemType cur_e){
//
//
CSTree f;
f=Point(T,cur_e);
if(f&&f->nextsibling)
return f->nextsibling->data;
else
return Nil;
}



Status InsertChild(CSTree &T,CSTree p,int i,CSTree c){
//
//
//
int j;
CSTree q;
if(T){
if(i==1){
c->nextsibling=p->firstchild;
p->firstchild=c;
}
else{
q=p->firstchild;
j=2;
while(q&&j<i){
q=q->nextsibling;
j++;
}
if(j==1){
c->nextsibling=q->nextsibling;
q->nextsibling=c;
}
else
return ERROR;
}
return OK;
}
else
return ERROR;
}

Status DeleteChild(CSTree &T,CSTree p,int i){
//
//
//
CSTree b,q;
int j;
if(T){
if(i==1){
b=p->firstchild;
p->firstchild=b->nextsibling;
b->nextsibling=NULL;
DestoryTree(b);
}
else {
q=p->firstchild;
j=2;
while(q&&j<i){
q=q->nextsibling;
j++;
}
if(j==i){
b=q->nextsibling;
q->nextsibling=b->nextsibling;
b->nextsibling=NULL;
DestoryTree(b);
}
else
return ERROR;
}
return OK;
}
return ERROR;
}


void PostOrderTraverse(CSTree T,void (*Visit)(TElemType)){
//
CSTree p;
if(T){
if(T->firstchild){
PostOrderTraverse(T->firstchild,Visit);
p=T->firstchild->nextsibling;
while(p){
PostOrderTraverse(p,Visit);
p=p->nextsibling;
}

}
Visit(Value(T));
}
}



void LevelOrderTraverse(CSTree T,void(*Visit)(TElemType)){
//
CSTree p;
LinkQueue q;
InitQueue(q);
if(T){
Visit(Value(T));
EnQueue(q,T);
while(!QueueEmpty(q)){
DeQueue(q,p);
if(p->firstchild){
p=p->firstchild;
Visit(Value(p));
EnQueue(q,p);
while(p->nextsibling){
p=p->nextsibling;
Visit(Value(p));
EnQueue(q,p);
}
}
}
}
printf("\n");
}


void PreOrderTraverse(CSTree T,void (*Visit)(TElemType)){
if(T){
Visit(T->data);
PreOrderTraverse(T->firstchild,Visit);
PreOrderTraverse(T->nextsibling,Visit);
}

}



void main(){
//
int i;
CSTree T,p,q;
TElemType e,e1;
InitTree(T);
printf("构造空树后,树空否?%d(1:是0:否)。树的根为%c,树的深度为%d。\n",TreeEmpty(T),Root(T),TreeDepth(T));
CreateTree(T);
printf("构造树T后,树空否?%d(1:是0:否)。树的根为%c,树的深度为%d。\n",TreeEmpty(T),Root(T),TreeDepth(T));
printf("层序遍历树T:\n");
LevelOrderTraverse(T,visit);
printf("请输入待修改的结点的值新值:");
scanf("%c%*c%c%*c",&e,&e1);
Assign(T,e,e1);
printf("层序遍历修改后的树T:\n");
LevelOrderTraverse(T,visit);
printf("%c的双亲是%c,长子是%c,下一个兄弟是%c。\n",e,Parent(T,e1),LeftChild(T,e1),RightSibling(T,e1));
printf("建立树p:\n");
CreateTree(p);
printf("层序遍历树p:");
LevelOrderTraverse(p,visit);
printf("将树p插到树T中,请输入T中p的双亲结点子树序号:");
scanf("%c%d%*c",&e,&i);
q=Point(T,e);
InsertChild(T,q,i,p);
printf("层序遍历修改后的树T:\n");
LevelOrderTraverse(T,visit);
printf("\n先根遍历树T:\n");
PreOrderTraverse(T,visit);
printf("\n后根遍历树T:\n");
PostOrderTraverse(T,visit);
printf("\n删除树T中结点e的第i颗子树,请输入e i:");
scanf("%c%d",&e,&i);
q=Point(T,e);
DeleteChild(T,q,i);
printf("层序遍历修改后的树T:\n");
LevelOrderTraverse(T,visit);
DestoryTree(T);
}


高手大侠们帮忙看一下void CreateTree(CSTree &T)已经不报错了,可能是那逻辑有问题。。。。。。。
代码转载自:https://pan.quark.cn/s/50df8825dc0d Tensilica Xtensa指令集架构手册是一份详尽阐述Xtensa指令集架构(ISA)的参考指南,它主要面向Tensilica公司所推出的处理器核心,尤其是LX106核,同时亦涵盖了ESP8266和ESP32等相关内容。该手册全面介绍了Xtensa处理器核心的指令集、配置选项、扩展能力以及将架构映射至硬件的详细情况。接下来将依据手册中所述的知识点进行深入解读。 ### Xtensa指令集架构(ISA) Xtensa指令集架构是Tensilica公司为其处理器产品设计的一套指令集,该指令集具备高度的可配置性和可扩展性。这一特性使得Xtensa ISA能够在不同的应用领域和硬件需求中进行个性化定制,以满足特定的功能与性能要求。 #### 可配置性(Configurability) Xtensa ISA的可配置性体现在用户可以根据实际需求对处理器的多个方面进行定制。这包括但不限于: - **指令集扩展**:用户可以根据特定的应用需求添加新的指令,从而提升处理特定任务的效率。 - **寄存器文件扩展**:根据应用需求,可以增加或优化处理器的寄存器数量和类型。 - **协处理器扩展**:通过集成特定的协处理器,可以将专门的计算任务卸载到协处理器上,从而提升整体性能。 #### 可扩展性(Extensibility) Xtensa ISA不仅可配置,还拥有卓越的可扩展性,它支持多种扩展,包括: - **状态扩展(State Extensions)**:允许处理器添加新的状态寄存器,以支持新的功能或增强现有功能。 - **指令扩展(Instruction Extensions)**:...

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧