数据结构实验7.doc

  1. 1、本文档共4页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
数据结构实验7

判断一棵二叉树是否为完全二叉树的函数(提示:这是一个遍历问题) #includestdio.h #includestdlib.h #define MAXSIZE 100 typedef char elemtype; typedef struct node { elemtype data; struct node *leftchild; struct node *rightchild; }BiTreeNode; //初始化二叉树 Initiate(BiTreeNode **root) { *root=(BiTreeNode *)malloc(sizeof(BiTreeNode)); (*root)-leftchild=NULL; (*root)-rightchild=NULL; } //构建二叉树 BiTreeNode* CreateBiTree(BiTreeNode**root) { char ch; scanf(%c,ch); if(ch==#) { *root=NULL; } else { if(((*root)=(BiTreeNode*)malloc(sizeof(BiTreeNode)))==NULL) return NULL; (*root)-data=ch; CreateBiTree((*root)-leftchild); CreateBiTree((*root)-rightchild); } return *root; } int FullBiTree(BiTreeNode*root) { BiTreeNode *Queue[MAXSIZE]; //定义一个队列存放 int front,rear,i; int flag; if(root==NULL) return 0; front=-1;i=-1;rear=0; Queue[rear]=root; while(i!=rear) { i++; if(Queue[i]-leftchild!=NULL) Queue[++rear]=Queue[i]-leftchild; if(Queue[i]-rightchild!=NULL) Queue[++rear]=Queue[i]-rightchild; } flag=1; for(front=0;front=rear;front++) { if(flag) { if(Queue[front]-leftchild!=NULLQueue[front]-rightchild==NULL) { flag=0; } if(Queue[front]-leftchild==NULLQueue[front]-rightchild==NULL) { flag=0; } if(Queue[front]-leftchild==NULLQueue[front]-rightchild!=NULL) { return 0; } } else { if(Queue[front]-leftchild!=NULL||Queue[front]-rightchild!=NULL) return 0; } } return 1; } Destory(BiTreeNode** root) { if((*root)!=NULL(*root)-leftchild!=NULL) { Destory((*root)-leftchild); } if((*root)!=NULL(*root)-rightchild!=NULL) { Destory((*root)-rightchild); } free(*root); } main() { BiTreeNode *root; int a; Initiate(root); printf(输入一个二叉树:); CreateBiTree(root); a=FullBiTree(root); if(a) { printf(该二叉树是完全二叉树。\n); } else { printf(该二叉树不是完全二叉树。\n); } Destory(root); } 2、哈夫曼编码的程序设计。要进行哈夫曼编码的字符集为{A,B,C,D}各字符在电文中出现的次数集为{1,3,5,7}要求编写实现哈夫曼树构造和哈夫曼树编码的函数,并输出最终各个字符的哈夫曼编码。

文档评论(0)

xy88118 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档