consturcting tree from its preorder,inorder..
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: consturcting tree from its preorder,inorder..

  1. #1
    Join Date
    Jan 2009
    Posts
    156

    consturcting tree from its preorder,inorder..

    i have two arrays which represent the preorder
    and inorder traversals in two rows.

    i cant see why i get an empty tree?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    typedef struct  node node;
    struct node{
    	int key;
    	node* left;
    	node* right;
    };
    
    
     
    
    node* t(char* pre,char* in,int st_pre,int en_pre,int st_in,int en_in); 
    int main ()
    {
    	
    	char pre[13]={'a','b','c','d','e','f','g','h','i','j','k','l','\0'};
    	char in[13]={'c','b','e','d','f','a','h','g','j','i','l','k','\0'};
    	node* root=t(pre,in,0,0,0,0);
    
    	return 0;
    }
    
    node* t(char* pre,char* in,int st_pre,int en_pre,int st_in,int en_in)
    {
    	int i;
    	node* root=(node*)malloc(sizeof(node));
    	if (st_pre==en_pre)
    		return NULL;
    	 
         
    	  root->key=pre[st_pre];
    	  for(i=0;((i<(int)strlen(pre))&&(pre[st_pre]!=in[i]));i++)
    	  {
    	  }
    	  root->left=t(pre,in,st_pre+1,st_pre+i+1,st_in,i-1);
          root->right=t(pre,in,st_pre+i+1,(int)(strlen(pre)-1),i+1,(int)(strlen(pre)-1));
    	  return root;
     
    }

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: consturcting tree from its preorder,inorder..

    Code:
    	if (st_pre==en_pre)
    		return NULL;
    That's why
    Kurt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center