您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 数据结构与算法源程序下载
  所属分类: C
  开发工具:
  文件大小: 39kb
  下载次数: 0
  上传时间: 2009-04-08
  提 供 者: lsx***
 详细说明: #include "stdio.h" #include "malloc.h" struct Bitree { char nodes; struct Bitree *lchild; struct Bitree *rchild; }; struct node { char this; struct node *next; }; build( ) { struct Bitree *b,*bn; /*b指向二叉树,bn为操作数 */ struct node *c,*cn; /*c放置节点,作为一个小的队列.操作cn*/ int i,j,k; char no; i=1; /*每层有i个节点*/ printf("now we'll build the binary tree,,in the process,if the node is not exist ,enter '#'.first enter a boot\n"); /*先输入根节点*/ c=(struct node *)malloc(sizeof(struct node)); b=(struct Bi tree *)malloc(sizeof(struct Bitree)); scanf("%c",&no); c->this=no; b->nodes=no; bn=b; do { j=2*i; /*i的下一层有2i个节点,*/ for(k=1;k<=i;k++) { bn=c->this; printf("the left child of %c:",c->this); scanf("%c",&no); bn->lchild=(struct Bitree *)malloc(sizeof(struct Bitree)); bn->lchild->nodes=no; if (no=="#") { j=j-1; /*如果节点的孩子为空,节点数-1*/ } else { cn->next=(struct Bitree *)malloc(sizeof(struct Bitree)); cn->next->this=no; cn=cn->next; }; printf("the right child of %c:",c->this); scanf("%c",&no); bn->rchild=(struct Bitree *)malloc(sizeof(struct Bitree)); bn->rchild->nodes=no; if(no=="#") { j=j-1; } else { cn->next=(struct Bitree *)malloc(sizeof(struct Bitree)); cn->next->this=no; cn=cn->next; }; c=c->next; }; i=j; }while(i!=0); } firstorder(struct Bitree *n) { printf("%c",n->nodes); if(n->nodes=="#") ; else firstorder(n->lchild); firstorder(n->rchild); } main() { struct Bitree *o; build(o); firstorder(o); } 回答:2006-06-07 16:51 提问者对答案的评价: 共0条评论...其他回答 共2条回答评论 ┆ 举报 飞鱼 [新手] 二叉树实现源代码如下: #include #include #include #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define OVERFLOW -2 typedef int status; typedef struct BiNode { char Data; struct BiNode* lChild; struct BiNode* rChild; }BiNode,*pBiNode; status CreateTree(BiNode** pTree); status PreOrderTraval(BiNode* pTree); status Visit(char Data); status Display(BiNode* pTree,int Level); status Clear(BiNode* pTree); BiNode *pRoot=NULL; main() { clrscr(); CreateTree(&pRoot); printf("\nPreOrder:"); PreOrderTraval(pRoot); printf("\n"); printf("\nInOrder:"); InOrderTraval(pRoot); printf("\n"); printf("\nPostOrder:"); PostOrderTraval(pRoot); printf("\n"); printf("\nShowLeaves:"); ShowLeaves(pRoot); printf("\n-----------------------\n"); printf("\n"); Display(pRoot,0); printf("\n"); printf("\nDeleting Tree:\n"); DelTree(pRoot); printf("BiTree Deleted."); getch(); } status CreateTree(BiNode** pTree) /*Input Example: abd##e##cf##g##*/ { char ch; scanf("%c",&ch); if(ch==‘#‘) { (*pTree)=NULL; } else { if(!((*pTree)=(BiNode*)malloc(sizeof(BiNode)))) { exit(OVERFLOW); } (*pTree)->Data=ch; CreateTree(&((*pTree)->lChild)); CreateTree(&((*pTree)->rChild)); } return OK; } status PreOrderTraval(BiNode* pTree) { if(pTree) { if(Visit(pTree->Data)) { if(PreOrderTraval(pTree->lChild)) { if(PreOrderTraval(pTree->rChild)) { return OK; } } } return ERROR; } else { return OK; } } status InOrderTraval(BiNode* pTree) { if(pTree) { if(InOrderTraval(pTree->lChild)) { if(Visit(pTree->Data)) { if(InOrderTraval(pTree->rChild)) { return OK; } } return ERROR; } return ERROR; } else { return OK; } } status PostOrderTraval(BiNode* pTree) { if(pTree) { if(PostOrderTraval(pTree->lChild)) { if(PostOrderTraval(pTree->rChild)) { if(Visit(pTree->Data)) { return OK; } return ERROR; } } return ERROR; } else { return OK; } } status Visit(char Data) { printf("%c",Data); return OK; } status Display(BiNode* pTree,int Level) { int i; if(pTree==NULL) return; Display(pTree->lChild,Level+1); for(i=0;i=1) { printf("--"); } printf("%c\n",pTree->Data); Display(pTree->rChild,Level+1); } status ShowLeaves(BiNode* pTree) { if(pTree) { if(ShowLeaves(pTree->lChild)) { if(ShowLeaves(pTree->rChild)) { if((pTree->lChild==NULL)&&(pTree->rChild==NULL)) { if(!Visit(pTree->Data)) { return ERROR; } } return OK; } } return ERROR; } else { return OK; } } status DelTree(BiNode* pTree) { if(pTree) { if(DelTree(pTree->lChild)) { if(DelTree(pTree->rChild)) { printf("Deleting %c\n",pTree->Data); free((void*)pTree); return OK; } } return ERROR; } else { return OK; } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 输入关键字,在本站1000多万海量源码库中尽情搜索: