实验八报告

学号20192308 2020-2021-1 《数据结构与面向对象程序设计》实验8报告

课程:《程序设计与数据结构》
班级: 1923
姓名: 王泽荣
学号:20192308
实验教师:王志强
实验日期:2020年12月3日
必修/选修: 必修

1.实验内容

参考教材PP16.1,完成链树LinkedBinaryTree的实现(getRight,contains,toString,preorder,postorder)
用JUnit或自己编写驱动类对自己实现的LinkedBinaryTree进行测试,提交测试代码运行截图,要全屏,包含自己的学号信息
课下把代码推送到代码托管平台

基于LinkedBinaryTree,实现基于(中序,先序)序列构造唯一一棵二㕚树的功能,比如给出中序HDIBEMJNAFCKGL和后序ABDHIEJMNCFGKL,构造出附图中的树
用JUnit或自己编写驱动类对自己实现的功能进行测试,提交测试代码运行截图,要全屏,包含自己的学号信息
课下把代码推送到代码托管平台

自己设计并实现一颗决策树
提交测试代码运行截图,要全屏,包含自己的学号信息
课下把代码推送到代码托管平台

输入中缀表达式,使用树将中缀表达式转换为后缀表达式,并输出后缀表达式和计算结果(如果没有用树,正常评分。如果用到了树,即使有小的问题,也酌情给满分)
提交测试代码运行截图,要全屏,包含自己的学号信息

2. 实验过程及结果

第一部分,
完成链树LinkedBinaryTree的实现代码如下

import java.util.ArrayList;

public class BTNode<T>
{
    protected T element;
    protected BTNode<T> left, right;

    public BTNode (T element)
    {
        this.element = element;
        left = right = null;
    }

    public T getElement()
    {
        return element;
    }

    public void setElement (T element)
    {
        this.element = element;
    }

    public BTNode<T> getLeft()
    {
        return left;
    }

    public void setLeft (BTNode<T> left)
    {
        this.left = left;
    }

    public BTNode<T> getRight()
    {
        return right;
    }

    public void setRight (BTNode<T> right)
    {
        this.right = right;
    }

    public BTNode<T> find (T target) {
        BTNode<T> result = null;

        if (element.equals(target))
            result = this;
        else
        {
            if (left != null)
                result = left.find(target);
            if (result == null && right != null)
                result = right.find(target);
        }

        return result;
    }

    public int count() {
        int result = 1;

        if (left != null)
            result += left.count();

        if (right != null)
            result += right.count();

        return result;
    }

    public void inorder (ArrayList<T> iter) {
        if (left != null)
            left.inorder (iter);

        iter.add (element);

        if (right != null)
            right.inorder (iter);
    }

    public void preorder (ArrayList<T> iter) {
        iter.add (element);

        if (left != null)
            left.inorder (iter);

        if (right != null)
            right.inorder (iter);
    }

    public void postorder (ArrayList<T> iter) {
        if (left != null)
            left.inorder (iter);

        if (right != null)
            right.inorder (iter);

        iter.add (element);
    }

    public char print() {
        return (char) element;
    }
import java.util.ArrayList;

public interface BinaryTree<T>
{
    public T getRootElement();
    public BinaryTree<T> getLeft();
    public BinaryTree<T> getRight();
    public boolean contains(T target);
    public T find(T target);
    public boolean isEmpty();
    public int size();
    public String toString();
    public ArrayList<T> preorder();
    public ArrayList<T> inorder();
    public ArrayList<T> postorder();
    public ArrayList<T> levelorder();
}
mport javafoundations.*;
import javafoundations.exceptions.*;


import java.util.ArrayList;
import java.util.Arrays;

public class LinkedBinaryTree<T> implements BinaryTree<T>
{
    protected BTNode<T> root;

    public LinkedBinaryTree()
    {
        root = null;
    }


    public LinkedBinaryTree (T element)
    {
        root = new BTNode<T>(element);
    }

    public LinkedBinaryTree (T element, LinkedBinaryTree<T> left, LinkedBinaryTree<T> right) {
        root = new BTNode<T>(element);
        root.setLeft(left.root);
        root.setRight(right.root);
    }

    public T getRootElement() {
        if (root == null)
            throw new EmptyCollectionException("Get root operation "
                    + "failed. The tree is empty.");

        return root.getElement();
    }

    public LinkedBinaryTree<T> getLeft() {
        if (root == null)
            throw new EmptyCollectionException("Get left operation "
                    + "failed. The tree is empty.");

        LinkedBinaryTree<T> result = new LinkedBinaryTree<T>();
        result.root = root.getLeft();

        return result;
    }

    public T find (T target) {
        BTNode<T> node = null;

        if (root != null)
            node = root.find(target);

        if (node == null)
            throw new ElementNotFoundException("Find operation failed. "
                    + "No such element in tree.");

        return node.getElement();
    }

    public int size() {
        int result = 0;

        if (root != null)
            result = root.count();

        return result;
    }

    public ArrayList<T> inorder() {
        ArrayList<T> iter = new ArrayList<T>();

        if (root != null)
            root.inorder (iter);

        return iter;
    }

    public ArrayList<T> levelorder() {
        LinkedQueue<BTNode<T>> queue = new LinkedQueue<BTNode<T>>();
        ArrayList<T> iter = new ArrayList<T>();

        if (root != null)
        {
            queue.enqueue(root);
            while (!queue.isEmpty())
            {
                BTNode<T> current = queue.dequeue();

                iter.add (current.getElement());

                if (current.getLeft() != null)
                    queue.enqueue(current.getLeft());
                if (current.getRight() != null)
                    queue.enqueue(current.getRight());
            }
        }

        return iter;
    }

    public LinkedBinaryTree<T> getRight() {
        if (root == null)
            throw new EmptyCollectionException("Get left operation "
                    + "failed. The tree is empty.");

        LinkedBinaryTree<T> result = new LinkedBinaryTree<T>();
        result.root = root.getRight();

        return result; }

    public boolean contains (T target) {
        if(root.find(target)==null)
            return false;
        else
            return true;
    }

    public boolean isEmpty() {
        if(root!=null)
            return false;
        else
            return true;
    }

    public String toString() {
        return super.toString();
    }

    public ArrayList<T> preorder() {
        ArrayList<T> iter = new ArrayList<T>();

        if (root != null)
            root.preorder (iter);

        return iter;
    }

    public ArrayList<T> postorder() {
        ArrayList<T> iter = new ArrayList<T>();

        if (root != null)
            root.postorder (iter);

        return iter;
    }

    public BTNode construct(char[] pre, char[] in){
        if (pre.length == 0 || in.length == 0) {
            return null;
        }
        BTNode<Character> tree = new BTNode<Character>(pre[0]);
        int index = search(0, in.length, in, tree.getElement());
        tree.setLeft(construct(Arrays.copyOfRange(pre, 1, index + 1), Arrays.copyOfRange(in, 0, index)));
        tree.setRight(construct(Arrays.copyOfRange(pre, index + 1, pre.length),
                Arrays.copyOfRange(in, index + 1, in.length)));
        return tree;
    }

    public int search(int start, int end, char[] inOrders, char data) {
        for (int i = start; i < end; i++) {
            if (data == inOrders[i]) {
                return i;
            }
        }
        return -1;
    }

    public void preOrder(BTNode<T> Tree)
    {
        System.out.print(Tree.getElement()+" ");
        BTNode<T> leftTree = Tree.left;
        if(leftTree != null)
        {
            preOrder(leftTree);
        }
        BTNode<T> rightTree = Tree.right;
        if(rightTree != null)
        {
            preOrder(rightTree);
        }
    }

    public static void inOrder(BTNode tree)
    {
        if(tree == null)
            return;
        else
        {
            inOrder(tree.left);
            System.out.print(tree.print()+" ");
            inOrder(tree.right);
        }
    }
}

foundation 包的截图

测试:

第二部分的代码和截图

public class part2 {
    public static void main(String[] args) {
        LinkedBinaryTree t = new LinkedBinaryTree();
        BTNode tree;
        char[] pre = {'A','B','D','H','I','E','J','M','N','C','F','G','K','L'};
        char[] in = {'H','D','I','B','E','M','J','N','A','F','C','K','G','L'};
        tree = t.construct(pre,in);

        System.out.println("先序遍历");
        t.preOrder(tree);
        System.out.println("\n中序遍历");
        t.inOrder(tree);
    }
}

第三部分的代码

import java.util.Scanner;

public class Expert {
    private LinkedBinaryTree<String> tree;

    public Expert(){
        String e1 = "喜欢球鞋?";
        String e2 = "有钱?";
        String e3 = "那没事了";
        String e4 = "你觉得李宁球鞋好吗";
        String e5 = "没钱你说个锤子";
        String e6 = "那你去买李宁去吧";
        String e7 = "就是,李宁脸都不要了,四百块一双鞋能炒到一千多是真的离谱";

        LinkedBinaryTree<String> n2,n3,n4,n5,n6,n7;
        n3 = new LinkedBinaryTree<String>(e3);
        n5 = new LinkedBinaryTree<String>(e5);
        n6 = new LinkedBinaryTree<String>(e6);
        n7 = new LinkedBinaryTree<String>(e7);
        n4 = new LinkedBinaryTree<String>(e4,n6,n7);
        n2 = new LinkedBinaryTree<String>(e2,n4,n5);

        tree = new LinkedBinaryTree<String>(e1,n2,n3);
    }

    public void diagnose(){
        Scanner scan = new Scanner(System.in);
        LinkedBinaryTree<String> current = tree;

        System.out.println("开始");
        while(current.size()>1)
        {
            System.out.println(current.getRootElement());
            if(scan.nextLine().equalsIgnoreCase("Y"))
                current = current.getLeft();
            else
                current = current.getRight();
        }
        System.out.println(current.getRootElement());
    }
}

最后一部分,表达式的代码截图

import java.util.Stack;

public class Fix {
    static Stack<Character> op = new Stack<>();

    public static Float getv(char op, Float f1, Float f2) {
        if (op == '+') return f2 + f1;
        else if (op == '-') return f2 - f1;
        else if (op == '*') return f2 * f1;
        else if (op == '/') return f2 / f1;
        else return Float.valueOf(-0);
    }

    public static float calrp(String rp) {
        Stack<Float> v = new Stack<>();
        char[] arr = rp.toCharArray();
        int len = arr.length;
        for (int i = 0; i < len; i++) {
            Character ch = arr[i];
            if (ch >= '0' && ch <= '9') v.push(Float.valueOf(ch - '0'));
            else v.push(getv(ch, v.pop(), v.pop()));
        }
        return v.pop();
    }

    public static String getrp(String s) {
        char[] arr = s.toCharArray();
        int len = arr.length;
        String out = "";
        for (int i = 0; i < len; i++) {
            char ch = arr[i];
            if (ch == ' ') continue;
            if (ch >= '0' && ch <= '9') {
                out += ch;
                continue;
            }
            if (ch == '(')
                op.push(ch);
            if (ch == '+' || ch == '-') {
                while (!op.empty() && (op.peek() != '('))
                    out += op.pop();
                op.push(ch);
                continue;
            }
            if (ch == '*' || ch == '/') {
                while (!op.empty() && (op.peek() == '*' || op.peek() == '/'))
                    out += op.pop();
                op.push(ch);
                continue;
            }
            if (ch == ')') {
                while (!op.empty() && op.peek() != '(')
                    out += op.pop();
                op.pop();
                continue;
            }
        }
        while (!op.empty()) out += op.pop();
        return out;
    }
}


3. 实验过程中遇到的问题和解决过程

  • 问题1 教材代码中的ArrayIterator类无法识别

  • 问题1解决方案:修改方法的定义类型为ArrayList

  • 问题2 书中代码内容残缺,对实验要求理解不全

  • 问题2 已解决,尽力补全代码使得书上程序能运行

其他(感悟、思考等)

树是一个比较难掌握的数据结构,虽然上学期在离散课上接触过了,但知识掌握程度还停留在理论方面,真正代码实现的时候——特别是许多方法涉及递归算法的时候——我觉得还是很困难的,需要更多的实践去练习。

posted @ 2020-12-09 21:42  就是个哈皮  阅读(127)  评论(0编辑  收藏  举报