博客
关于我
2017-2018-1 20162308 实验二 树
阅读量:797 次
发布时间:2023-04-04

本文共 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 MyLinkedBinaryTree
    implements 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集合框架的源码分析,进一步提升自身的技术能力。

    转载地址:http://gsrfk.baihongyu.com/

    你可能感兴趣的文章
    mysql 断电数据损坏,无法启动
    查看>>
    MySQL 日期时间类型的选择
    查看>>
    Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
    查看>>
    MySQL 是如何加锁的?
    查看>>
    MySQL 是怎样运行的 - InnoDB数据页结构
    查看>>
    mysql 更新子表_mysql 在update中实现子查询的方式
    查看>>
    MySQL 有什么优点?
    查看>>
    mysql 权限整理记录
    查看>>
    mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
    查看>>
    MYSQL 查看最大连接数和修改最大连接数
    查看>>
    MySQL 查看有哪些表
    查看>>
    mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
    查看>>
    MySql 查询以逗号分隔的字符串的方法(正则)
    查看>>
    MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
    查看>>
    mysql 查询,正数降序排序,负数升序排序
    查看>>
    MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
    查看>>
    mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
    查看>>
    mysql 死锁(先delete 后insert)日志分析
    查看>>
    MySQL 死锁了,怎么办?
    查看>>
    MySQL 深度分页性能急剧下降,该如何优化?
    查看>>