Commit 3f024881 authored by norbert.dajnowski's avatar norbert.dajnowski

Code cleaned up and comments added

parent 6074f0df
...@@ -2,101 +2,101 @@ ...@@ -2,101 +2,101 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#define MAXCHAR 1000 #define MAXCHAR 1000 //maxchar constant equal to 1000
FILE* file; FILE* file; //global variables initialized
char data[MAXCHAR]; char data[MAXCHAR];
struct node struct node //node class initialised
{ {
char record [200]; char record [200]; //record variable stores the whole string of each record from the file
char key [50]; char key [50]; //the rest of the variables store the record variable split up
char module [50]; char module [50];
char year [50]; char year [50];
char semester [50]; char semester [50];
struct node* left; struct node* left; //Creates another node and assigns it as left from the current one
struct node* right; struct node* right; //Creates another node and assigns it as right from the current one
}; };
struct node* createNode(char record [200]){ struct node* createNode(char record [200]){ //procedure to create a new node and input values into its variables
struct node* newNode = malloc(sizeof(struct node)); struct node* newNode = malloc(sizeof(struct node)); //sets space in the memory for the node using malloc
strcpy(newNode->record, record); strcpy(newNode->record, record); //copies whole record parameter into the new node
char* token = strtok(record, ","); char* token = strtok(record, ",");
strcpy(newNode->key, token); strcpy(newNode->key, token); //splits record and copies parts of it into the class variables
token = strtok(NULL, ","); token = strtok(NULL, ",");
strcpy(newNode->module, token); strcpy(newNode->module, token);
token = strtok(NULL, ","); token = strtok(NULL, ",");
strcpy(newNode->year, token); strcpy(newNode->year, token);
token = strtok(NULL, ","); token = strtok(NULL, ",");
strcpy(newNode->semester, token); strcpy(newNode->semester, token);
newNode->left = NULL; newNode->left = NULL; //finally sets the next left and right node to NULL.
newNode->right = NULL; newNode->right = NULL;
return newNode; return newNode;
} }
struct node* input(struct node* root, char record [200]) struct node* input(struct node* root, char record [200]) //procedure to input a record into the tree
{ {
if (root == NULL) return createNode(record); if (root == NULL) return createNode(record); //if the tree is empty then enter into root
if (strcmp(record, root->record) < 0){ if (strcmp(record, root->record) < 0){ //compares record to existing records in the tree
root->left = input(root->left, record); root->left = input(root->left, record); //if record is smaller then recurse the procedure with the left leaf
} }
else if (strcmp(record, root->record) > 0){ else if (strcmp(record, root->record) > 0){ //compares record to existing records in the tree
root->right = input(root->right, record); root->right = input(root->right, record); //if record is smaller then recurse the procedure with the left leaf
} }
return root; return root; //procedure will recurse until an empty root is found
} }
void traverse_order(struct node* root){ void traverse_order(struct node* root){ //procedure to read the tree in incrementing order
if(root == NULL) return; if(root == NULL) return;
traverse_order(root->left); traverse_order(root->left); //recurses to the last left leaf where its null
printf("%s (Left: ", root->key); printf("%s (Left: ", root->key); //printing left-node-right
if (root->left != NULL) printf("%s)", root->left->key); if (root->left != NULL) printf("%s)", root->left->key); //prints out the left and right node keys, else prints null
else printf("NULL)"); else printf("NULL)");
if (root->right != NULL) printf(" (Right: %s)\n", root->right->key); if (root->right != NULL) printf(" (Right: %s)\n", root->right->key);
else printf(" (Right: NULL)\n"); else printf(" (Right: NULL)\n");
fflush(stdout); fflush(stdout);
fputs(root->record, file); fputs(root->record, file); //write the record to the file sorted
traverse_order(root->right); traverse_order(root->right); //recurses to the right node
} }
int main() { int main() { //program begins
char* filenameread = "pg03data.txt"; char* filenameread = "pg03data.txt"; //local variables (file paths, counters, tree root, file)
char* filenamewrite ="pg03sorted.txt"; char* filenamewrite ="pg03sorted.txt";
int counter = 1; int counter = 1;
struct node *root = NULL; struct node *root = NULL;
file = fopen(filenameread, "r"); file = fopen(filenameread, "r");
if (file == NULL) printf("Could not open read file %s", filenameread); if (file == NULL) printf("Could not open read file %s", filenameread);
while (fgets(data, MAXCHAR, file) != NULL){ while (fgets(data, MAXCHAR, file) != NULL){ //reading records from the file
if (counter == 1){ if (counter == 1){
root = input(root, data); root = input(root, data); //inputs root into the file
counter = counter + 1; counter = counter + 1;
}else input(root,data); }else input(root,data); //inputs the rest of the records as normal
} }
fclose(file); fclose(file); //read file closes
file= fopen(filenamewrite, "w"); file= fopen(filenamewrite, "w"); //opens file to write to
if (file == NULL) printf("Could not open file to write to %s", filenamewrite); if (file == NULL) printf("Could not open file to write to %s", filenamewrite);
traverse_order(root); traverse_order(root); //starts the read through the binary tree
fclose(file); fclose(file);
return 0; return 0; //end of program
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment