树的先根遍历

来源:互联网 发布:淘宝卖家申请直播 编辑:程序博客网 时间:2024/05/09 15:43
#include <stdio.h>#include <stdlib.h>typedef char DataType;typedef struct CSNode{DataType  data;                  /*结点信息*/struct CSNode  *FirstChild;      /*第一个孩子*/struct CSNode  *Nextsibling;     /*下一个兄弟*/}CSNode, *CSTree;CSNode *find(CSNode *root, char value){CSNode *q;if(root == NULL)return (NULL);elseif(root->data == value)q=root;else{q = find(root->FirstChild,value);if(q == NULL)q = find(root->Nextsibling,value);}return q;}CSNode * create(){char pvalue,cvalue;int i;int type;CSNode *p,*q,*s,*head;i=1;printf("请输入建立树序列(以$表示结束)!\n");printf("第%d个结点=>根结点值:",i);i++;scanf("%c",&cvalue);if(cvalue != '$'){head=(CSNode *)malloc(sizeof(CSNode));head->data = cvalue;head->FirstChild = NULL;head->Nextsibling = NULL;}elsereturn (NULL);do{printf("第%d个结点=>结构关联父结点值:",i);i++;fflush(stdin);scanf("%c",&pvalue);if(pvalue != '$'){do {printf("   孩子(0)或兄弟(1)结点:");fflush(stdin);scanf("%d",&type);} while(type!=0 && type!=1);printf("        当前结点值为:");fflush(stdin);scanf("%c",&cvalue);p = head;q = find(p,pvalue);if (q!=NULL){s=(CSNode *)malloc(sizeof(CSNode));s->data = cvalue;s->FirstChild = NULL;s->Nextsibling = NULL;if(type == 0)q->FirstChild = s;if(type == 1)q->Nextsibling = s;}else{printf("已建立的树中没有此结点!\n");i--;}}}while(pvalue != '$');return (head);}void  RootFirst(CSTree  root) {if (root!=NULL){printf("%c  ",root->data);           /*访问根结点*/RootFirst (root->FirstChild);   /*先根遍历首子树*/RootFirst (root->Nextsibling);  /*先根遍历兄弟树*/}}/*void  RootFirst(CSTree  root) { CSNode *p;if (root!=NULL){printf("%c  ",root->data);        / * 访问根结点 * /p= root->FirstChild;while (p!=NULL){RootFirst(p);      / * 访问以p为根的子树 * /p = p->Nextsibling;}}}*/int main(){CSTree ct;int layer;layer = 0;ct = create();RootFirst(ct);printf("\n");}

0 0
原创粉丝点击