// Assignment N4: Template file! #include #include #include #define FLUSH stdin=freopen(NULL,"r",stdin) #define MAX_LEN 50 typedef struct node BST_node; struct node { short id; float gpa; BST_node...

1 answer below »
Please check all requirements carefully! Professor is very strict with those. It must run on Netbeans/Cygwig. Please refer to Exercise10_1 so assignment has similar structure, function and variable names.


// Assignment N4: Template file! #include #include #include #define FLUSH stdin=freopen(NULL,"r",stdin) #define MAX_LEN 50 typedef struct node BST_node; struct node { short id; float gpa; BST_node *leftChild; BST_node *rightChild; }; // You must implement all the functions below BST_node* createBST_node(BST_node* root); // function to create the BST node BST_node* createBST(); // function to create the BST void displayBST(BST_node* root); // function to display the BST void insert_node(BST_node** root, BST_node* node ); // function to insert node into the BST. void removeBST(BST_node** root); // function to remove BST void tree_traversal(BST_node*); // function used in displayBST() to traverse BST_node* searchBST(BST_node*, short); // function to return a node with specific ID void changeBST (BST_node**); // function called to change ID in a node in BST void move_node(BST_node**, short, short); // function used inside changeBST() to delete node with specific ID and then re-insert it into BST. // You can declare and implement your own function(s) if needed int main() { printf("\nCreating List of students:\n\n"); BST_node* root = createBST(); displayBST(root); if (root!=NULL) { putchar('\n'); changeBST(&root); // Note: comment it out if you don't implement it! displayBST(root); } removeBST(&root); return 0; } // function to display the BST. No need to change it! void displayBST(BST_node* root){ printf("\n------------------\n"); printf(" ID\t GPA\n"); printf("------------------\n"); if(root == NULL) { printf("List of students is empty.\n"); } else { tree_traversal(root); } } // Functions below are declared so template can compile! // Check all the function prototypes above main() function! // function to create the BST BST_node* createBST() { BST_node* root = NULL; // You must implement it! return root; } // function called to change ID in a node in BST void changeBST (BST_node** root) { // You must implement it! } // function used in displayBST() to traverse through nodes! void tree_traversal(BST_node* root) { // You must implement it. } // function to remove BST void removeBST(BST_node** root) { // You must implement it } Sheridan College Page 1 of 2 PROG20799: Assignments N4 Winter 2020 typedef struct node BST_node; struct node { short id; float gpa; BST_node *leftChild; BST_node *rightChild; }; PROG20799: Data Structures and Algorithms in C Evaluation: 10 points, 5% of your final grade. Due date: See SLATE. Late submission: 0% per day penalty, up to 5 days. In this Assignment N4 you need to create a binary search tree (BST) to store student’s info. Your goal is to collect student’s ID and GPA, display the BST and modify it if needed. Please check the demo version: https://youtu.be/r3oy6_1e-M8 You must use a BST based on ID with the following structure: Requirements: • Please download and use main.c.txt template file. You must only use this template file with function prototypes or you’ll get a zero grade. • You absolutely must use the structure mentioned above. You’ll get a zero grade if you use anything else to store student’s info. • Structures must be allocated on the HEAP. You’ll get a zero grade otherwise. • You must use fgets() to get both the id and gpa of the student (check MAX_LEN macros). • You must allocate student's name on the HEAP and use appropriate strto…() function to convert input to floating point or short value. Do NOT use scanf(), or atof() anywhere! • You must use BST based on ID. I.e. for input with id 2000, 1000 and 3000 you’ll have root with id 2000, left node with id 1000 and right node with ID 3000. • You cannot modify the main() function given to you in the template file. • You must use and implement the function prototypes given to you. • You must destroy the BST and deallocate memory at the end of the program. Please use function removeBST(..) accordingly. • Your program must work similarly to the Demo version posted on YouTube. Namely, you must ignore the incorrect input, have same error notifications, check for duplicate ids, display the BST correctly (minimum ID first and maximum ID last). • Your solution should have optimal time and space complexity with no repetitive code. You can/should add your own function(s) if needed. For instance, to avoid repetitive code you should implement get_id() function that checks the id during input. See Hints below. https://youtu.be/r3oy6_1e-M8 Sheridan College Page 2 of 2 PROG20799: Assignments N4 Winter 2020 Hints: 1. Check Exercise 10.1. It has all the functions you need. 2. Notice that when you display BST, the record with minimum ID always displayed first and the record with maximum ID always displayed last. Which of the traversal functions (preorder, postorder, inorder) can help you? 3. When you enter empty id it is assumed there are no more nodes to add to the list. 4. In the removeBST() function you need to de-allocate memory for all the nodes. To visit the nodes use same algorithm as in traversal functions (preorder, postorder, inorder). 5. Changing ID of a node can/should be done inside move_node() function. First, you’d need to detach the node from the BST and then insert it again with new ID. Check remove_node() function in Exercise 10.1. 6. To avoid repetitive code you should implement short get_id(BST_node*, int flag) function that returns ID input from console, checks for correctness (between 1000 and 9999), checks for duplicates (flag=1) and can return value -1 if ID is incorrect or -2 if ID input is empty. This function can be used in both createBST_node() and changeBST() functions. Submission: This is an individual assignment. Even partially copied code will be subject to regulations against academic integrity. Do NOT discuss your solution with anybody else. Posting this assignment and/or solution on the Internet is a violation of the academic integrity policy. • Please make sure your code is POSIX-compliant (works in NetBeans/Cygwin)! • Please save main.c as a text file with extension .txt • You must upload main.txt file to the Assignment Dropbox by the due date. • Your submission must be unique or have references. • Please self-evaluate and self-grade your code in the comments section of the Dropbox. • Only submissions that are up to 5 days late will be accepted. Grading Scheme: • See Main requirements and also Course_Introduction.pdf. Deductions will be applied if partial functionality is provided. You can get up to 70% if changeBST() not implemented (but everything else is perfect!) • You’ll get zero grade if your code doesn’t compile. Any compilation warning is a major mistake. • "Debugging code" or commented out "old code" is a minor mistake. PROG20799: Data Structures and Algorithms in C /* Exercise 10.1 * File: main.c * Author: ninez * * Created on March 26, 2020, 3:37 PM */ #include #include typedef struct node BST_node; struct node { int data; BST_node *leftChild; BST_node *rightChild; }; BST_node* create_node(); void insert_node(BST_node**, int); BST_node* search_bst(BST_node*, int); void inorder_traversal(BST_node*); void postorder_traversal(BST_node*); void preorder_traversal(BST_node*); BST_node* find_max(BST_node*); // DIY: Implement these functions: BST_node* find_min(BST_node *); void delete_node(BST_node**, int); void delete_tree(BST_node**); #define MAX_SIZE 9 int main() { int array[MAX_SIZE] = {27, 14, 35, 10, 19, 31, 42, 55, 99}; BST_node* root = NULL; int i = 0; //create and insert node for (i = 0; i < max_size;="" i++)="" {="" insert_node(&root,="" array[i]);="" }="" finding="" max="" bst_node*="" temp_node="NULL;" temp_node="find_max(root);" if="" (temp_node="" !="NULL)" {="" printf("\nmax="" element="" is="" %d.\n",="" temp_node-="">data); } else { printf("\nMax element not found!\n"); } //search node i = 31; temp_node = search_bst(root, i); if (temp_node != NULL) { printf("\n%d element found!\n", temp_node->data); } else { printf("\n%d element not found!\n", i); } //inorder traversal printf("\nInorder traversal: "); inorder_traversal(root); printf("\n"); //postorder traversal printf("\nPostorder traversal: "); postorder_traversal(root); printf("\n"); //preorder traversal printf("\nPreorder traversal: "); preorder_traversal(root); printf("\n"); return (0); } BST_node* create_node() { BST_node* bst = (BST_node*) malloc(sizeof (BST_node)); if (bst == NULL)
Answered Same DayApr 02, 2021

Answer To: // Assignment N4: Template file! #include #include #include #define FLUSH...

Aditya answered on Apr 05 2021
147 Votes
// Assignment N4: Template file!
#include
#include
#include
#define FLUSH stdin=freopen(NULL,"r",stdin)
#define MAX_LEN 50
typedef struct node BST_node;
struct node {
short
id;
float gpa;
BST_node *leftChild;
BST_node *rightChild;
};
// You must implement all the functions below
BST_node* createBST_node(); // function to create the BST node
BST_node* createBST(); // function to create the BST
void displayBST(BST_node* root); // function to display the BST
void insert_node(BST_node** root,short,float ); // function to insert node into the BST.
void removeBST(BST_node* root); // function to remove BST
void tree_traversal(BST_node*); // function used in displayBST() to traverse
BST_node* searchBST(BST_node*, short); // function to return a node with specific ID
void changeBST (BST_node**); // function called to change ID in a node in BST
void move_node(BST_node**); // function used inside changeBST() to delete node with specific ID and then re-insert it into BST.
int getId(BST_node**, short);
void delete_node(BST_node* , short);
struct BST_node* deleteNode(BST_node*, short );
struct BST_node * minValueNode(BST_node*);
// You can declare and implement your own function(s) if needed
int main() {
printf("\nCreating List of students:\n\n");
char C_id[MAX_LEN],C_GPA[MAX_LEN];
short id;
float gpa;
BST_node* root = createBST();
while(1){
printf("Enter the Student Id(4 Digits) :\n");
fgets(C_id,MAX_LEN,stdin);
id = strtof(C_id,NULL);
if(id == NULL)
{
displayBST(root);
break;
}
else if((id>=1000)&&(id<=9999))
{
BST_node* temp_node = NULL;
temp_node = searchBST(root, id);
if (temp_node != NULL) {
printf("\n Student Id Already Exists\n");
} else {
while(1){
printf("Enter the Student GPA :\n");
fgets(C_GPA,MAX_LEN,stdin);
gpa = strtof(C_GPA,NULL);
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here