1. 程式人生 > 實用技巧 >20192320楊坤資料結構實驗八

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+="\n";
        }
        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("選擇英雄"+"\n"+"選擇天賦:"+"\n"+"亞索:迅捷步伐與征服者 "+" 勁夫:叢刃與征服者"+"\n"+"選擇上路還是中路");

        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在上面觀看相關的視訊能夠更好的掌握。