C - Dereferencing pointer to incomplete type - insert string into Binary Search Tree -
i dereferencing pointer incomplete type on line 58: rootnode->_left = null. ideas?
also there lot of code commented out single out error have question format of adt: usual binary search tree structures out there have node class , bst functions such insert take , return node. here have use separate tree structure has root structure tnode. has been problematic me example in addstringtotree function returns , takes tree parameter. don't know how recurse on in usual way nodes. created helper function solution not sure ideal.
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <stdbool.h> typedef struct tnode { struct tnode* _left; struct tnode* _right; struct tnode* _key; } tnode; typedef struct tree { tnode* _root; } tree; tree* makeemptytree(); void destroytree(tree* root); tree* addstringtotree(tree* t, char* value); tnode* addstringtotreehelper(tnode* node, char* value); bool lookupintree(tree* t, char* value); void traverse(tnode* root); struct tree* wordtree; struct tnode* wordroot; int main() { if(wordtree = makeemptytree()) { printf("tree initialized.\n"); /// traverse(wordtree->_root); addstringtotree(wordtree, "peter"); //printf(wordtree->_root->_key); //traverse(wordtree->_root); } else { printf("error initializing tree.\n"); } return 0; } tree* makeemptytree() { struct tree* thetree = malloc(sizeof(struct tree*)); // allocate memory tree thetree->_root = null; return thetree; } tree* addstringtotree(tree* t, char* value) { if(t->_root == null) { struct tnode* rootnode = malloc(sizeof(struct tnode*)); rootnode->_left = null; //rootnode = (tnode*)malloc(sizeof(struct tnode)); //strcpy(rootnode->_key, value); // rootnode->_left = null; // rootnode->_right = null; //printf(rootnode->_key); } else { //addstringtotreehelper(root, value); } return t; } tnode* addstringtotreehelper(tnode* node, char* value) { // node = malloc(sizeof(tnode)); // going on if(strcmp(value, node->_key) < 0) { node->_left = addstringtotreehelper(node->_left, value); } else if(strcmp(value, node->_key) > 0) { node->_right = addstringtotreehelper(node->_right, value); } return node; } void traverse(tnode* root) { // if(root != null) { // traverse(root->_left); // printf("%s\n", root->_key); // traverse(root->_right); // } else { // printf("empty tree\n"); // } }
2nd edit
wow silly typo. guys. _key variable of tnode should of type char* instead of struct tnode* rolls eyes
struct tnode* rootnode = malloc(sizeof(struct tnode*));
should be
struct tnode* rootnode = malloc(sizeof(struct tnode));
you're allocating enough memory pointer , pointing it.
edit:
that should tnode* rootnode
not tnode* rootnode
.
Comments
Post a Comment