收藏 分销(赏)

湖南大学数据结构第5次作业演示教学.doc

上传人:a199****6536 文档编号:3794006 上传时间:2024-07-18 格式:DOC 页数:4 大小:105KB 下载积分:5 金币
下载 相关 举报
湖南大学数据结构第5次作业演示教学.doc_第1页
第1页 / 共4页
湖南大学数据结构第5次作业演示教学.doc_第2页
第2页 / 共4页


点击查看更多>>
资源描述
湖南大学数据结构第5次作业 精品文档 1、画出对下列存储于数组中的值执行buildheap后得到的最大值堆: 10 5 12 3 2 1 8 7 9 4 先序遍历为12 10 4 1 2 9 5 8 3 7 中序遍历为1 4 2 10 5 9 12 3 8 7 2、假设某字母表各个字母的权如下: Q Z F M T S O E 2 3 10 10 10 15 20 30 (a) 按照这个字母表,一个包含n个字母的字符串采用Huffman编码在最差情况下需要多少位?怎样的串会出现最差情况? 在最差的情况下需要5*n位,当所有的字母都是Q或者Z的时候。 (b)按照这个字母表,包含n个字母的字符串采用Huffman编码在最佳情况下需要多少位?怎样的串会出现最佳情况? 在最佳的情况下需要2*n位,当所有的字母都是E或者O的时候。 (c)按照一个字母表,一个字母平均需要多少位? (2*30 + 2*20 + 3*15 + 3*10 + 3*10 + 4*10 + 5*3+ 5*2)/100 =2.7 ∴ 2.7 3、 编写一个算法来判断两棵树是否相同。尽可能提高算法效率,并分析算法的运行时间代价。 template <class Elem> bool Compare(GTNode<Elem>* tree1, GTNode<Elem>* tree2) { GTNode<Elem> *num1, *num2; if (((tree1 == NULL) && (tree2 != NULL)) || ((tree2 == NULL) && (tree1 != NULL))) return 0; if ((t1 == NULL) && (t2 == NULL)) return 1; if (tree1->val() != tree2->val()) return 0; Num1 = tree1->left_child(); Num2 = tree2->left_child(); while(!((num1 == NULL) && (num2 == NULL))) { if (!Compare(num1, num2)) return false; if (num1 != NULL) num1 = num1->right_value(); if (num2 != NULL) num2 = num2->right_value(); }} O(n) 4、编写出一个函数,以一棵树为输入,返回树的结点数目。要求使用下面给出的GenTree和GTNode ADT。 // General tree node ADT Template <class Elem> class GTNode { Public: GTNode (const Elem&); // Constructor ~GTNode ( ); // Destructor Elem value ( ); Bool isLeaf ( ); GTNode * parent ( ); GTNode * right_sibling ( ); Void setValue ( Elem &); Void insert_first(GTNode <Elem>* n); // Insert first child Void insert_next(GTNode <Elem> * n); // Insert next sibling Void remove_first ( ); // Remove first child Void remove_next ( ); // Remove right sibling }; //General tree ADT Template <class Elem> class GenTree { Private: Void printhelp ( GTNode *) ; // Print helper function Public : GenTree ( ); //Constructor ~GenTree ( ); //Destructor Void clear ( ); // Send nodes to free store GTNode* root ( ); // Retrun the root // Combine two subtrees Void newroot (Elem& , GTNode <Elem>* ,GTNode<Elem>* ); Void print ( ); // print a tree }; template <class Elem> int gencount(GTNode<Elem>* subroot) { if (subroot == NULL) return 0 int count = 1; GTNode<Elem>* temp = rt->leftmost_child(); while (temp != NULL) { count += gencount(temp); temp = temp->right_sibling(); } return count; } 5、对下列用(6.3)式编码方法写出的树的顺序表示,画出树的形状。 XPC)Q)RV)M)))) 收集于网络,如有侵权请联系管理员删除
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服