Question: I need help with this function add. Especially with setting all the fields appropriately. The language is C and it is a Binary Search Tree

I need help with this function "add". Especially with setting all the fields appropriately. The language is C and it is a Binary Search Tree with nodes. //////////////////////////////////////////////////////// Challenge.h //////////////////////////////////////////////////////// #ifndef CHALLENGE_H #define CHALLENGE_H

struct node { char *string; struct node *left; struct node *right; int count; };

#define NUM_NODES 1000

void add(struct node **proot, char *str); void reset();

#endif //////////////////////////////////////////////////////// Challenge.c //////////////////////////////////////////////////////// #include #include #include

#include "challenge.h"

//1. Allocate items sequentially from a fixed size array. //2. Link the items together into a Binary Search Tree over strings read from an input file, counting the number of occurrences of each string.

// As needed, get new nodes from this array. struct node nodes[NUM_NODES];

// Track the number allocated so you know the next entry of // the_nodes that is available, and can check for trying to // allocate more than NUM_NODES nodes.

int num_allocated = 0;

//////////////////////////////////////////////////////////////////////// void add(struct node **proot, char *str) { // Fill this function in // Don't forget, proot is a _pointer to_ the pointer to the BST root. // This is so that when a new subtree is needed, you can set *proot. // Note that, to access the count field, for example, you need // to write (*proot)->count, etc. if (*proot == NULL) { // Insert code here to allocate a new bst_node struct from the array. // If no more space is available, you should print "Out of space! " // and call exit(1); If you _can_ get a node, fill in its fields and // set root (what proot points to!) to point to it. Don't forget to // copy str using strdup(). //////////////////////////////////////////////////////////////////////// // Note that you will need to assign to *proot the _address_ of the // array element you are allocating, and fill in that element. You // should NEVER return or store the address of a local variable! //////////////////////////////////////////////////////////////////////// } }

// Used in the tests to reset the bst void reset() { num_allocated = 0; for (int i = 0; i < NUM_NODES; i++) { the_nodes[i].string = NULL; the_nodes[i].left = NULL; the_nodes[i].right = NULL; the_nodes[i].count = 0; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!