Binary Search Tree with multiple structs?
Hi, I am having an issue with trying to create a binary search tree while having multiple structs. The struct code provided is provided for us.
#define CAT_NAME_LEN 25 #define APP_NAME_LEN 50 #define VERSION_LEN 10 #define UNIT_SIZE 3 struct app_info{ char category[CAT_NAME_LEN]; // name of category char app_name[APP_NAME_LEN]; // name of application char version[VERSION_LEN]; // version number float size; // size of application char units[UNIT_SIZE]; // GB or MB float price; // price in $ of the application }; struct tree{ struct app_info record; struct tree *left; struct tree *right; }; struct categories{ char category[CAT_NAME_LEN]; // name of category struct tree *root; // pointer to root of search tree for this category };
an example input would be(comments not included in input):
3 // number of categories Games // name of 1st category Medical // name of 2nd category Social Networking // name of 3rd category 2 // number of games being added Games // category of game Minecraft // game name 0.12.1 // version # 24.1 // size MB // size in mb or gb 6.99 // price Games // - same as Minecraft FIFA 16 2.0 1.25 GB 0.00
// this input does not use other 2 categories, but will eventually
this binary search tree is simultaneously built with a hash table, but I'm only working on one for now.
I have been able to save input each time to app_Info, but not able to create a tree with it. I assume my logic is incorrect when creating an initial pointer to the tree, and when inserting, each time I call an insert function, it goes to the first 'if' statement[ if (node == NULL) ] , and replaces the head node each time.
Based on the information you have provided right now, there is a logic error at your end. You have not created new tree node properly.
Imagining your code to be following the usual BST algorithm coding style,
1. Inside insertion function, if (tree==null), create a node with record as app_info and point the *left and *right to null.
2. Or you can try adding an extra struct like this every time a new node is created.
struct tree *newNode(int item){
struct tree *temp = (struct tree*)malloc(sizeof(struct tree));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
Also a few tips included:
3. Store the category pointers in a Linked List.
4. Each element in LL can point toward an individual category.
5. Also while storing the app_info struct record, do not save the category details. This will save your memory later.
Get Answers For Free
Most questions answered within 1 hours.