1. 程式人生 > 實用技巧 >20192312吳欣欣 實驗八 《面向物件程式設計》實驗報告

20192312吳欣欣 實驗八 《面向物件程式設計》實驗報告

20192312 2020-2021-1 實驗七 《查詢與排序》實驗報告

課程:《程式設計與資料結構》
班級: 1923
姓名: 吳欣欣
學號:20192312
實驗教師:王志強
實驗日期:2020年12月8日
必修/選修: 必修

1.實驗內容

1.參考教材PP16.1,完成鏈樹LinkedBinaryTree的實現(getRight,contains,toString,preorder,postorder)
用JUnit或自己編寫驅動類對自己實現的LinkedBinaryTree進行測試,提交測試程式碼執行截圖,要全屏,包含自己的學號資訊
課下把程式碼推送到程式碼託管平臺

2.基於LinkedBinaryTree,實現基於(中序,先序)序列構造唯一一棵二㕚樹的功能,比如給出中序HDIBEMJNAFCKGL和後序ABDHIEJMNCFGKL,構造出附圖中的樹
用JUnit或自己編寫驅動類對自己實現的功能進行測試,提交測試程式碼執行截圖,要全屏,包含自己的學號資訊
課下把程式碼推送到程式碼託管平臺

3.自己設計並實現一顆決策樹
提交測試程式碼執行截圖,要全屏,包含自己的學號資訊
課下把程式碼推送到程式碼託管平臺

4.輸入中綴表示式,使用樹將中綴表示式轉換為字尾表示式,並輸出字尾表示式和計算結果(如果沒有用樹,正常評分。如果用到了樹,即使有小的問題,也酌情給滿分)
提交測試程式碼執行截圖,要全屏,包含自己的學號資訊

2.實驗過程及結果

1.參考教材PP16.1,完成鏈樹LinkedBinaryTree的實現(getRight,contains,toString,preorder,postorder)

用JUnit或自己編寫驅動類對自己實現的LinkedBinaryTree進行測試,提交測試程式碼執行截圖,要全屏,包含自己的學號資訊
課下把程式碼推送到程式碼託管平臺

package exe8;

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 T getElement() {
        return element;
    }

    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)); }
            }
        }
}


package exe8;


import java.util.Scanner;

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);
    }
    public void CreateTree2(){//決策樹
        String s1="今天吃火鍋了嗎?";
        String s2="點了蝦滑和鵪鶉蛋嗎?";
        String s3="點的是整份嗎?";
        String s4="今天是不幸的一天。";
        String s5="今天是不完整的一天。";
        String s6="今天是幸福但不完整的一天。";
        String s7="今天是幸福又充實的一天";
        BinaryTreeNode n1,n2,n3,n4,n5,n6,n7;
        n1=new BinaryTreeNode(s1);
        n2=new BinaryTreeNode(s2);
        n3=new BinaryTreeNode(s3);
        n4=new BinaryTreeNode(s4);
        n5=new BinaryTreeNode(s5);
        n6=new BinaryTreeNode(s6);
        n7=new BinaryTreeNode(s7);
        n1.setLeft(n2);
        n1.setRight(n4);
        n2.setLeft(n3);
        n2.setRight(n5);
        n3.setLeft(n7);
        n3.setRight(n6);
        root=n1;
        decision(root);
    }
    public void decision(BinaryTreeNode root){
        Scanner scan=new Scanner(System.in);
        BinaryTreeNode current=root;
        while(current.left!=null||current.right!=null){
            System.out.print(current.getElement()+"(請輸入Y/N): ");
            if(scan.next().equalsIgnoreCase("y"))current=current.left;
            else current=current.right;
        }
        System.out.println(current.getElement());
    }

    @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();
    }
}

2.基於LinkedBinaryTree,實現基於(中序,先序)序列構造唯一一棵二㕚樹的功能,比如給出中序HDIBEMJNAFCKGL和後序ABDHIEJMNCFGKL,構造出附圖中的樹

用JUnit或自己編寫驅動類對自己實現的功能進行測試,提交測試程式碼執行截圖,要全屏,包含自己的學號資訊
課下把程式碼推送到程式碼託管平臺

3.自己設計並實現一顆決策樹

提交測試程式碼執行截圖,要全屏,包含自己的學號資訊
課下把程式碼推送到程式碼託管平臺

4.輸入中綴表示式,使用樹將中綴表示式轉換為字尾表示式,並輸出字尾表示式和計算結果(如果沒有用樹,正常評分。如果用到了樹,即使有小的問題,也酌情給滿分)

提交測試程式碼執行截圖,要全屏,包含自己的學號資訊

package exe8;


import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;

public class BTtree <T>{
    BinaryTreeNode root;
    public BTtree (){
        root=null;
    }
    public void CreateTree(String s){
        Stack<BinaryTreeNode> num= new Stack<BinaryTreeNode>();
        Stack<BinaryTreeNode> num1= new Stack<BinaryTreeNode>();
        Queue<BinaryTreeNode> op=new LinkedList<BinaryTreeNode>();
        BinaryTreeNode temp,temp1;
        StringTokenizer str=new StringTokenizer(s,"+-*/",false);
        while(str.hasMoreTokens()){num1.push(new BinaryTreeNode(str.nextToken()));}
        while(!num1.isEmpty()){num.push(num1.pop());}
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)=='+'||s.charAt(i)=='-'||s.charAt(i)=='*'||s.charAt(i)=='/')
                op.offer(new BinaryTreeNode(s.charAt(i)));
        }
        while(!op.isEmpty()){
            temp=op.poll();
            if(op.isEmpty()){
                temp.left=num.pop();
                temp.right=num.pop();
                num.push(temp);
                break;
            }
            if(pri(temp.element.toString().charAt(0),op.peek().element.toString().charAt(0))){
                temp.left=num.pop();
                temp.right=num.pop();
                num.push(temp);
            }
            else{
                temp.left=num.pop();
                temp.right=op.peek();
                int count=0;
                while(count==0){
                    temp1=op.poll();
                    if(op.isEmpty()){
                        temp1.left=num.pop();
                        temp1.right=num.pop();
                        num.push(temp1);
                        break;
                    }
                    if(pri(temp1.element.toString().charAt(0),op.peek().element.toString().charAt(0))){
                        temp1.left=num.pop();
                        temp1.right=num.pop ();
                        count=1;
                    }
                    else{
                        temp1.left=num.pop();
                        temp1.right=op.peek();
                    }
                }
                num.push(temp);
            }
        }
        root=num.peek();
    }
    public String IteratorPostorder() {
        if(root==null)return null;
        else return root.IteratorPostOrder();
    }
    public boolean pri(char a,char  b){
        boolean result=true;
        if(a=='+'||a=='-'){
            switch(b){
                case '+':result=true;break;
                case '-':result=true;break;
                case '*':result=false;break;
                case '/':result=false;break;
                default:result=false;break;
            }
        }
        if(a=='*'||a=='/'){
            switch(b){
                case '+':result=true;break;
                case '-':result=true;break;
                case '*':result=true;break;
                case '/':result=true;break;
                default:result=false;break;
            }
        }
        return result;
    }
}


3.心得體會

經過學習對樹的瞭解又深了一層。通過中綴表示式轉字尾表示式,也回顧了關於棧的知識。