zoukankan      html  css  js  c++  java
  • 20192320杨坤数据结构实验八

    《数据结构与面向对象程序设计》实验八报告

    课程:《程序设计与数据结构》

    班级: 1923

    姓名: 杨坤

    学号:20192320

    实验教师:王自强

    实验日期:2020年12月3日

    必修/选修: 必修

    1.实验内容

    参考教材PP16.1,完成链树LinkedBinaryTree的实现(getRight,contains,toString,preorder,postorder)

    用JUnit或自己编写驱动类对自己实现的LinkedBinaryTree进行测试,提交测试代码运行截图,要全屏,包含自己的学号信息

    课下把代码推送到代码托管平台

    基于LinkedBinaryTree,实现基于(中序,先序)序列构造唯一一棵二㕚树的功能,比如给出中序HDIBEMJNAFCKGL和后序ABDHIEJMNCFGKL,构造出附图中的树

    用JUnit或自己编写驱动类对自己实现的功能进行测试,提交测试代码运行截图,要全屏,包含自己的学号信息

    课下把代码推送到代码托管平台

    自己设计并实现一颗决策树

    提交测试代码运行截图,要全屏,包含自己的学号信息

    课下把代码推送到代码托管平台

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

    代码

    public interface BinaryTreeADT<T> {
        public T getRootElement();
        public boolean isEmpty();
        public int size();
        public int height();
        public boolean contains(T target);
        public BinaryTreeNode find(T target);
        public String IteratorInorder();
        public String IteratorPreorder();
        public String IteratorPostorder();
        public String IteratorLevel();
    
    }
    
    public class BinaryTreeNode<T> {
        protected T element;
        protected BinaryTreeNode left, right;
    
        public BinaryTreeNode() {
            element = null;
            left = null;
            right = null;
        }
    
        public BinaryTreeNode(T element) {
            this.element = element;
            left = right = null;
        }
    
        public BinaryTreeNode getLeft() {
            return left;
        }
    
        public BinaryTreeNode getRight() {
            return right;
        }
    
        public void setLeft(BinaryTreeNode left) {
            this.left = left;
        }
    
        public void setRight(BinaryTreeNode right) {
            this.right = right;
        }
    
        public BinaryTreeNode find(T target) {
            BinaryTreeNode result = null;
            if (element.equals(target)) return this;
            else {
                if (left != null) result = left.find(target);
                if (right != null) result = right.find(target);
            }
            return result;
        }
    
        public int count() {
            int counter = 1;
            if (left != null) counter += left.count();
            if (right != null) counter += right.count();
            return counter;
        }
    
        public int level() {
            int level = 1, max = 0;
            if (left != null) {
                max = left.level();
            }
            if (right != null && right.level() > max) max = right.level();
            return level + max;
        }
    
        public String IteratorInOrder() {
            String s = "";
            if (left != null) s += left.IteratorInOrder();
            s += this.element.toString() + " ";
            if (right != null) s += right.IteratorInOrder();
            return s;
        }
    
        public String IteratorPreOrder() {
            String s = "";
            s += this.element.toString() + " ";
            if (left != null) s += left.IteratorPreOrder();
            if (right != null) s += right.IteratorPreOrder();
            return s;
        }
    
        public String IteratorPostOrder() {
            String s = "";
            if (left != null) s += left.IteratorPostOrder();
            if (right != null) s += right.IteratorPostOrder();
            s += this.element.toString() + " ";
            return s;
        }
    
        public BinaryTreeNode Insert(char s) {
            BinaryTreeNode result = null;
            if (left == null) {
                left = new BinaryTreeNode(s);
                result=left;
            }
            else if(!left.element.equals('#'))result=left.Insert(s);
            if(result==null){
                if (right == null) {
                    right= new BinaryTreeNode(s);
                    result=right;
                }
                else if(!right.element.equals('#'))result=right.Insert(s);
            }
            return result;
        }
        public void Insert1(String a,String b) {
            int i = 0, j;
            for (j = 0; b.charAt(j) != a.charAt(i) && j < b.length(); j++) ;
            if (left == null) {
                if(j!=0) {
                    left = new BinaryTreeNode(a.charAt(1));
                    left.Insert1(a.substring(i + 1, j + 1), b.substring(0, j)); }
            }
            if (right == null) {
                if(j!=b.length()-1) {
                    right = new BinaryTreeNode(a.charAt(j + 1));
                    right.Insert1(a.substring(j + 1), b.substring(j + 1)); }
            }
        }
    
    }
    
    
    public class LinkedBinaryTree <T>implements BinaryTreeADT{
        BinaryTreeNode root;
        public LinkedBinaryTree(){
            root=null;
        }
        public LinkedBinaryTree(T element){
            root=new BinaryTreeNode(element);
        }
        public LinkedBinaryTree(T element,LinkedBinaryTree left,LinkedBinaryTree right){
            root=new BinaryTreeNode(element);
            root.setLeft(left.root);
            root.setRight(right.root);
        }
        @Override
        public T getRootElement() {
            if(root==null)return null;
            return (T) root.element;
        }
    
        @Override
        public boolean isEmpty() {
            if(root==null)return true;
            else return false;
        }
        public LinkedBinaryTree getLeft(){
            if(root==null)return null;
            LinkedBinaryTree result = new LinkedBinaryTree();
            result.root = root.getLeft();
            return result;
        }
        public LinkedBinaryTree getRight(){
            if(root==null)return null;
            LinkedBinaryTree result = new LinkedBinaryTree();
            result.root = root.getRight();
            return result;
        }
        public void CreateTree(String s){
            root=new BinaryTreeNode(s.charAt(0));
            for(int i=1;i<s.length();i++){
                root.Insert(s.charAt(i));
            }
        }
        public void CreateTree1(String a,String b){
            root=new BinaryTreeNode(a.charAt(0));
            root.Insert1(a,b);
        }
        @Override
        public int size() {
            if(root==null)return 0;
            return root.count();
        }
    
        @Override
        public int height() {
            if(root==null)return 0;
            return root.level();
        }
    
        @Override
        public boolean contains(Object target) {
            if(root==null)return false;
            T a=(T)target;
            if(root.find(a)==null)return false;
            else return true;
        }
    
        @Override
        public BinaryTreeNode find(Object target) {
            if(root==null)return null;
            T a=(T)target;
            return root.find(a);
        }
    
        @Override
        public String IteratorInorder() {
            if(root==null)return null;
            else return root.IteratorInOrder();
        }
    
        @Override
        public String IteratorPreorder() {
            if(root==null)return null;
            else return root.IteratorPreOrder();
        }
    
        @Override
        public String IteratorPostorder() {
            if(root==null)return null;
            else return root.IteratorPostOrder();
        }
    
        @Override
        public String IteratorLevel() {
            String s="";
            int i,j,count=1;
            int t[]=new int[20];
            BinaryTreeNode m[]=new BinaryTreeNode [20];
            m[0]=root;
            t[0]=count;
            for(i=0,j=1,count=2;i!=j;i++){
                if(m[i].left!=null){m[j]=m[i].left;t[j]=t[i]+1;j++;}
                if(m[i].right!=null){m[j]=m[i].right;t[j]=t[i]+1;j++;}
                s+=m[i].element+" ";
                if(t[i+1]!=t[i]) s+="
    ";
            }
            return s;
        }
    
        public String ToString(){
            return root.IteratorPreOrder();
        }
    
    }
    
    public class BStest {
        public static void main(String[] args) {
            LinkedBinaryTree tree=new LinkedBinaryTree();
            tree.CreateTree1("ABDHIEJMNCFGKL","HDIBEMJNAFCKGL");
            System.out.println(tree.IteratorLevel());
        }
    
    }
    
    public class decisionTree<T> {
    
    
        public static void main(String[] args) {
            String e0 = "开始游戏!选择亚索还是劲夫?";
            String e1 = "你这把选择亚索";
            String E1 = "你这把选择劲夫";
            String m1 = "这把亚索你选择什么天赋呢?";
    
    
            String e2 = "这把你亚索选择天赋:迅捷步伐";
            String E2 = "这把你亚索选择天赋:征服者";
    
            String e3 = "这把你劲夫选择天赋:丛刃";
            String E3 = "这把你劲夫选择天赋:征服者";
    
            String e4 = "你这把选择走上路";
            String E4 = "你这把选择走中路";
    
            String e5 = "这把游戏输了,亚索是孤儿";
            String e6 = "这把游戏输了,但是你快乐到了太空";
            String e7 = "这把游戏输了,没有女性角色,劲夫不好发挥";
            String e8 = "这把游戏赢了,万豪带丛刃,劲夫变叶问";
            String E7 = "输了,劲夫倒了(哭腔)";
            String E8 = "劲夫劲夫,全场芜湖";
    
    
            String choose = "0";
    
    
            BTNode<String> n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, N2;
            TreeNode treeNode = new TreeNode();
            treeNode.insert(e0);
            /*System.out.println(e1);
            treeNode.insert('Y');
            treeNode.insert('N');
            System.out.println(e2);
            treeNode.insert('Y');
            treeNode.insert('N');*/
            //选择英雄
            n1 = treeNode.otherInsert(treeNode.root, e1, E1);
            //选择天赋
            n2 = treeNode.otherInsert(n1.left, e2, E2);
            n3 = treeNode.otherInsert(n1.right, e3, E3);
            //选择对线路
            n4 = treeNode.otherInsert(n2.left, e4, E4);
            n5 = treeNode.otherInsert(n2.right, e4, E4);
            n6 = treeNode.otherInsert(n3.left, e4, E4);
            n7 = treeNode.otherInsert(n3.right, e4, E4);
    
    
    
    
            //游戏结果
            n8 = treeNode.otherInsert(n4.left, e5, e5);
            n9 = treeNode.otherInsert(n4.right, e6, e6);
            n10 = treeNode.otherInsert(n5.left, e5, e5);
            n11 = treeNode.otherInsert(n5.right, e5, e5);
            n12 = treeNode.otherInsert(n6.left, e7, e7);
            n13 = treeNode.otherInsert(n6.right, e8, e8);
            n14 = treeNode.otherInsert(n7.left, E8, E8);
            n15 = treeNode.otherInsert(n7.right, E7, E7);
    
            BTNode current = new BTNode();
    
            Scanner scanner = new Scanner(System.in);
            current = treeNode.root;
           
           
            System.out.println("选择英雄"+"
    "+"选择天赋:"+"
    "+"亚索:迅捷步伐与征服者 "+" 劲夫:丛刃与征服者"+"
    "+"选择上路还是中路");
    
            while (treeNode.size(current) >2 ) {
                System.out.println(current.data);
    
                System.out.println("选择第一个输入Y第二个输入N");
                if (scanner.nextLine().equalsIgnoreCase("N"))
                    current = current.right;
                else current = current.left;
    
    
            }
            current=current.left;
            
            System.out.println(current.data);
    
    
        }
    
    
    }
    

    中缀的代码:

    public class MiddleToRear {
    
        public static void main(String[] args) {
            //不用索引扫描表达式,将表达式加入到list中
            String expression;
            Scanner scanner=new Scanner(System.in);
            expression=scanner.next();
            //得到中缀表达式转为list的集合
            List<String> list=getList(expression);
            //将中缀表达式的集合转到后缀表达式的集合
            List<String> rearList=getRearList(list);
            System.out.println("rearList = " + rearList);
    
            //输出计算结果
            System.out.println(getResult(rearList));
    
    
        }
    
        //中缀表达式的集合转为后缀表达式的集合
        private static List<String> getRearList(List<String> list) {
            ArrayList<String> rearList = new ArrayList<>();
            //定义符号栈
            Stack<String> operatorStack = new Stack<>();
            //定义集合代替数字栈,数字栈本身并没有pop操作,并且最后要倒叙输出,随意用list代替
            ArrayList<String> numList = new ArrayList<>();
            for (String s : list) {
                //如果是个数字
                if (s.matches("\d+")){
                    numList.add(s);
                }else if (s.equals("(")){
                    operatorStack.push(s);
                }else if (s.equals(")")){
                    //符号栈栈顶直到左括号之前的符号放入数字栈
                    while (!"(".equals(operatorStack.peek())){
                        numList.add(operatorStack.pop());
                    }
                    //把左括号弹出来
                    operatorStack.pop();
                    //判断优先级,当前符号优先级小于等于符号栈栈顶优先级,将符号栈栈顶弹出加入数字栈,
                    //直到找到当前符号优先级大于符号栈栈顶优先级为止,再将当前符号加入符号栈
                }else{
                    while (operatorStack.size()!=0 && (OperPriority.getPriority(s) <= OperPriority.getPriority(operatorStack.peek()))){
                        numList.add(operatorStack.pop());
                    }
                    //将当前符号加入符号栈
                    operatorStack.push(s);
                }
            }
            //将符号栈中剩余符号加入数字栈
            while (operatorStack.size()!=0){
                numList.add(operatorStack.pop());
            }
            return numList;
        }
    
        //将中缀表达式的各个字符存到list集合,方面遍历
        private static List<String> getList(String expression) {
            ArrayList<String> list = new ArrayList<>();
            int index=0;
            String str=""; //多位数的拼接
            char c;  //用于存放每次扫描到的结果
            do {
                //判断是否为数字,非数字直接加入list
                c=expression.charAt(index);
                if (c < 48 || c >57){
                    list.add(""+c);
                }else {//数字.考虑可能不是一位数,字符串拼接
                    str+=c;
                    if (index == expression.length()-1){
                        list.add(str);
                    }else {
                        if (expression.charAt(index+1) < 48 || expression.charAt(index+1) > 57){
                            list.add(str);
                            str="";
                        }
                    }
                }
                index++;
            }while (index < expression.length());
            return list;
        }
        //输出计算结果
        private static int getResult(List<String> list) {
            Stack<String> stack = new Stack<>();
            for (String s : list) {
                //匹配数字
                if (s.matches("\d+")){
                    stack.push(s);
                }else {
                    int num01=Integer.parseInt(stack.pop());
                    int num02=Integer.parseInt(stack.pop());
                    int result=0;
                    if (s.equals("+")){
                        result=num01+num02;
                    }else if (s.equals("-")){
                        result=num02-num01;
                    }else if (s.equals("*")){
                        result=num02*num01;
                    }else if (s.equals("/")){
                        result=num02/num01;
                    }else {
                        throw new RuntimeException("无法解析的字符串");
                    }
                    stack.push(""+result);
                }
            }
            return Integer.parseInt(stack.pop());
        }
    
    
    }
    

    截图:



    2.实验心得体会

    自身的自学能力还要提升,大多数的实验代码都在同学的帮助下完成,在运到困难时,容易感到沮丧,还要多锻炼自己。平时也要多加练习。
    这是一个新的软件,一切都是陌生的,在遇到这种问题的时候,多上网搜集资料是非常必要的,同时多家运用学习的app在上面观看相关的视频能够更好的掌握。

  • 相关阅读:
    手动执行把nconf生成的配置文件附加到nagios下
    创建django工程
    mysql安装、配置、导入数据库
    PHP里<?php和<?通用要在配置里怎么设置_百度知道
    Loganalyzer数据库乱码解决方法:
    RSyslog安装配置
    修改cacti的系统时间
    Mysql5.7.11 安装 cacti0.8.8f ,在导入cacti.sql数据库时出现下记错误,导致数据库导入终止: ERROR 1067 (42000) at line 1847: Invalid default value for 'status_fail_date'
    中间件组件
    cookie与session 组件
  • 原文地址:https://www.cnblogs.com/yk20192320/p/14111210.html
Copyright © 2011-2022 走看看