You are to develop some binary tree routines that will handle single word data.
ID: 664442 • Letter: Y
Question
You are to develop some binary tree routines that will handle single word data. The binary tree is to be maintained as an ordered tree.
The routines you need are:
ADD (add a new word to tree, do not allow duplicates),
SEARCH (look up a word in the tree and indicate the word is in the structure or not)
TRAVERSE ( inorder, preorder, postorder, )
Label tree output with traversal order used to print out the tree
Use the following sequence to test your program. Execute the commands in the same order given and insert the words in the order given. The ‘add’ command can be set up with ONE word following so you would have a bunch of ‘add’ commands. Same for ‘search’ one word following.
add kindness rascal structures man forest mice manoman until rascals yahoo jack
add jammers kindman help freedom rock ill hope free boy rocknroll kelvin jimbo
add bubba rockback kindhearted mankind rooster manup kinny
inorder
preorder
search kindness kindman kin rocknroll
search rascal rascals mankind
add kindness kindwoman kinny rockey mom
inorder
postorder
preorder
You are to develop some binary tree routines that will handle single word data. The binary tree is to be maintained as an ordered tree.
The routines you need are:
ADD (add a new word to tree, do not allow duplicates),
SEARCH (look up a word in the tree and indicate the word is in the structure or not)
TRAVERSE ( inorder, preorder, postorder, )
Label tree output with traversal order used to print out the tree
Use the following sequence to test your program. Execute the commands in the same order given and insert the words in the order given. The ‘add’ command can be set up with ONE word following so you would have a bunch of ‘add’ commands. Same for ‘search’ one word following.
add kindness rascal structures man forest mice manoman until rascals yahoo jack
add jammers kindman help freedom rock ill hope free boy rocknroll kelvin jimbo
add bubba rockback kindhearted mankind rooster manup kinny
inorder
preorder
search kindness kindman kin rocknroll
search rascal rascals mankind
add kindness kindwoman kinny rockey mom
inorder
postorder
preorder
Explanation / Answer
int insert(struct node **root char *word)
{
char c;
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
strcpy(word,temp->data);
temp->left=temp->right=NULL;
if(*root==NULL)
{
*root=temp;
return;
}
printf("where to insert l/r of root");
c=getche();
if(c=='r')
{
if((*root)->right==NULL)
{
(*root)->right=temp;
return;
}
else
{
insert(&(*root)->right,item);
}
}
else
{
if((*root)->left==NULL)
{
(*root)->left=temp;
return;
}
else
{
insert(&(*root)->left,item);
}
}
}
Void search(struct node **root, char *word)
{
if(*root==NULL)
return;
search(*root->left, word);
if(strcpy(*root->data,word)==0)
{
printf(“Element is present in binary tree”);
return;
}
Search(*root->right,word);
}
char inorder(struct node *root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%s ",root->data);
inorder(root->right);
}
}
char preorder(struct node *root)
{
if(root!=NULL)
{
printf("%s ",root->data);
preorder(root->left);
preorder(root->right);
}
}
char postorder(struct node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%s ",root->data);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.