# Sub-structure of the tree

Source: Internet
Author: User
Enter Two Binary Trees, A and B, and determine whether B is a sub-structure. The nine-degree questions are described as follows:
`Description: Enter Two Binary Trees A and B to determine whether B is a sub-structure. Input: the input may contain multiple test examples. The input ends with EOF. For each test case, the input first line is an integer n, m (1 <= n <= 1000, 1 <= m <= ): N indicates the number of nodes of the Binary Tree A to be input (the number of nodes starts from 1), and M indicates the number of nodes of the Binary Tree B to be input (the number of nodes starts from 1 ). The next row contains N numbers, each representing the I-th element of the tree, and the next n rows. The first Ki number represents the number of children on the I-th node, next there are ki trees, representing the node I child node number. The next m + 1 line is the same as the description of tree. Output: corresponds to each test case. If B is a sub-tree of a, "yes" (excluding quotation marks) is output ). Otherwise, "no" is output (excluding quotation marks ). Example input: 7 38 8 7 9 2 4 72 2 32 4 5002 6 7008 9 22 2 300`
My Preface provides specific ideas in "offoffoffer", but the binary tree data structure definition provided by him obviously cannot meet the Nine-degree problem, because I really cannot come up with other methods besides using struct arrays to represent trees. Note that I am talking about pure C implementation, c ++ is different from other languages. Here, my binary tree node is defined as follows:
`// Binary Tree node defines struct btree {int value; int lchild, rchild ;};`
The structure of tree A and Tree B is as follows to determine whether Tree B is a sub-tree of tree A: to find whether tree A has a sub-tree similar to tree B, there are two steps:
1. Locate the node R with the same value as the root node of Tree B in tree.
2. Then, determine whether the sub-tree with R as the root node in tree A contains a sub-structure such as tree B.
It sounds like a recursive process (pure C). Now I only use pure C, PHP, and shell programming languages. I can basically understand C ++ and Java, we still need to get involved in a wider range. First, we need to lay a solid foundation for algorithms and computer systems, and then start the first step. In Tree A, we need to find the same value as that of the B root node, in fact, is the pre-order traversal of the tree, it is recommended to recursive, convenient (PS: Non recursion is nothing more than a stack storage node, no technical content, refer to the link: http://blog.csdn.net/zinss26914/article/details/8450952)
`/*** Step 1: traverse tree A to find out if there is a subtree equal to the B root node */INT judgechildtree (struct btree * ahead, int NUMA, struct btree * bhead, int numb) {int flag = 0; If (NUMA! =-1 & numb! =-1) {If (ahead [NUMA]. value = bhead [numb]. Value) Flag = doestree1hastree2 (ahead, NUMA, bhead, numb); If (! Flag & ahead [NUMA]. lchild! =-1) Flag = judgechildtree (ahead, ahead [NUMA]. lchild, bhead, numb); If (! Flag & ahead [NUMA]. rchild! =-1) Flag = judgechildtree (ahead, ahead [NUMA]. rchild, bhead, numb);} return flag ;}`

Step 2: Determine whether the subtree with R as the root node of tree A has the same node as that of Tree B.

`/*** Step 2: judge whether tree A has B sub-structure */INT doestree1hastree2 (struct btree * ahead, int NUMA, struct btree * bhead, int numb) {If (numb =-1) return 1; if (NUMA =-1) return 0; If (ahead [NUMA]. value! = Bhead [numb]. value) return 0; Return (doestree1hastree2 (ahead, ahead [NUMA]. lchild, bhead, bhead [numb]. lchild) & doestree1hastree2 (ahead, ahead [NUMA]. rchild, bhead, bhead [numb]. rchild ));}`
Complete code (9 degree system problems, I can't determine whether my code is AC, I think it is OK)
`# Include <stdio. h> # include <stdlib. h> // define the struct btree {int value; int lchild, rchild ;}; // The maximum number of knots of tree A and Tree B, int n, m; /*** Step 2: judge whether tree A has B sub-structure */INT doestree1hastree2 (struct btree * ahead, int NUMA, struct btree * bhead, int numb) {If (numb =-1) return 1; if (NUMA =-1) return 0; If (ahead [NUMA]. value! = Bhead [numb]. value) return 0; Return (doestree1hastree2 (ahead, ahead [NUMA]. lchild, bhead, bhead [numb]. lchild) & doestree1hastree2 (ahead, ahead [NUMA]. rchild, bhead, bhead [numb]. rchild);}/*** Step 1: traverse tree A to check whether there is a subtree equal to the B root node */INT judgechildtree (struct btree * ahead, int NUMA, struct btree * bhead, int numb) {int flag = 0; If (NUMA! =-1 & numb! =-1) {If (ahead [NUMA]. value = bhead [numb]. Value) Flag = doestree1hastree2 (ahead, NUMA, bhead, numb); If (! Flag & ahead [NUMA]. lchild! =-1) Flag = judgechildtree (ahead, ahead [NUMA]. lchild, bhead, numb); If (! Flag & ahead [NUMA]. rchild! =-1) Flag = judgechildtree (ahead, ahead [NUMA]. rchild, bhead, numb);} return flag;} int main (void) {int I, Data, Count, left, right, flag; struct btree * ahead, * bhead; while (scanf ("% d", & N, & M )! = EOF) {// get the node value of tree a ahead = (struct btree *) malloc (sizeof (struct btree) * n); for (I = 0; I <N; I ++) {scanf ("% d", & data); ahead [I]. value = data; ahead [I]. lchild = ahead [I]. rchild =-1 ;}for (I = 0; I <n; I ++) {scanf ("% d", & COUNT); If (COUNT = 0) {continue;} else {If (COUNT = 1) {scanf ("% d", & left); ahead [I]. lchild = left-1;} else {scanf ("% d", & left, & right); ahead [I]. lchild = left-1; ahead [I]. rchild = right-1 ;}}// obtain the node value bhead of Tree B = (struct btree *) malloc (sizeof (struct btree) * m ); for (I = 0; I <m; I ++) {scanf ("% d", & data); bhead [I]. value = data; bhead [I]. lchild = bhead [I]. rchild =-1 ;}for (I = 0; I <m; I ++) {scanf ("% d", & COUNT); If (COUNT = 0) {continue;} else {If (COUNT = 1) {scanf ("% d", & left); bhead [I]. lchild = left-1;} else {scanf ("% d", & left, & right); bhead [I]. lchild = left-1; bhead [I]. rchild = right-1 ;}}// determine whether the subtree of Tree B is a if (n = 0 | M = 0) {printf ("NO \ n"); continue;} flag = judgechildtree (ahead, 0, bhead, 0); If (FLAG) printf ("Yes \ n "); elseprintf ("NO \ n"); free (ahead); free (bhead);} return 0 ;}`
Related Keywords:

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.