Question: python coding To make this work, you need to complete the following Step 6 : The insertNode function must insert the words into the binary

python coding
To make this work, you need to complete the following
Step 6:
The insertNode function must insert the words into the binary search tree, where words are compared using strcmp to decide the correct position in the tree.
Step 7-9:
The inorder, preorder, and postorder traversal functions must recursively print the tree's contents in the specified order.
My code is the following:
#include
#include
#include
//self-referential structure
struct treeNode {
struct treeNode *leftPtr; //pointer to left subtree
int data; //node value
struct treeNode *rightPtr; //pointer to right subtree
};
typedef struct treeNode TreeNode; //synonym for struct treeNode
typedef TreeNode *TreeNodePtr; //synonym for TreeNode *
//prototypes
void insertNode(TreeNodePtr *treePtr,int value);
void inOrder(TreeNodePtr treePtr);
void preOrder(TreeNodePtr treePtr);
void postOrder(TreeNodePtr treePtr);
int main(void){
TreeNodePtr rootPtr =NULL; //tree initially empty
char input[256]; //Step 3: Input buffer to hold the text
//Step 3: Input a line of text
printf("Enter a line of text: ");
fgets(input,256,stdin);
//Step 4: Tokenize the input into words and insert into the BST
char *token =strtok(input,"
");
while (token !=NULL){
insertNode(&rootPtr, token);
token =strtok(NULL,"
");
}
//Step 5: Print the inorder, preorder, and postorder traversals
printf("
Inorder traversal:
");
inOrder(rootPtr);
printf("
Preorder traversal:
");
preOrder(rootPtr);
printf("
Postorder traversal:
");
postOrder(rootPtr);
return 0;
}
//Step 6: Complete the insertNode function to insert words into the BST
void insertNode(TreeNodePtr *treePtr,char *value){
//If the tree is empty, allocate memory for a new node
if (*treePtr ==NULL){
*treePtr =malloc(sizeof(TreeNode));
if (*treePtr !=NULL){
//Allocate memory for the word and copy it into the node
(*treePtr)->data =malloc(strlen(value)+1);
strcpy((*treePtr)->data, value);
(*treePtr)->leftPtr =NULL;
(*treePtr)->rightPtr =NULL;
}else {
printf("Memory not allocated for %s.
",value);
}
}else {
//Step 6: Recursively insert the word in the correct position
if (strcmp(value,(*treePtr)->data)<0){
insertNode(&((*treePtr)->leftPtr),value);
}else if (strcmp(value,(*treePtr)->data)>0){
insertNode(&((*treePtr)->rightPtr),value);
}
}
}
//Step 7: Complete the inOrder function
void inOrder(TreeNodePtr treePtr){
if (treePtr !=NULL){
inOrder(treePtr->leftPtr);
printf("%s ",treePtr->data);
inOrder(treePtr->rightPtr);
}
}
//Step 8: Complete the preOrder function
void preOrder(TreeNodePtr treePtr){
if (treePtr !=NULL){
printf("%s ",treePtr->data);
preOrder(treePtr->leftPtr);
preOrder(treePtr->rightPtr);
}
}
//Step 9: Complete the postOrder function
void postOrder(TreeNodePtr treePtr){
if (treePtr !=NULL){
postOrder(treePtr->leftPtr);
postOrder(treePtr->rightPtr);
printf("%s ",treePtr->data);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!