Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Must be done in C language and also must be done in linux mint In 1872, Samuel R

ID: 3704094 • Letter: M

Question

Must be done in C language and also must be done in linux mint

In 1872, Samuel R Percy patents dried milk and in 1945, the NFL required players to wear long stockings. Wow Write a program that opens a file of integers and generates a binary tree using that data. The file format will be one integer per line. The file will be called hw9.data. As you generate the tree you must also create a linked list that POINTS to the same nodes. Your code must do a pre-, in-, and post-order traversal of the tree and a traversal of the linked list. These must be four seperate functions. Your program must also correctly free all links of the tree and list on termination. This MUST be a seperate function. You have all of the code you need in your class notes. You just need to figure out to combine them

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>

// Creates a structure for node
struct node
{
// To store data for a node
int value;
// Left and right pointer of the node
struct node *leftChild, *rightChild;
};

// Function to create a new node and return it
struct node *newNode(int data)
{
// Dynamically allocates memory to a node
struct node *tempNode = (struct node *)malloc(sizeof(struct node));
// Assigns the value part
tempNode -> value = data;
// Assigns null to left and right child
tempNode -> leftChild = tempNode -> rightChild = NULL;
// Returns the node
return tempNode;
}// End of function

// Function for in-order traversal of Binary Search Tree
void in_order(struct node *rootNode)
{
// Checks if the root node is not null
if (rootNode != NULL)
{
// Recursively calls the left child
in_order(rootNode->leftChild);
// Displays the root node value
printf("%d ", rootNode -> value);
// Recursively calls the right child
in_order(rootNode->rightChild);
}// End of if condition
}// End of function

// Function for pre-order traversal of Binary Search Tree
void pre_order(struct node *rootNode)
{
// Checks if the root node is not null
if (rootNode != NULL)
{
// Displays the root node value
printf("%d ", rootNode -> value);
// Recursively calls the left child
pre_order(rootNode->leftChild);
// Recursively calls the right child
pre_order(rootNode->rightChild);
}// End of if condition
}// End of function

// Function for post-order traversal of Binary Search Tree
void post_order(struct node *rootNode)
{
// Checks if the root node is not null
if (rootNode != NULL)
{
// Recursively calls the left child
post_order(rootNode->leftChild);
// Recursively calls the right child
post_order(rootNode->rightChild);
// Displays the root node value
printf("%d ", rootNode -> value);
}// End of if condition
}// End of function

// Function to insert a new node in Binary Search Tree
struct node* insert(struct node* rootNode, int data)
{
// Checks if the root is null then tree is empty, return a new node
if (rootNode == NULL)
return newNode(data);

// Otherwise checks the data to insert is less than the current node value
if (data < rootNode -> value)
// Recursively call the function for left child
rootNode->leftChild = insert(rootNode->leftChild, data);
// Otherwise checks the data to insert is greater than the current node value
else if (data > rootNode->value)
// Recursively call the function for right child
rootNode->rightChild = insert(rootNode->rightChild, data);

// return the node
return rootNode;
}// End of function

// Function to read file and display information
int readFile(int dataNode[])
{
// File pointer declared
FILE *fRPtr;
int c = 0;
// Opens the file for reading and checks whether it can be opened or not
if ((fRPtr = fopen("treeData.txt", "r")) == NULL)
{
// If unable to open shows error message
printf("Error! in opening file for reading");
// Program exits if the file pointer returns NULL.
exit(1);
}// End of if condition

// Loops till end of file
// Extracts a number and stores it in array
while(!feof(fRPtr))
{
fscanf(fRPtr,"%d", &dataNode[c++]);
}// End of function
// Close the file
fclose(fRPtr);
// Returns number of records
return c;
}// End of function

// Driver Program to test above functions
int main()
{
// Creates an array to store numbers read from file
int dataNode[100];
// Creates a root node and initializes it to NULL
struct node *root = NULL;
// Call the function to read the file contents and store it in array
int len = readFile(dataNode);
int c;
// Stores the first value as the root node value
root = insert(root, dataNode[0]);
// Loops from one to end of the array
for(c = 1; c < len; c++)
// Calls the function to insert array data to tree node
insert(root, dataNode[c]);

// print inoder traversal of the Binary Search Tree
printf(" In - order traversal: ");
in_order(root);

// print preorder traversal of the Binary Search Tree
printf(" Pre - order traversal: ");
pre_order(root);

// print postorder traversal of the Binary Search Tree
printf(" Post - order traversal: ");
post_order(root);

return 0;
}// End of main function

Sample Output:

In - order traversal: 20 30 40 50 60 70 80
Pre - order traversal: 50 30 20 40 70 60 80
Post - order traversal: 20 40 30 60 80 70 50

treeData.txt file contents

50
30
20
40
70
60
80
20