Question

Binary Search Tree with multiple structs? Hi, I am having an issue with trying to create...

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.

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The one missing piece was inserting into a binary search tree; we'll take care of that...
The one missing piece was inserting into a binary search tree; we'll take care of that today and write the insert function, as well as a height function. Both functions will be implemented in the "bst.h" header file, and tested using a provided main program in "main.cpp". Step one is to implement the insert function --- go ahead and modify "bst.h", adding the necessary code to (1) allocate a new node, and (b) link it into the tree. Once you...
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
How can I prove that any node of a binary search tree of n nodes can...
How can I prove that any node of a binary search tree of n nodes can be made the root in at most n − 1 rotations?
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None:...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None: """This method adds new value to the tree, maintaining BST property. Duplicates must be allowed and placed in the right subtree.""" Example #1: tree = BST() print(tree) tree.add(10) tree.add(15) tree.add(5) print(tree) tree.add(15) tree.add(15) print(tree) tree.add(5) print(tree) Output: TREE in order { } TREE in order { 5, 10, 15 } TREE in order { 5, 10, 15, 15, 15 } TREE in order {...
This assignment involves using a binary search tree (BST) to keep track of all words in...
This assignment involves using a binary search tree (BST) to keep track of all words in a text document. It produces a cross-reference, or a concordance. This is very much like assignment 4, except that you must use a different data structure. You may use some of the code you wrote for that assignment, such as input parsing, for this one. Remember that in a binary search tree, the value to the left of the root is less than the...
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of...
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of the SAX parser, I am having trouble changing my code in order to utilize the DOM parser to extract the XML data into the tree structure. Would you be able to explain what changes should be made to the code in order to use the DOM parser instead of the SAX parser? // Java Packages //      import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT