Question: I need to implement a binary search tree in C using these methods: /** Create a proper, empty binary search tree object. * * Pre:

I need to implement a binary search tree in C using these methods:

/** Create a proper, empty binary search tree object. * * Pre: compare is the name of a user-defined function satisfying the BST specification * display is the name of a user-defined function satisfying the BST specification * destroy is the name of a user-defined function satisfying the BST specification * * Returns: a BST object with NULL root and configured to use the three user-supplied * functions for comparing, destroying and displaying user-defined data objects * stored in the tree */ BST BST_create(int32_t (*compare)(const BSTNode* const left, const BSTNode* const right), void (*display)(FILE* fp, const BSTNode* const pD), void (*destroy)(BSTNode* pNode));

/** Inserts user data object into a BST, unless it duplicates an existing object. * * Pre: pTree points to a proper BST object * userNode points to a proper BSTNode object * * Returns: true iff the insertion was performed; the implementation will not * insert a new element that is equal to one that's already in the * BST (according to the user-supplied comparison function) */ bool BST_insert(BST* const pTree, const BSTNode* const userNode);

/** Searches a proper BST for an occurence of a user data object that equals * *pData (according to the user-supplied comparison function). * * Pre: pTree points to a proper BST object * pData points to a proper BSTNode object * * Returns: pointer to matching user data object; NULL if no match is found */ BSTNode* BST_find(const BST* const pTree, const BSTNode* const userNode);

/** Deallocates all dynamic memory associated with a proper BST object. * * Pre: *pTree is a proper BST object * Post: all the user payloads and payload wrappers associated with *pTree * have been freed * the BST object itself is NOT freed since it may or may not have * been allocated dynamically; that's the responsibility of the caller * * Calls: Payload_destroy() to handle destruction of the user's data object */ void BST_destroy(BST* const pTree);

Structs used:

struct _PayloadWrapper { Payload* userdata; // pointer to a user data object BSTNode node; }; typedef struct _PayloadWrapper PayloadWrapper;

struct _Payload { char* str; }; typedef struct _Payload Payload;

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!