Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**In C language ************ Copyable code: // =================== Support Code ================= // Binary Search Tree ( BST ). // // // // - Implement each

**In C language ************image text in transcribedimage text in transcribed

Copyable code:

// =================== Support Code ================= // Binary Search Tree ( BST ). // // // // - Implement each of the functions to create a working BST. // - Do not change any of the function declarations // - (i.e. bst_t* create_bst() should not have additional arguments) // - You should not have any 'printf' statements in your BST functions. // - (You may consider using these printf statements to debug, but they should be removed from your final version) // ================================================== #ifndef MYBST_H #define MYBST_H // Create a node data structure to store data within // our BST. In our case, we will stores 'integers' typedef struct node{ int data; struct node* rightChild; struct node* leftChild; }node_t; // Create a BST data structure // Our BST holds a pointer to the root node in our BST called root. typedef struct BST{ int count; // count keeps track of how many items are in the BST. node_t* root; // root points to the root node in our BST. }bst_t; // Creates a BST // Returns a pointer to a newly created BST. // The BST should be initialized with data on the heap. // The BST fields should also be initialized to default values. bst_t* create_bst(){ // Modify the body of this function as needed. bst_t* myBST= NULL; return myBST; } // BST Empty // Check if the BST is empty // Returns 1 if true (The BST is completely empty) // Returns 0 if false (the BST has at least one element enqueued) int bst_empty(bst_t* t){ return 0; } // Adds a new node containng item in the correct place of the BST. // If the data is less then the current node we go right, otherwise we go left. // Same as described in the README.md file. // Returns a -1 if the operation fails (otherwise returns 0 on success). // (i.e. the memory allocation for a new node failed). // It should run in O(log(n)) time. int bst_add(bst_t* t, int item){ return -1; // Note: you should have two return statements in this function. } // Prints the tree in ascending order if order = 0, otherwise prints in descending order. // For NULL tree it should print nothing. // It should run in O(n) time. void bst_print(bst_t*t, int order){ } // Returns the sum of all the nodes in the tree. // exits the program for a NULL tree. // It should run in O(n) time. int sum(bst_t *t){ return 0; } // Returns 1 if value is found in the tree, 0 otherwise. // For NULL tree it exists the program. // It should run in O(log(n)) time. int find(bst_t * t, int value){ return 0; } // BST Size // Queries the current size of the BST // A BST that has not been previously created will crash the program. // (i.e. A NULL BST cannot return the size) unsigned int bst_size(bst_t* t){ return 0; } // Free BST // Removes a BST and ALL of its elements from memory. // This should be called before the proram terminates. void free_bst(bst_t* t){ } #endif
Support Code 2 I/ Binary Search Tree BST) 3 II 4 11 5 II 6 II-Implement each of the functions to create a working BST 7 II-Do not change any of the function declarations 8 II-(.e bst t* create bst) should not have additional arguments) 9 II- You should not have any 'printf' statements in your BST functions. 10 II (You may consider using these printf statements to debug, but they should be removed from your final version) 12 #ifndefMYBST H 13 #define MYBST_A 14 15 IT Create a node data structure to store data within 16 /1 our BST. In our case, we will stores integers' 17 typedef struct node 18 19 20 21 node_t int data; struct nodex rightChild; struct node* leftChild; 23 I Create a BST data structure 24 11 Our BST holds a pointer to the root node in our BST called root. 25 typedef struct BST 26 27 28 bstt; 29 30 I Creates a BST 31 I Returns a pointer to a newly created BST. 32 IIThe BST should be initialized with data on the heap. 33 / The BST fields should also be initialized to default values. 34 bst_t* create_bst() 35 36 37 38 39 40 1 1/ BST Empty 42 1/ Check if the BST is empty 3I/ Returns 1 if true (The BST is completely empty) 4 I/Returns 0 if false (the BST has at least one element enqueued) 45 int bst empty(bst_t* t) 46 47 48 int count; node_t* root; // count keeps track of how many items are in the BST // root points to the root node in our BST. // Modify the body of this function as needed. bst t* myBST NULL return myBST; return 0 49 II Adds a new node containng item in the correct place of the BST. 50 I If the data is less then the current node we go right, otherwise we go left. 51 I1 Same as described in the README. md file. 52 II Returns a -1 if the operation fails (otherwise returns on success) 53 11 (i.e. the memory allocation for a new node failed) 54 I1It should run in o(log(n)) time. 55 int bst_add (bst t* t, int item) 56 57 58 59 // Prints the tree in ascending order if order = 0, otherwise prints in descending order. 60 II For NULL tree it should print nothing 61 11 It should run in 0 (n) time 62 void bst_print(bst t*t, int order) 63 64 65 66 /I Returns the sum of all the nodes in the tree. 67 // exits the program for a NULL tree. 68 I1 It should run in 0(n) time. 69 int sum(bst t *t) 70 71 72 73 II Returns 1 if value is found in the tree, 0 otherwise. 74 //For NULL tree it exists the program. 75 I1 It should run in o(log(n)) time. 76 int find (bst t* t, int value) return -1; // Note: you should have two return statements in this function return 0; return 0 78 79 80 I1 BST Size 81 I Queries the current size of the BST 82 I A BST that has not been previously created will crash the program. 83 11 (i.e. A NULL BST cannot return the size) 4 unsigned int bst size(bst t* t) 85 86 87 88 I Free BST 89 II Removes a BST and ALL of its elements from memory. 90 II This should be called before the proram terminates 91 void free_bst (bst_t* t) 92 93 94 95 96 97 #endif return 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions