Binary tree (----) Whether a binary tree contains another binary tree

Source: Internet
Author: User


Binary Tree Problem Collection


2, problem description

The data of each node of the binary tree A and b (int data) is stored in different files, stored in the sequence traversal and the middle sequence traversal, according to these two traversal reconstruction binary tree, and determine whether the binary tree A contains two fork tree B.


3. Algorithm description

(1) The first sequence traversal of the node data and the sequential traversal sequence are read into the array.

(2) Reconstruct binary tree A and B according to their respective pre-sequence traversal and middle sequence traversal respectively

(3) Determine if B is in a

Code:

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <memory.h> #include <stack>using namespace Std;enum Common_size{buf_max_size = 1024,max_num_len = 10,};enum Error_value {input_param_error = -1,OPEN_FILE_ERROR = -2,FILE_ Not_exsit = -3,file_is_emtpy = -4,alloca_failure = -5,rebuild_btree_error = -6,num_overflow_error = -7,};typedef struct BT Reenode_t_{int m_nvalue;struct btreenode_t_ *m_pleft;struct btreenode_t_ *m_pright;} Btreenode_t;void Release_btree (btreenode_t *proot) {if (Proot = NULL) return;if (proot->m_pleft) Release_btree ( Proot->m_pleft), if (proot->m_pright) Release_btree (proot->m_pright), free (proot);p root = Null;return;}  btreenode_t * Reconstruct_btree_by_preinorder (int *ppreorder, int *pinorder, int tree_nodes_total) {if (PPreOrder = = NULL || Pinorder = = NULL) {fprintf (stderr, "Error:while construct btree by preinorder\n"); return NULL;} InchT m_nvalue = ppreorder[0];int Root_data_index = -1;int i = 0;for (i = 0; i < tree_nodes_total; ++i) {if (ppreorder[0] = = Pinorder[i]) {root_data_index = I;break;}} if (Root_data_index = =-1) {fprintf (stderr, "note:ppreorder[0" is%d, pinorder[0] is%d\n ", ppreorder[0], pinorder[0]); FPR intf (stderr, "Note:root_data_index is-1, Total nodes:%d\n", tree_nodes_total); return NULL;} btreenode_t *proot = Null;proot = (btreenode_t *) malloc (sizeof (btreenode_t)), if (proot = = NULL) {fprintf (stderr, "Erro R:can ' t alloca btree node:%d\n ", M_nvalue); return NULL;} Proot->m_nvalue = M_nvalue;proot->m_pleft = Null;proot->m_pright = Null;int Left_tree_len = Root_data_index; int Right_tree_len = Tree_nodes_total-left_tree_len-1;int *pleft_preorder = ppreorder + 1;int *pright_preorder = PPreO Rder + 1 + left_tree_len;int *pleft_inorder = Pinorder;int *pright_inorder = pinorder + Left_tree_len + 1;if (left_tree_le n > 0) {proot->m_pleft = Reconstruct_btree_by_preinorder (Pleft_preordeR, Pleft_inorder, Left_tree_len); if (proot->m_pleft = = NULL) {fprintf (stderr, "error:failure to rebuild Leftree, now R Elease data:%d\n ", M_nvalue); Release_btree (proot);p root = Null;return NULL;}} if (Right_tree_len > 0) {proot->m_pright = Reconstruct_btree_by_preinorder (Pright_preorder, Pright_inorder, Right_tree_len); if (proot->m_pright = = NULL) {fprintf (stderr, "error:failure to right tree, now release data:%d\n", M _nvalue); Release_btree (proot);p root = Null;return NULL;}} return proot;} int Get_traver_data (char *buf, int *porder, int *order_len) {if (buf = = NULL | | porder = NULL) {return input_param_error ;} Char *ptr = Buf;char *pnumstart = Ptr;int i = 0;int Numlen = 0;int Get_no_num = 0;int flag = 0;fprintf (stderr, "Note:now Enter Get_traver_data () \ n "), while (*ptr! = ')} {if ((*ptr >= ' 0 ') && (*ptr <= ' 9 ')) {++numlen;} else I F (*ptr = = ') {*ptr = ' + '; if (Numlen > 0 && numlen <= max_num_len) {Porder[i] = atoi (Ptr-numlen); fpriNTF (stderr, "Note:num is:%d\n", Porder[i]); ++i;} else if (Numlen > Max_num_len) {flag = Num_overflow_error;break;} Numlen = 0;} Else{get_no_num = -1;break;} ++ptr;} if (Numlen! = 0) {Porder[i] = atoi (Ptr-numlen); fprintf (stderr, "Note:num is:%d\n", Porder[i]);} if (get_no_num! = 0) Return Get_no_num;*order_len = i + 1;fprintf (stderr, "Note:finish get_traver_data () \ n"); return flag; }int get_traverse_nodes (char * file_name, int **ppreorder, int **pinorder, int *tree_nodes_total) {if (file_name = = NULL {fprintf (stderr, "Error:file (%s), line (%d), input parameter error\n", __file__, __line__); return input_param_error;} if (Access (file_name, F_OK)! = 0) {fprintf (stderr, "Error:file (%s) not exsit\n", file_name); return file_not_exsit;} struct stat fstat;size_t file_size = -1;stat (file_name, &fstat); file_size = fstat.st_size;if (file_size = = 0) {fprintf (stderr, "Error:file (%s) is empty\n", file_name); return file_is_emtpy;} fprintf (stderr, "Note:file size:%ld\n", fstat.st_size); char * BUF = NULL;BUF = (char *) malloc ((file_size + 1) * sizeof (char)), if (buf = = NULL) {fprintf (stderr, "Error:alloca buf Fer failure\n "); return alloca_failure;} File *input = fopen (file_name, "RB"), if (input = = NULL) {fprintf (stderr, "Error:can ' t open input file [%s]\n], file_name ); free (buf); buf = Null;return open_file_error;}  int Line_len = -1;int index = 0;int flag = 0;int Fini_read = 0;int Preorder_len = 0;int Inorder_len = 0;while (Fgets (BUF, File_size, input)! = NULL) {size_t Buf_len = strlen (BUF); if (buf[buf_len-1] = = ' \ n ') buf[buf_len-1] = ' + '; fpri NTF (stderr, "Note:current line was:%s\n", buf); switch (index) {case 0: {*ppreorder = (int. *) malloc (Buf_len * sizeof (in t)); if (*ppreorder = = NULL) {flag = -1;break;} fprintf (stderr, "Note:finish to get ppreorder\n"); flag = Get_traver_data (buf, *ppreorder, &preorder_len); Case 1: {*pinorder = (int *) malloc (Buf_len * sizeof (int)); if (*pinorder = = NULL) {flag = -1;break;} fprintf (stderr, "Note:finish to get pinorder\n "); flag = Get_traver_data (buf, *pinorder, &inorder_len); break;} Default:{break;}} ++INDEX;IF (flag! = 0 | | index = = 2) break;} if (flag! = 0) | | (Preorder_len! = Inorder_len)) {fprintf (stderr, "Error:flag is%d, Preorder_len are%d, Inorder_len is%d\n", Flag, Preorder_len, Inorder_len); if (*ppreo Rder) {free (*ppreorder); *ppreorder = NULL;} if (*pinorder) {free (*pinorder); *pinorder = NULL;} flag =-1;}  Free (BUF); buf = = Null;fclose (input); input = Null;*tree_nodes_total = preorder_len;fprintf (stderr, "note:sucess finish Get_traverse_nodes () \ n "); return flag;} void Print_btree (btreenode_t *proot) {if (proot = = null) return;stack< btreenode_t *> st;while (proot! = NULL | |!st . empty ()) {while (proot! = NULL) {printf ("Preorder Test:node data:%d\n", Proot->m_nvalue), St.push (proot);p root = PRo Ot->m_pleft;} if (!st.empty ()) {proot = St.top (); St.pop ();p root = Proot->m_pright;}} return;} void Pprint_btree (btreenode_t *proot) {if (Proot = = NULL) return;fprintf (stderR, "Preorder Test:node:%d\n", Proot->m_nvalue), if (proot->m_pleft) Print_btree (proot->m_pleft); if (pRoot- >m_pright) Print_btree (proot->m_pright); return;} int Check_is_include_helper (btreenode_t *proot1, btreenode_t *proot2) {if (pRoot1 = = NULL | | pRoot2 = NULL) {fprintf (stde RR, "Error:in check_is_include_helper (), input param error\n"); return input_param_error;} Stack <btreenode_t *> st1;stack <btreenode_t *> st2;int flag = 0;while ((pRoot1! = NULL | |!st1.empty ()) & & (PRoot2! = NULL | |!st2.empty ())) {while (pRoot1! = NULL && PRoot2! = null) {printf ("Note:cur data:proot1-& Gt;m_nvalue:%d, Proot2->m_nvalue:%d\n ", Proot1->m_nvalue, Proot2->m_nvalue); if (proot1->m_nvalue! = Proot2->m_nvalue) {flag = -1;break;} St1.push (PROOT1); St2.push (pRoot2);p Root1 = Proot1->m_pleft;proot2 = Proot2->m_pleft;} if (flag! = 0) break;if (!st1.empty () &&!st2.empty ()) {pRoot1 = St1.top (); St1.pop ();p Root2 = St2.top (); St2.pop (); PROOT1 = Proot1->m_pright;proot2 = Proot2->m_pright;}} if (pRoot2! = NULL | |!st2.empty ()) {flag =-1;} while (!st1.empty ()) {St1.pop ();} while (!st2.empty ()) {St2.pop ();} return flag;} int Check_is_include (btreenode_t *proot1, btreenode_t *proot2) {if (pRoot1 = = NULL | | pRoot2 = NULL) {fprintf (stderr, "ER Ror:in check_is_include (), input param error\n "); return input_param_error;}  Stack <BTreeNode_t*> st;int flag = -1;while (pRoot1! = NULL | |!st.empty ()) {while (pRoot1! = null) {printf ("Note:now Check node data:%d\n ", proot1->m_nvalue); if (Check_is_include_helper (pRoot1, pRoot2) = = 0) {flag = 0;break;} St.push (pRoot1);p Root1 = Proot1->m_pleft;} if (flag = = 0) break;if (!st.empty ()) {pRoot1 = St.top (); St.pop ();p Root1 = Proot1->m_pright;}} while (!st.empty ()) St.pop (); return flag;} int main (int argc, char * * argv) {if (ARGC < 3) {fprintf (stderr, "Error:file (%s), line (%d), input parameter error\n", __file__, __line__); return input_param_error;} Char *afile = Argv[1];char *bfile = argv[2];int ret = 0;int *ppreorder = null;int *pinorder = NULL; btreenode_t *proot1 = NULL; btreenode_t *proot2 = Null;int tree_nodes_total = 0;ret = Get_traverse_nodes (Afile, &ppreorder, &pInOrder, &t Ree_nodes_total); if (ret! = 0 | | tree_nodes_total = 0) {fprintf (stderr, "error:failure to get tree nodes info from file (% s) \ n ", afile); goto end;} PROOT1 = Reconstruct_btree_by_preinorder (Ppreorder, Pinorder, tree_nodes_total); if (pRoot1 = = NULL) {fprintf (stderr, " Error:failure to rebuild Btree from file (%s) \ n ", afile); ret = Rebuild_btree_error;goto end;}  Free (ppreorder);pP reorder = Null;free (pinorder);p inorder = Null;print_btree (PROOT1); ret = Get_traverse_nodes (bfile, &ppreorder, &pinorder, &tree_nodes_total); if (ret! = 0 | | tree_nodes_total = = 0) {fprintf (stderr, "Error:fail Ure to get tree nodes info from file (%s) \ n ", bfile); goto end;} PRoot2 = Reconstruct_btree_by_preinorder (Ppreorder, Pinorder, tree_nodes_total); if (PRoot2 = = NULL) {fprintf (stderr, " ErrOr:failure to rebuild Btree from file (%s) \ n ", bfile); ret = Rebuild_btree_error;goto end;}  Print_btree (PROOT2); #if 1ret = Check_is_include (pRoot1, PRoot2); if (ret! = 0) {fprintf (stderr, "Error:failure to find B Btree in a btree\n "); goto end;} #endifprintf ("note:success to find B btree in a btree\n"); End:if (Ppreorder! = null) {free (ppreorder);pP reorder = NULL;} if (Pinorder! = null) {free (pinorder);p inorder = null;} if (PROOT1) Release_btree (PROOT1), if (PRoot2) Release_btree (PROOT2); return ret;}


Code runs display:

Binary Tree A:

1

2 3

4 5 6 7

8


Binary Tree B:

3

6 7


stored in ABTree.txt and BBTree.txt, respectively.

ABTree.txt:



BBTree.txt:



Run:


./a.out ABTree.txt BBTree.txt

can find


./a.out ABTree.txt ABTree.txt

I can't find them.






Binary tree (----) Whether a binary tree contains another binary tree

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.