Thread: constarcting tree from its preorder,inorder..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    constarcting 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 run access memory
    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[12]={'a','b','c','d','e','f','g','h','i','j','k','l'};
    	char in[12]={'c','b','e','d','f','a','h','g','j','i','l','k'};
    	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));
    	  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
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your t function is infinitely recursive
    You do no bounds checking on your int arguments before using them as indexes.
    Your char arrays are not strings (aren't null terminated) and you should not therefore be using strlen on them.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i did that
    its still gives 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;
     
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM