forked from ajaymohanan/Week_6_Workshop_Tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask_2.c
115 lines (102 loc) · 3.77 KB
/
Task_2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
*******************************************************************************************************
Task_2_description
*******************************************************************************************************
The main() function of the following C program initially constructs a binary search tree with each node
of type struct node and the data member in each node composed of a positive number greater than zero.
After the tree is constructed, the main() function calls a user-defined function delete_tree() that
takes the address of a pointer to the rootnode of the tree as input argument and deletes all the nodes
in the tree. However, in this case, the main() function utilizes the delete_tree() function to delete
all the nodes in the right subtree of the rootnode and it does this by calling the function with the
address of the rightPtr member of the rootnode as input argument.
For instance, if data was inserted into this binary search tree in the following order:
5 7 3 6 4 2 8 1 9
inOrder traversal of the tree after construction and upon exiting the delete_tree() function would print
the following output within the main() function:
After construction of tree: 1 2 3 4 5 6 7 8 9
Upon exiting delete_tree() function: 1 2 3 4 5
1. The definition of the delete_tree() function contains errors. You must correct these
errors and verify that the function provides the correct output.
2. Prepare the C file Task_2.c for system testing by allowing it to receive the numbers to be inserted
into the tree as a command line argument utilizing the commma character as delimiter. Once the generated
executable is called with the required command line argument, the printed output should be the result of
inOrder traversal of the tree after deletion of the right subtree of the rootnode of the tree, where
the data in the printed output is delimited using one whitespace character.
*******************************************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
struct node* leftPtr;
int data;
struct node* rightPtr;
};
void insert_node(struct node** treePtr, int data);
void inOrder(struct node* treePtr);
void delete_tree(struct node** treePtr);
int main(int argc, char *argv[]) {
char *tokenPtr = NULL;
struct node* treePtr = NULL;
// printf("Enter the value of the new data member: ");
//scanf("%d", &temp);
tokenPtr = strtok(argv[1],",");
while (tokenPtr != NULL)
{
insert_node(&treePtr, atoi(tokenPtr));
// printf("Enter the value of the new data member: ");
// scanf("%d", &temp);
tokenPtr = strtok(NULL, ",");
}
//printf("Initial version of binary tree:\n");
//inOrder(treePtr);
//printf("\n");
delete_tree(&(treePtr->rightPtr));
//printf("Modified version of binary tree:\n");
inOrder(treePtr);
//printf("\n");
}
void insert_node(struct node** treePtr, int data)
{
if (*treePtr == NULL)
{
(*treePtr) = (struct node*)malloc(sizeof(struct node));
(*treePtr)->data = data;
(*treePtr)->leftPtr = NULL;
(*treePtr)->rightPtr = NULL;
}
else
{
if (data < (*treePtr)->data)
{
insert_node(&((*treePtr)->leftPtr), data);
}
else if (data > (*treePtr)->data)
{
insert_node(&((*treePtr)->rightPtr), data);
}
}
}
void inOrder(struct node* treePtr)
{
if (treePtr != NULL)
{
inOrder(treePtr->leftPtr);
printf("%d ", treePtr->data);
inOrder(treePtr->rightPtr);
}
}
void delete_tree(struct node** treePtr)
{
// free(*treePtr);
// delete_tree(&((*treePtr)->leftPtr));
// delete_tree(&((*treePtr)->rightPtr));
if (*treePtr != NULL)
{
delete_tree(&((*treePtr)->leftPtr));
delete_tree(&((*treePtr)->rightPtr));
free(*treePtr);
(*treePtr) = NULL;
}
}