Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I only need the C program(not 3.1 or 3.2) Programming language in C Binary tree implement the following instruction in C You can assume that

I only need the C program(not 3.1 or 3.2) Programming language in C Binary tree implement the following instruction in C

You can assume that the BST will never be NULL

For this assignment, you should allocate everything on the heap

2.1 struct BSTNode* bst_makeNode(char* key, struct BSTNode* left, struct BSTNode* right)

This function should use dynamic memory allocation to initialize a Node. Use the parameters (key, left, and right) to initialize the members of the struct BSTNode*. You can assume that the key will not be NULL. You should allocate the strings on the heap as well. strdup will be helpful

2.2 void bst_add(struct BST* bst, char* key)

This function should add a BSTNode* with the specified key to bst. Do nothing if a node with the same key already exists in the tree.

2.3 void bst_remove(struct BST* bst, char* key)

This function should remove the BSTNode* with the specified key from bst. Do nothing if a node with this key does not exist in the tree.

2.4 int bst_contains(struct BST* bst, char* key)

This function should search the tree for a node with the specified key. Return 1 if the node is found; otherwise, return 0.

2.5 char* bst_max(struct BST* bst)

This function should return the key of the largest node in the tree (that is, the node with the largest key). This function is already implemented (correctly) for you in bst.c, which is provided along with bst.h in the starter code.

{struct BSTNode* node = bst->root; 
 if (node) { 
 while (node->right) { 
 node = node->right; 
 } 
 return node->key; 
 } else { 
 return NULL; 
 } 
} 

2.6 char* bst_min(struct BST* bst)

This function should return the key of the smallest node in the tree (that is, the node with the smallest key).

2.7 int bst_count(struct BST* bst)

This function should just call its recursive helper, count, implemented in Assembly (see section 3). The function should return the number of nodes in the tree.

2.8 int bst_totalLength(struct BST* bst)

This function should just call its recursive helper, totalLength, implemented in Assembly (see section 3). The function should sum the lengths of all the keys in bst, and return this value (the "total length").

2.9 void bst_deleteTree(struct BST* bst)

This function deletes the whole bst tree. Think about what was dynamically allocated in the tree, and be sure to deallocate it. Make sure to deallocate the bst itself as well, which should have been dynamically allocated in your tester (or wherever the bst was created). You can use valgrind to confirm that you have no memory leaks.

here is the header(DO NOT MODIFY HEADER, its already done)

#ifndef BST_CLASS #define BST_CLASS #include "bst.h" #include #include

struct BSTNode { char* key; struct BSTNode* left; struct BSTNode* right; };

struct BST { struct BSTNode* root; };

// In C struct BSTNode* bst_makeNode(char* key, struct BSTNode* left, struct BSTNode* right); void bst_add(struct BST* bst, char* key); void bst_remove(struct BST* bst, char* key); int bst_contains(struct BST* bst, char* key); char* bst_max(struct BST* bst); char* bst_min(struct BST* bst); void bst_deleteTree(struct BST* bst);

// In assembly (helper functions for bst_count and bst_totalLength) int count(struct BSTNode* node); int totalLength(struct BSTNode* node);

// Two C functions, implemented with Assembly helpers int bst_count(struct BST* bst); int bst_totalLength(struct BST* bst);

#endif // BST_CLASS

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

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions

Question

=+When and under what circumstances are contracts renegotiated?

Answered: 1 week ago

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago