Thread: Help with tree

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    2

    Help with tree

    So i have this code (main.c tab) and the output (untitled tab):
    GDB online Debugger | Code, Compile, Run, Debug online C, C++

    The problem is:

    I don't want a node to have a repeated child e.g. node at level 2 had a child 'd' so it can't have another child 'd'.
    for that i had a simple loop at line 38:

    Code:
    for(c = 0; c < 3; c++){
            if( mother->child[c] != NULL){
                if(choice == mother->child[c]->opt){
                    choice++;
                }
            }
        }

    But if you check the output this seems to be ignored. I don't have time to change the whole logic. I wanna know if somehow i can fix it or should I accept my fate.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,616
    Code:
    	char choice = 'a';
    	
    	if( lvl == 5 ) return;
    	
    	tmp=(Node*)malloc(sizeof(Node));
    	for(c = 0; c < 3; c++)
        tmp->child[c] = NULL;
        
        //avoid same opt siblings
        //won't work. WHY ????
        for(c = 0; c < 3; c++){
            if( mother->child[c] != NULL){
                if(choice == mother->child[c]->opt){
                    choice++;
                }
            }
        }
        if(choice > 'f'){
            free (tmp);
            return;
        }
    How are you supposed to get from 'a' to 'f' in only 3 iterations?
    You may as well bail at the first match.
    Code:
                if(choice == mother->child[c]->opt){
                    free (tmp);
                    return;
                }
    No I haven't tested it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    2
    Ohmygod I just noticed this foolishness. I just copied the for loop after each "choice" if and it worked. Of course it couldn't go from 'a' to 'f' like that. Thank you my friend.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rooted Tree (Family tree)
    By ch4 in forum C Programming
    Replies: 4
    Last Post: 10-14-2008, 04:24 PM
  2. Replies: 2
    Last Post: 08-01-2007, 01:08 PM
  3. display tree data in binary tree format
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2003, 12:47 AM
  4. Replies: 5
    Last Post: 05-23-2003, 01:11 PM
  5. b-tree (not binary tree) implementation help
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 01-02-2002, 03:30 PM

Tags for this Thread