2017-2018-1 20162308 实验二 树
发布日期:2025-06-07 22:09:50
浏览次数:3
分类:精选文章
本文共 4253 字,大约阅读时间需要 14 分钟。
实验二 报告
一、实验内容概述
本次实验的主要内容包括以下几个方面:
实现LinkedBinaryTree
- 完成链树LinkedBinaryTree的实现,包括getRight、contains、toString、preorder、postorder等方法。
- 使用JUnit或自编写驱动类对实现进行测试。
中序先序序列构造二叉树
- 基于LinkedBinaryTree,实现基于中序、先序序列构造唯一一棵二叉树的功能。
- 使用递归的方法,从根结点的两边开始构造子树。
决策树
- 完成PP16.6实验,设计一个猜小动物的诊断树。
表达式树
- 完成PP16.8实验,实现表达式树的构造与计算功能。
二叉搜索树
- 完成PP17.1实验,实现二叉搜索树的基本操作方法。
源码分析
- 参考教材和相关文档,分析Java中的红黑树(TreeMap、HashMap)源码。
二、详细实验内容
1. 实现LinkedBinaryTree
本实验的核心是实现一个基于链表的二叉树数据结构。主要完成以下方法:
- getRight:获取右孩子节点。
- contains:检查树中是否存在指定节点。
- toString:生成树的字符串表示。
- preorder:生成前序遍历结果。
- postorder:生成后序遍历结果。
代码实现如下:
import java.util.*;public class MyLinkedBinaryTreeimplements BinaryTree { protected BTNode root; public MyLinkedBinaryTree() { root = null; } public MyLinkedBinaryTree(T element) { root = new BTNode(element); } public MyLinkedBinaryTree(BTNode rootNode) { root = rootNode; } public MyLinkedBinaryTree(T element, MyLinkedBinaryTree left, MyLinkedBinaryTree right) { root = new BTNode(element); root.setLeft(left.root); root.setRight(right.root); } // ... (其他方法如getLeft、find、size等)}
2. 中序先序序列构造二叉树
基于前序和中序序列,使用递归方法构造二叉树。具体实现如下:
public class exp2 { final static char[] inorder = "HDIBEMJNAFCKGL".toCharArray(); final static char[] preorder = "ABDHIEJMNCFGKL".toCharArray(); public static void main(String[] args) { MyLinkedBinaryTree root = new MyLinkedBinaryTree(genetree(preorder, inorder)); System.out.println(root); } public static BTNode genetree(char[] pre, char[] in) { HashMap map = geneMap(in); return preIn(pre, 0, pre.length - 1, in, 0, in.length - 1, map); } public static HashMap geneMap(char[] charArray) { HashMap temp = new HashMap(); for (int i = 0; i < charArray.length; i++) { temp.put(charArray[i], i); } return temp; } public static BTNode preIn(char[] pre, int preIndex, int preJ, char[] in, int inIndex, int inJ, HashMap map) { if (preIndex > preJ) return null; BTNode head = new BTNode(pre[preIndex]); int index = map.get(pre[preIndex]); head.left = preIn(pre, preIndex + 1, preIndex + index - inIndex, in, inIndex, index - 1, map); head.right = preIn(pre, preIndex + index - inIndex + 1, preJ, in, index + 1, inJ, map); return head; }} 3. 决策树
完成PP16.6实验,设计一个诊断小动物的二叉树。树的结构如下:
public class BackPainExpert { private MyLinkedBinaryTree tree; public BackPainExpert() { // ... (树的初始化代码) public void diagnose() { Scanner scan = new Scanner(System.in); MyLinkedBinaryTree current = tree; while (current.size() > 1) { System.out.println(current.getRootElement()); String answer = scan.nextLine().equalsIgnoreCase("N"); current = current.getLeft(); } System.out.println(current.getRootElement()); }} 4. 表达式树
完成PP16.8实验,实现表达式树的构造与计算。代码实现如下:
public class ExpressionTree { private String exp; private BTNode root; public void geneTree(String s) { ArrayList oper = new ArrayList(); for (int i = 0; i < s.length(); i++) { char c = s.toCharArray()[i]; if (Character.isDigit(c)) { exp += c + " "; } else { oper.add(new BTNode(exp)); exp = ""; oper.add(c + " "); } } // ... (后续代码) public Integer getResult() { ArrayList strList = getStringList(toStr()); ArrayList rpn = getRPN(strList); return calculate(rpn); }} 5. 二叉搜索树
完成PP17.1实验,实现二叉搜索树的基本操作。代码如下:
public class LinkedBinarySearchTree> extends MyLinkedBinaryTree { public void add(T item) { if (root == null) { root = new BSTNode(item); } else { ((BSTNode) root).add(item); } }
6. 源码分析
参考教材和相关文档,分析TreeMap的源码。主要分析put和getEntry方法:
public V put(K key, V value) { TreeMapEntry t = root; // ... (完整代码)}public V get(Object key) { Entry p = getEntry(key); return (p == null ? null : p.value);} 三、总结与展望
本次实验主要完成了多个与二叉树相关的开发任务,包括数据结构实现、算法设计、树的构造与分析等内容。通过本次实验,我对Java高级数据结构的实现有了更深入的理解,也掌握了二叉树在实际应用中的构造与操作方法。未来,我将继续深入学习Java集合框架的源码分析,进一步提升自身的技术能力。
发表评论
最新留言
关注你微信了!
[***.104.42.241]2026年06月23日 01时11分09秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
PHP交换两个变量值
2023-03-01
php代码执行完整流程介绍
2023-03-01
PHP代码格式化工具phpcf常见问题解决方案
2023-03-01
PHP使用3DES算法加密解密字符串
2023-03-01
php使用memcached扩展的一个BUG
2023-03-01
PHP内核介绍及扩展开发指南—基础知识
2023-03-01
PHP写日志fwrite和file_put_contents的区别与性能
2023-03-01
PHP函数
2023-03-01
PHP函数__autoload失效原因(与smarty有关)
2023-03-01
PHP函数操作数字和汉字互转(100以内)
2023-03-01
PHP函数方法
2023-03-01
PHP删除指定目录下的所有文件和文件夹 | 删除指定文件
2023-03-01
php判断ip黑名单程序代码
2023-03-01
php判断复选框是否被选中的方法
2023-03-01
PHP判断指定目录下是否存在文件
2023-03-01
php判断数组是否为空
2023-03-01
PHP判断数组是否有重复值、获取重复值
2023-03-01
PHP利用正则表达式实现手机号码中间4位用星号(*)替换显示
2023-03-01
PHP加密与安全的最佳实践
2023-03-01