Question
please help on the c programming assignment! Instructions are: 3 files are provided to you 1) binaryTree.h 2) binaryTree.c 3) pa6.c Use these files as
please help on the c programming assignment!
Instructions are:
3 files are provided to you
1) binaryTree.h
2) binaryTree.c
3) pa6.c
Use these files as a starting point.
makefile
Include an "all" target that builds an executable named pa6 (no extension).
Write separate targets for pa6, pa6.o, and binaryTree.o.
Include a "clean" target that removes all object files (all files ending with .o).
Make sure you test the clean target.
Required components of this assignment:
1. Documentation and Style
2. You must add 3 function prototypes to the .h file.
3. You must add 3 function definitions to the binaryTree.c file.
4. You must extend the body of the main function in pa6.c so that it will thoroughly test the 3 functions.
5. Meaningful messages must be displayed with all output.
6. Programs must compile. If you submit any program or makefile that contains a syntax error, the grader will enter a
score of 0 for that program or makefile.
Function 1
int min ( TreeNodePtr treePtr );
The function should find and return the minimum value in the tree. If there are no nodes in the tree, the function should return 0.
Function 2
int max ( TreeNodePtr treePtr );
The function should find and return the maximum value in the tree. If there are no nodes in the tree, the function should return 0.
Function 3
int sum ( TreeNodePtr treePtr );
The function should find and return the sum of all values in the tree. If there are no nodes in the tree, the function should return 0.
following are the code:
binaryTree.c
// Fig. 12.19: fig12_19.c // Creating and traversing a binary tree // preorder, inorder, and postorder #include
#include "binaryTree.h"
// insert node into tree void insertNode(TreeNodePtr *treePtr, int value) { // if tree is empty if (*treePtr == NULL) { *treePtr = malloc(sizeof(TreeNode));
// if memory was allocated, then assign data if (*treePtr != NULL) { (*treePtr)->data = value; (*treePtr)->leftPtr = NULL; (*treePtr)->rightPtr = NULL; } else { printf("%d not inserted. No memory available. ", value); } } else { // tree is not empty // data to insert is less than data in current node if (value < (*treePtr)->data) { insertNode(&((*treePtr)->leftPtr), value); }
// data to insert is greater than data in current node else if (value > (*treePtr)->data) { insertNode(&((*treePtr)->rightPtr), value); } else { // duplicate data value ignored printf("%s", "dup"); } } }
// begin inorder traversal of tree void inOrder(TreeNodePtr treePtr) { // if tree is not empty, then traverse if (treePtr != NULL) { inOrder(treePtr->leftPtr); printf("%3d", treePtr->data); inOrder(treePtr->rightPtr); } }
// begin preorder traversal of tree void preOrder(TreeNodePtr treePtr) { // if tree is not empty, then traverse if (treePtr != NULL) { printf("%3d", treePtr->data); preOrder(treePtr->leftPtr); preOrder(treePtr->rightPtr); } }
// begin postorder traversal of tree void postOrder(TreeNodePtr treePtr) { // if tree is not empty, then traverse if (treePtr != NULL) { postOrder(treePtr->leftPtr); postOrder(treePtr->rightPtr); printf("%3d", treePtr->data); } }
binaryTree.h
// struct definition and prototypes for binary tree functions // #include
#ifndef BINARY_TREE #define BINARY_TREE
// node structure definition
struct treeNode { struct treeNode *leftPtr; // pointer to left subtree int data; // node value struct treeNode *rightPtr; // pointer to right subtree };
// typedef statements to make it easier to declare node and tree // variables
typedef struct treeNode TreeNode; // synonym for struct treeNode typedef TreeNode *TreeNodePtr; // synonym for TreeNode*
// function prototypes void insertNode(TreeNodePtr *treePtr, int value); void inOrder(TreeNodePtr treePtr); void preOrder(TreeNodePtr treePtr); void postOrder(TreeNodePtr treePtr);
#endif
pa6.c
// Fig. 12.19: fig12_19.c // Creating and traversing a binary tree // preorder, inorder, and postorder #include
#include "binaryTree.h"
// function main begins program execution int main(void) { TreeNodePtr rootPtr = NULL; // tree initially empty
srand(time(NULL)); puts("The numbers being placed in the tree are:");
// insert random values between 0 and 14 in the tree for (unsigned int i = 1; i <= 10; ++i) { int item = rand() % 15; printf("%3d", item); insertNode(&rootPtr, item); }
// traverse the tree preOrder puts(" The preOrder traversal is:"); preOrder(rootPtr);
// traverse the tree inOrder puts(" The inOrder traversal is:"); inOrder(rootPtr);
// traverse the tree postOrder puts(" The postOrder traversal is:"); postOrder(rootPtr); printf(" "); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started