Thread: what is the purpose of this whole code..

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

    what is the purpose of this whole code..

    this is the code as it is presented.
    Code:
    #define N 3
    typedef struct tree {
    	int value;
    	struct tree **sons;  // array of pointers 
    } tree;
    
    tree * what1(){
    	int i;
    	tree *temp = (tree *)malloc(sizeof(tree));
    if(!temp) exit(1);
    	temp->value=0;
    	temp->sons = (tree **)malloc(N * sizeof(tree *)); 
    	if(!temp->sons) exit(1);
    	for(i=0; i<N; i++)
      temp->sons[i]=NULL;
    	return temp;
    }
    
    void what2(tree *root, char *text){
    	if(!(*text)){
    		root->value++;
    		return;
    	}
    	if(!root->sons[*text-'a'])	
    root->sons[*text-'a']= what1();
    	what2(root->sons[*text-'a'],text+1);
    }
    
    void what3(tree *root){
    	int i;
    	if(!root) return;
    	printf("%d ",root->value);
    	for(i=0; i<N; i++)
    		what3(root->sons[i]);
    }
    
    void main(){
    	tree *root= what1();
    	int i;
    	char *text[]={"aac","aa","bbb","aac","aa","aabc","a"},
     stack[80]={0};
    	for(i=0; i<7; i++)
    		what2(root,text[i]);
    	what3(root);
    	putchar('\n');
    }
    i traces the for loop for i=0 and i=1
    FreeImageHosting.net Hosting Service


    what i saw is that it created some structure with root value 0
    and when the text run out the last root has value 1.
    and when we go from i=0 to i=1
    it gets the original roots and overrides completely the old structure
    and create a leak in the memory

    is there any general description for what its doing
    ??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To build a tree with the count of each letter ('a'-'c') in that position.

    You have already asked this, so why are you asking again?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Leaks... yeah, I don't see any calls to free being made.
    "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

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hk_mp5kpdw View Post
    Leaks... yeah, I don't see any calls to free being made.
    But I also do not see anywhere where it "overwrites the whole tree". It only creates new nodes when there isn't an existing node.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    "count of each letter ('a'-'c') in that position."

    i cant see where its counting ??

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    value should really be count then?

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cantsee what its counting
    i showed to for iterations
    and i cant see what its counting?

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    It's a count of the number of times a given string has been added to the trie. The string is defined by the edges leading up to the node in question. It's done in the first few lines of code in what2().

    Please, review the link I posted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM