Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions

Question

3 How supply and demand together determine market equilibrium.

Answered: 1 week ago