PTA- 04- whether tree 4 is the same binary search tree (25 points) [java language implementation]

04- Whether tree 4 is the same binary search tree (25 points) [java language implementation]

A binary search tree can be uniquely determined given an insertion sequence. However, a given binary search tree can be derived from many different insertion sequences. For example, inserting into an initially empty binary search tree according to the sequence {2, 1, 3} and {2, 3, 1} respectively, will get the same result. So for the various insertion sequences of the input, you need to determine whether they can generate the same binary search tree.

Input format:

The input contains several sets of test data. The first row of each set of data gives two positive integers N (≤10) and L, which are the number of elements inserted in each sequence and the number of sequences to be checked, respectively. Line 2 gives N space-separated positive integers as the initial insertion sequence. The last L lines, each giving the N inserted elements, belong to the L sequences that need to be checked.
For simplicity, we guarantee that each insertion sequence is a permutation of 1 to N. When reading N is 0, the input of the flag is over, and this group of data should not be processed.

Output format:

For each set of sequences to be checked, if the binary search tree generated by it is the same as that generated by the corresponding initial sequence, output "Yes", otherwise output "No".
Input sample:


4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0

Sample output:

Yes
No
No

ideas

You can rewrite it in java according to the code in Xiaobai's special session, and you need to pay attention to some small details.

code


import java.util.Scanner;

class BinarySearchTree{
    int value;
    BinarySearchTree left;
    BinarySearchTree right;

    int flag;

    /**
     * 够着函数,用于初始化
     *
     * https://blog.csdn.net/zjkC050818/article/details/53730108
     * @param value
     */
    public BinarySearchTree(int value){
        this.value = value;
        left = null;
        right = null;
        flag =0;
    }
}

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        int length=sc.nextInt();
        int N=0;
        BinarySearchTree root=null;
        while(length!=0){
            N=sc.nextInt();
            //建树
            root=buildTree(sc,length);
            //判断是否同一棵树,把结果存储到一棵树中
            boolean[] arr=new boolean[N];
            for(int i=0;i<N;i++){
                arr[i]=judge(root,sc,length);
                reset(root);
            }

            for(int j=0;j<N;j++){
               if(arr[j])
                    System.out.println("Yes");
                else
                    System.out.println("No");
            }
           // sc.nextInt();
            length=sc.nextInt();
        }

    }


    /**
     * 检测函数,这个不懂
     * @param value
     * @param t
     * @return
     */
    public static boolean check(int value,BinarySearchTree t){
        if (t.flag == 0){
            if (value == t.value){
                t.flag =1;
                return true;
            }
            else
                return false;
        }
        if (value>t.value){
            return check(value,t.right);
        }
        if (value<t.value){
            return check(value,t.left);
        }
        else {
            t.flag =1;
            return true;
        }
    }

    /**
     * 建立二叉搜索树
     * @param sc
     * @param length
     * @return
     */
    public  static  BinarySearchTree buildTree(Scanner sc,int length){
        BinarySearchTree root = null;
        int tem;
        for (int i=0;i<length;i++){
            tem = sc.nextInt();//读入
            root = insert(tem,root);
        }

        return root;//返回根结点
    }

    /**
     * 插入结点
     * @param value
     * @param t
     * @return
     */
    public static  BinarySearchTree insert(int value,BinarySearchTree t){
        if (t == null){/**如果为null,则实例化结点*/
            return new BinarySearchTree(value);
        }
        else {
            if (value>t.value){
                t.right = insert(value,t.right);
            }
            else if (value<t.value){
                t.left = insert(value,t.left);
            }
            /**如果已经存在,则什么都不做*/
        }

        return t;
    }

    /**
     * 判断是否合格,一个一个的 check
     * @param root
     * @param sc
     * @param length
     * @return
     */
    public static boolean judge(BinarySearchTree root,Scanner sc,int length) {
        int temp;
        boolean flag = true;/**false 表示还是一致的*/
        for (int j = 0; j < length; j++) {
            temp = sc.nextInt();
            if (!check(temp, root)) {
                //需要继续打印
                j++;//因为这里在for 循环本应该加1 的,但是被耽误了,不加一就会出现错位
                while (j < length) {
                    int x= sc.nextInt();
                    j++;
                }
                return false;
            }
        }
        return true;
    }

    /**
     * 重置 二叉树中的 flag
     * @param root
     */
    public static void reset(BinarySearchTree root){
        if (root.left != null)
            reset(root.left);
        if (root.right != null)
            reset(root.right);
        root.flag =0;
    }

}


Reference blog: zjkC050818's blog

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324951595&siteId=291194637