Determine if the T1 tree has exactly the same subtree as the T2 tree __string

Source: Internet
Author: User
Tags serialization


Determine if the T1 tree has exactly the same subtree as the T2 tree public class containsamesubtree{//two fork tree node definition public static class node{public int Valu
		E
		Public Node left;

		public Node right;
		public Node (int data) {this.value=data; }//Solution one to determine whether each subtree in the H1 has the same structure as the H2, Time complexity O (n*m)//To determine whether there is the same subtree (solution two: first serialization, and then use the string KMP matching algorithm, time complexity of O (n+m)) public static
        Boolean Iscontainsamesubtree (Node h1,node h2) {if (H2==null) {return true;
        } if (H1==null) {return false;
        } String Str1=serialbypre (H1);

        String Str2=serialbypre (H2);

	Return Getindexof (STR1,STR2)!=-1; 
        }//First-order traversal serialization binary tree public static String Serialbypre (Node head) {if (head==null) {return "#!";
        } String res=head.value+ "!";
        Res+=serialbypre (Head.left);

        Res+=serialbypre (Head.right);

	return res; }//String matching (KMP algorithm) public static int Getindexof (string s,string m) {if(s==null| | m==null| | M.length () <1| |
    	S.length () <m.length ()) {return-1;
    	///Convert String to array Char[]ss=s.tochararray ();
        
        Char[]ms=m.tochararray ();
        int si=0;
        int mi=0;
        int []next=getnextarray (MS);
        		while (si<ss.length&&mi<ms.length) {if (Ss[si]==ms[mi]) {si++;
        	mi++;
        	else if (next[mi]==-1) {si++;
        	}else{Mi=next[mi];

    } return mi==ms.length?si-mi:-1; ///Get Next array public static Int[]getnextarray (CHAR[]MS) {if (ms.length==1) {return new
       INT[]{-1};
       int []next=new int[ms.length];
       Next[0]=-1;
       next[1]=0;
       int pos=2;
       int cn=0;
       	   while (pos<next.length) {if (MS[POS-1]==MS[CN]) {NEXT[POS++]=++CN;
      else if (cn>0) { 	   	CN=NEXT[CN];
       	   } else{next[pos++]=0;

    } return to Next;
      public static void Main (string[] args) {Node h1=new node (1);
      H1.left=new Node (2);
      H1.right=new Node (3);
      H1.left.left=new Node (4);
      H1.left.right=new Node (5);
      H1.right.left=new Node (6);
      H1.right.right=new Node (7);
      H1.left.left.right=new Node (8);

      H1.left.right.left=new Node (9);
      Node H2=new node (2);
      H2.left=new Node (4);
      H2.right=new Node (5);
      H2.left.right=new Node (8);


      H2.right.left=new Node (9);


	System.out.println (Iscontainsamesubtree (H1,H2)); }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.