是否是同一颗树 java实现

7-4 是否同一棵二叉搜索树(25 分)

给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果。于是对于输入的各种插入序列,你需要判断它们是否能生成一样的二叉搜索树。

输入格式:

输入包含若干组测试数据。每组数据的第1行给出两个正整数N (10)和L,分别是每个序列插入元素的个数和需要检查的序列个数。第2行给出N个以空格分隔的正整数,作为初始插入序列。最后L行,每行给出N个插入的元素,属于L个需要检查的序列。

简单起见,我们保证每个插入序列都是1到N的一个排列。当读到N为0时,标志输入结束,这组数据不要处理。

输出格式:

对每一组需要检查的序列,如果其生成的二叉搜索树跟对应的初始序列生成的一样,输出“Yes”,否则输出“No”。

输入样例:

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

输出样例:

Yes
No
No

思路:1.创建树的节点类型
   2.创建树类型
   3.创建一个树型链表,储存树
   4.比较树是否相同
import java.util.Scanner;
//创建树的节点
class Node 
{
    int key;
    Node left;
    Node right;
    Node next;
    public Node()
    {
        key=-1;
        left=null;
        right=null;
        next=null;
    }
    public Node(int key)
    {
        this.key=key;
        left=null;
        right=null;
        next=null;
    }
}
//创建树
class tree
{
    Node root;
    Node leftchild;
    Node rightchild;
    tree next;
    public tree()
    {
        root=new Node();
        leftchild=new Node();
        rightchild=new Node();
        next=null;
        root.left=leftchild;
        root.right=rightchild;    
    }
    public boolean isEmpty()
    {
        return(root.key==-1);
    }
    //向树中加入节点
    public void addNode(Node node)
    {    
        if (isEmpty())
        {
            root=node;
            root.left=null;
            root.right=null;
        }
        Node temp=root;    
        if(!isEmpty())
        {
            if ((node.key<root.key)&&(root.left==null))
            {
                root.left=node;
            }
            if ((node.key>root.key)&&(root.right==null))
            {
                root.right=node;
            }
            if ((node.key<root.key)&&(root.left!=null))
            {
                root=root.left;
                addNode(node);
            }
            if (node.key>root.key&&root.right!=null)
            {
                root=root.right;
                addNode(node);
            }
        }
        root=temp;    
    }
    public Node getRoot()
    {
        return this.root;
    }
    
}
//创建链表,由于不知道给出数据的组数,因此用一个动态的容器来装建立的树
class list
{
    tree head;
    tree rear;

    public list()
    {
        head=new tree();
        rear=new tree();
        head=rear;
    }
    public boolean isEmpty()
    {return(head==rear);}
    public void addTree(tree t)
    {
        if (isEmpty())
        {
            head.next=t;
            rear=t;
            rear.next=null;
        }
        else
        {
            rear.next=t;
            rear=t;
        }
    }    
}
//测试类
class test
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        list arr=biuldTree(sc);
        tree temp=arr.head.next;
        while (temp.next!=null )
        {
            
            if(arr.head.next.getRoot().key==temp.next.getRoot().key)
            {
                boolean result=isSame(arr.head.next.getRoot(),temp.next.getRoot());
                if (result)
                    System.out.println("Yes");
                else
                    System.out.println("No");
                temp=temp.next;    
            }
            else
            {
                arr.head=temp;
                temp=temp.next;
                
                boolean result=isSame(arr.head.next.getRoot(),temp.next.getRoot());
                if (result)
                    System.out.println("Yes");
                else
                    System.out.println("No");
                temp=temp.next;

            }
        }    
    }
    //建立树
    public static list biuldTree(Scanner sc) 
    {
        int N=sc.nextInt();
        //int num=sc.nextInt();
        list arr=new list();
        while (N!=0)
        {
            int num=sc.nextInt();
            for (int i=0;i<=num ; i++)
            {
                
                tree t=new tree();
                for (int a=0;a<N ;a++ )
                {
                    Node node=new Node();
                    node.key=sc.nextInt();
                    t.addNode(node);
                }
                arr.addTree(t);
            }
            N=sc.nextInt();
            
        }
        return arr;
    }
    //比较两棵树是否相同
    public static boolean isSame(Node root1,Node root2)
    {
        if (root1==null&&root2==null)
        {
            return true;
        }
        if ((root1==null&&root2!=null)||(root1!=null&&root2==null))
        {
            return false;
        }
        if (root1.key!=root2.key)
        {
            return false;
        }
        if ((root1.key==root2.key)&&(root1.left==null)&&(root2.left==null))
        {
            return(isSame(root1.right,root2.right));
        }
        if ((root1.key==root2.key)&&(root1.right==null)&&(root2.right==null))
        {
            return(isSame(root1.left,root2.left));
        }
        else
            return((isSame(root1.left,root2.left))&&(isSame(root1.right,root2.right)));

    }
}

测试结果

猜你喜欢

转载自www.cnblogs.com/javaStudy947/p/8964590.html