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 :
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 :
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
selfreferential 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 insertNodeTreeNodePtr treePtrint value;
void inOrderTreeNodePtr treePtr;
void preOrderTreeNodePtr treePtr;
void postOrderTreeNodePtr treePtr;
int mainvoid
TreeNodePtr rootPtr NULL; tree initially empty
char input; Step : Input buffer to hold the text
Step : Input a line of text
printfEnter a line of text: ;
fgetsinputstdin;
Step : Tokenize the input into words and insert into the BST
char token strtokinput
;
while token NULL
insertNode&rootPtr, token;
token strtokNULL
;
Step : Print the inorder, preorder, and postorder traversals
printf
Inorder traversal:
;
inOrderrootPtr;
printf
Preorder traversal:
;
preOrderrootPtr;
printf
Postorder traversal:
;
postOrderrootPtr;
return ;
Step : Complete the insertNode function to insert words into the BST
void insertNodeTreeNodePtr treePtrchar value
If the tree is empty, allocate memory for a new node
if treePtr NULL
treePtr mallocsizeofTreeNode;
if treePtr NULL
Allocate memory for the word and copy it into the node
treePtrdata mallocstrlenvalue;
strcpytreePtrdata, value;
treePtrleftPtr NULL;
treePtrrightPtr NULL;
else
printfMemory not allocated for s
value;
else
Step : Recursively insert the word in the correct position
if strcmpvaluetreePtrdata
insertNode&treePtrleftPtrvalue;
else if strcmpvaluetreePtrdata
insertNode&treePtrrightPtrvalue;
Step : Complete the inOrder function
void inOrderTreeNodePtr treePtr
if treePtr NULL
inOrdertreePtrleftPtr;
printfs treePtrdata;
inOrdertreePtrrightPtr;
Step : Complete the preOrder function
void preOrderTreeNodePtr treePtr
if treePtr NULL
printfs treePtrdata;
preOrdertreePtrleftPtr;
preOrdertreePtrrightPtr;
Step : Complete the postOrder function
void postOrderTreeNodePtr treePtr
if treePtr NULL
postOrdertreePtrleftPtr;
postOrdertreePtrrightPtr;
printfs treePtrdata;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
