一个二叉树是否包含另一个二叉树(2)

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 buffer 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 ] = '\0';
  fprintf(stderr, "note: current line is: %s\n", buf);
  switch( index )
  {
   case 0 :
   {
    *pPreOrder = (int *) malloc( buf_len * sizeof( int ));
    if( *pPreOrder == NULL ){
     flag = -1;
     break;
    }
    fprintf(stderr, "note: finish to get pPreOrder\n");
    flag = get_traver_data( buf, *pPreOrder, &preorder_len);
    break; 
   }
   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 is %d, inorder_len is %d\n", flag, preorder_len, inorder_len);
  if( *pPreOrder ){
   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);
   pRoot = pRoot->m_pLeft;
  }
 
  if( !st.empty()){
   pRoot = st.top();
   st.pop();
   pRoot = 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(stderr, "ERROR: in check_is_include_helper(), input param error\n");
  return INPUT_PARAM_ERROR;
 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/ca1d7dcf284ebaad32f4848705f74416.html