版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上武汉纺织大学数据结构实验报告班级: 专业 班 姓名: 序号: 实验时间: 年 月 日 指导教师: 实验二:二叉树操作及应用一、实验目的:1、掌握二叉树的基本概念、基本操作以及各种存储结构。2、掌握二叉树的多种遍历方法。2、掌握哈夫曼树以及哈夫曼编码的求取过程。二、实验内容: 1、编写一个程序,生成一棵二叉树并进行基本操作。 实验步骤: 、在Java语言编辑环境中新建程序,输入程序内容,并保存和编译; 、运行程序,从键盘输入二叉树各个结点数据,参考书本173页【例6.1】; 、显示菜单如下: 1先序遍历 2中序遍历 3后序遍历 4层次遍历 5求结点总数 6求高度 0退出
2、 、输入菜单选项,进行相应操作并输出结果。 可参考程序为:172页先序、中序、后序遍历;174页求结点个数、求高度;185页层次遍历。 2、编写一个程序,构造哈夫曼树并获取哈夫曼编码。 实验步骤: 、在Java语言编辑环境中新建程序,参考书本205-207页程序内容,并保存和编译; 、运行程序,根据指定权值,建立哈夫曼树; 、输出哈夫曼树存储结构信息; 、输出各个哈夫曼编码。 、如有能力,请将此程序修改为:从键盘上输入权值,并构造哈夫曼树、获取哈夫曼编码。 3、编写程序,实现对二叉树的中序线索化操作。实验步骤: 、在Java语言编辑环境中新建程序,参考书本190-193页程序内容,并保存和编译
3、; 、运行程序,建立二叉树存储结构; 、对二叉树进行中序线索化,建立中序线索二叉树; 、输出中序遍历序列。三、操作步骤:1.二叉树(1)接口类BinaryTTree.javapackage tree;public interface BinaryTTree<T> /二叉树接口,二叉树抽象数据类型boolean isEmpty();/判断二叉树是否为空int count();/返回二叉树的结点个数int height();/返回二叉树的高度void preOrder();/先根次序遍历二叉树void inOrder();/中根次序遍历二叉树void postOrder();/后根次序
4、遍历二叉树void levelOrder();/按层次遍历二叉树BinaryNode<T> search(T key);/查找并返回首次出现的关键字为key元素结点BinaryNode<T> getParent(BinaryNode<T> node);/返回node的父母结点void insertRoot(T x);/插入元素x作为根节点BinaryNode<T> insertChild(BinaryNode<T> p,T x,boolean leftChild);/插入孩子节点void removeChild(BinaryNode&
5、lt;T> p,boolean leftChild);/删除p结点的左或右子树void removeAll();/删除二叉树(2)二叉链表结点类BinaryNode.javapackage tree;public class BinaryNode<T> /二叉树的二叉链表结点类public T data;/数据域,存储数据元素public BinaryNode<T> left,right;/链域,分别指向左、右孩子结点/构造结点,参数分别指定元素和左、右孩子结点public BinaryNode(T data,BinaryNode<T>left,Bin
6、aryNode<T> right)this.data = data;this.left = left;this.right = right;public BinaryNode(T data)this(data,null,null);/构造指定值的叶子节点public BinaryNode()this(null,null,null);(3)二叉树类BinaryTree.javapackage tree;/二叉树类,实现BinaryTree<T>接口,泛型T指定节点的元素类型public class BinaryTree<T> implements Binary
7、TTree<T> public BinaryNode<T> root;public BinaryTree()this.root = null;public boolean isEmpty() return this.root=null;public int count() return count(root);public int count(BinaryNode<T> p)if(p=null)return 0;return 1+count(p.left)+count(p.right);public int height() return height(ro
8、ot);public int height(BinaryNode<T> p)if(p = null)return 0;int lh = height(p.left);int rh = height(p.right);return (lh>rh)?lh+1:rh+1;public void preOrder() System.out.print("先根次序遍历: ");preOrder(root);System.out.println();public void preOrder(BinaryNode<T> p)if(p!=null)System
9、.out.print(p.data.toString()+" ");preOrder(p.left);preOrder(p.right);public void inOrder() System.out.print("中根次序遍历: ");inOrder(root);System.out.println();public void inOrder(BinaryNode<T> p)if(p != null)inOrder(p.left);System.out.print(p.data.toString()+" ");inOr
10、der(p.right);public void postOrder() System.out.print("后根次序遍历: ");postOrder(root);System.out.println();public void postOrder(BinaryNode<T> p)if(p != null)postOrder(p.left);postOrder(p.right);System.out.print(p.data.toString()+" ");public void levelOrder() /按层次遍历二叉树LinkedQue
11、ue<BinaryNode<T>> que = new LinkedQueue<BinaryNode<T>>();/创建空队列BinaryNode<T> p = this.root;System.out.print("层次遍历: ");while(p != null)System.out.print(p.data+" ");if(p.left != null)que.enqueue(p.left);/p的左孩子结点入队if(p.right != null)que.enqueue(p.right)
12、;/p的右孩子结点入队p = que.dequeue();/p指向出队结点,若队列空返回nullSystem.out.println();(4)使用二叉树UseBinaryTree.javapackage tree;import java.util.Scanner;/* * * author pang * */public class UseBinaryTree public static BinaryTree<String> make(String a,String b,String c,String d,String e,String f,String g,String h)B
13、inaryTree<String> bitree = new BinaryTree<String>();BinaryNode<String> child_f,child_d,child_b,child_c;child_d = new BinaryNode<String>(d,null,new BinaryNode<String>(g);child_b = new BinaryNode<String>(b,child_d,null);child_f = new BinaryNode<String>(f,new B
14、inaryNode<String>(h),null);child_c = new BinaryNode<String>(c,new BinaryNode<String>(e),child_f);bitree.root = new BinaryNode<String>(a,child_b,child_c);return bitree;public static void main(String args)System.out.println("请输入结点数据:");Scanner scan = new Scanner(Syste
15、m.in);String a = scan.next();String b = scan.next();String c = scan.next();String d = scan.next();String e = scan.next();String f = scan.next();String g = scan.next();String h = scan.next();BinaryTree<String> bitree = make(a,b,c,d,e,f,g,h);System.out.println("1-先序遍历n2-中序遍历n3-后序遍历n4-层次遍历n5
16、-求结点总数n6-求高度n0-退出");System.out.println("输入菜单选项,进行相应操作并输出结果");while (true) int number = scan.nextInt();operate(bitree,number);/菜单功能方法public static void operate(BinaryTree<String> bitree,int a)switch(a)case 1:bitree.preOrder();break;case 2:bitree.inOrder();break;case 3:bitree.post
17、Order();break;case 4:bitree.levelOrder();break;case 5:System.out.println("二叉树结点数: "+bitree.count();break;case 6:System.out.println("二叉树高度: "+bitree.height();break;case 0:System.exit(0);default:System.out.println("请正确输入指令!");(5)注意事项二叉树类BinaryTree.java中有用到队列部分内容,需要导入包或者将该
18、部分文件放入tree包中才能使用(6)运行结果:2.哈夫曼树(1)二叉树的静态三叉链表结点类TriElement.javapackage huffman;public class TriElement /二叉树的三叉静态链表结点类int data;/数据域,表示权值int parent,left,right;/父母结点和左、右孩子结点下标public TriElement(int data,int parent,int left,int right)this.data = data;this.parent = parent;this.left = left;this.right = right
19、;public TriElement(int data)this(data,-1,-1,-1);public TriElement()this(0);public String toString()return "("+this.data+","+this.parent+","+this.left+","+this.right+")"(2)哈夫曼树类HuffmanTree.javapackage huffman;public class HuffmanTree /Huffman树private
20、int leafNum;/叶子结点private TriElement huftree;/Huffman树的结点数组public HuffmanTree(int weight)/构造指定权值集合的Huffman树int n = weight.length;/n个叶子结点this.leafNum = n;this.huftree = new TriElement2*n-1;/n个叶子结点的Huffman树共有2n-1个结点for(int i=0;i<n;i+)/结点数组初始化有n个叶子结点this.huftreei = new TriElement(weighti);for(int i=0
21、;i<n-1;i+)/构造n-1个2度将结点,每次循环构造1个2度结点int min1=Integer.MAX_VALUE,min2=min1;/最小和次最小权值,初值为整数最大值int x1=-1,x2=-1;/记录两个无父母的最小权值结点下标for(int j=0;j<n+i;j+)/查找两个无父母的最小权值结点if(huftreej.data<min1 && huftreej.parent=-1)min2 = min1;x2 = x1;min1 = huftreej.data;/min1记下最小权值x1 = j;/x1记下最小权值结点的下标else if
22、(huftreej.data<min2 && huftreej.parent=-1)min2 = huftreej.data;x2 = j;/x2记下次最小权值结点的下标huftreex1.parent = n+i;/将找出的两棵权值最小的子树合并为一棵子树huftreex2.parent = n+i;this.huftreen+i = new TriElement(huftreex1.data+huftreex2.data,-1,x1,x2);public String toString()String str="Huffman树的结点数组:n"fo
23、r(int i=0;i<this.huftree.length&&huftreei!=null;i+)str += "第"+i+"行"+this.huftreei.toString()+"n"return str;public String huffmanCodes()/返回当前Huffman树的Huffman编码String hufcodes = new Stringthis.leafNum;for(int i=0;i<this.leafNum;i+)/求n个叶子结点的Huffman编码hufcodesi
24、 = ""int child = i;int parent = huftreechild.parent;while(parent != -1)/由叶子结点向上直到根节点循环if(huftreeparent.left = child)hufcodesi="0"+hufcodesi;/左孩子结点编码为0elsehufcodesi="1"+hufcodesi;/右孩子结点编码为1child = parent;parent = huftreechild.parent;return hufcodes;(3)获取哈夫曼编码HuffmanTree_
25、example.javapackage huffman;import java.util.Scanner;import Josephus.SeqList;/* * * author pang * */public class HuffmanTree_example public static void main(String args)SeqList<Integer> list = new SeqList<Integer>();System.out.println("请依次输入权值并以0结束作为标识");Scanner scan = new Scan
26、ner(System.in);while(true)int x = scan.nextInt();if(x != 0)list.append(x);elsebreak;int weight = new intlist.length();for(int i = 0;i<list.length();i+)weighti = list.get(i);HuffmanTree htree = new HuffmanTree(weight);System.out.print(htree.toString();String code = htree.huffmanCodes();System.out.
27、print("Huffman编码: n");for(int i = 0;i<code.length;i+)System.out.println(char)('A'+i)+": "+codei+" ");(4)注意事项从键盘输入权值且不确定个数,需要一个标识符,这里用了0作为标识符。使用到了线性表部分内容,需要导入包或将文件放入此包中。(5)运行结果:3.二叉树的中序线索化(1)二叉链表结点类ThreadNode.javapackage threadBinaryTree;public class ThreadNod
28、e<T> public T data;public ThreadNode<T> left,right;public int ltag,rtag;public ThreadNode(T data,ThreadNode<T> left,int ltag,ThreadNode<T> right,int rtag)this.data = data;this.left = left;this.ltag = ltag;this.right = right;this.rtag = rtag;public ThreadNode(T data) this(data
29、,null,0,null,0);public ThreadNode() this(null,null,0,null,0);(2)中序线索二叉树类ThreadBinaryTree.javapackage threadBinaryTree;public class ThreadBinaryTree<T> public ThreadNode<T> root;public ThreadBinaryTree() this.root = null;public boolean isEmpty() return root = null;public ThreadBinaryTree(
30、T prelist)this.root = create(prelist);inorderThread(this.root);private int i = 0;private ThreadNode<T> create(T prelist) ThreadNode<T> p = null;if (i < prelist.length) T elem = prelisti+;if (elem != null) p = new ThreadNode<T>(elem);p.left = create(prelist);p.right = create(prelist);return p;private ThreadNode<T> front=null
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026 年中职导航无人机应用(无人机导航定位)试题及答案
- 2026 年中职船舶制造与修理(船体焊接)试题及答案
- 2025年医院感染控制师资格认证试题及真题
- 完整版塑钢门窗施工方案
- 2025年特种设备作业人员考试电梯安全知识试卷及答案
- 2025年机动车环保检测站培训试卷及答案
- 2025公证员资格考试题库及答案
- 2025年产品设计史考试题及答案
- 风电运检员笔试题库及答案
- 家纺行业织布工面试试题及答案
- (2026年)企业春节后复工复产安全教育培训课件
- 2026春季新学期校长在全体教师大会上精彩讲话:以“四好”践初心以实干育新人
- 铁路集中修施工培训
- 卫生技术管理正高
- 电商客服服务流程与话术手册
- Python深度学习入门(从零构建CNN和RNN)
- 小学信息科技课堂中人工智能教育实践研究教学研究课题报告
- 2026年桥梁耐久性与设计初衷的关系
- 2025年上海辅警招聘考试真题(附答案)
- (2025)继发性高血压筛查和诊断中国专家共识解读课件
- 钢管桩施工方案及质量控制
评论
0/150
提交评论