Problems creating a tree

This is a discussion on Problems creating a tree within the C Programming forums, part of the General Programming Boards category; I am trying to create a tree that will represent an array of strings given the array of strings, the ...

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92

    Problems creating a tree

    I am trying to create a tree that will represent an array of strings given the array of strings, the starting node, and the size of the array of strings.

    Code:
    e.g calling initialize tree with {"ab","ac","ace","act","aco","bd","b"} should be
    
                                    a                                     b
                                  /   \                                   |
                                 c    b                                  d
                               /|\        
                            e  t o
    I am getting warnings complaining about assignment from incompatible pointer types, but I don't understand where I went wrong.

    Code:
    	typedef struct{
    		struct struct_node *child_next;
    		struct struct_node *child_start;
    		char data;
    		int is_end;
    	}struct_node;
    	void initialize_tree(struct_node *tree, char **text, int size){
    		void add_element(struct_node *tree, char *text, int i){
    			if(text[i] == '\0')
    				tree->is_end = 1;
    			else if(tree->data == text[i] && tree->child_start != NULL){
    				tree = tree->child_start;
    				add_element(tree, text, ++i);
    			}else if(tree->data != text[i] && tree->child_next != NULL){
    				tree = tree->child_next;
    				add_element(tree, text, ++i);
    			}else if(tree->child_next == NULL
    			|| (tree->data == text[i] && tree->child_start == NULL)){
    				tree->child_next = malloc(sizeof(struct_node));
    				tree = tree->child_next;
    				tree->child_start = tree;
    				tree->data = text[i];
    				add_element(tree, text, ++i);
    			}
    		}
    		struct struct_node *root = tree;
    		int i = 0;
    		while(i < size){
    			add_element(tree, text[i], 0);
    			tree = root;
    			i++;
    		}
    	};

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,279
    It's really confusing have a type named "struct_node", especially when mashing that into a typedef of the same name and trying to use "struct struct_node". You really should consider a better name. Just 'node' would be a big improvement.

    Your problem is that you declare child_start to be of type struct struct_node, but there is no struct by that name. You have a nameless struct typedef'ed to struct_node. Try the following:
    Code:
    typedef struct struct_node {  // that 'struct_node' is the name of your struct
        struct struct_node *child_next;
        struct struct_node *child_start;
        char data;
        int is_end;
    } struct_node;  // this one just creates a short hand for 'struct struct_node'
    I generally prefer something like:
    Code:
    typedef struct node_s node_t;  // make 'node_t' a synonym for 'struct node_s'
    struct node_s {  // declare my struct separate from the typedef, nice and clean
        node_t *child_next;
        ...
    };
    Also, don't put functions inside other functions, and drop that ; from the closing } of initialize_tree.

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 Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. Binary Search Tree, Inserting node
    By cheryl_li in forum C Programming
    Replies: 1
    Last Post: 09-13-2003, 03:53 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21