Using C, help me with this hwk! The document is below!! CAUTION! I want the prog
ID: 3858421 • Letter: U
Question
Using C, help me with this hwk!
The document is below!!
CAUTION! I want the program to print words not integers which is different from the instruction below. You may change few function names and things like typedef struct Integers_ to typedef struct Words_. Please help me!
Assignment
Use the following structure definition:
typedef struct Integers_
{
int num;
struct Integer_* leftPtr;
struct Integer_* rightPtr;
} Integer;
You will make a simple binary search tree using an input file. This tree will have pointers pointing both ways so that we can traverse the tree if we need to. Print out the tree in indorder fashion. Search the tree for a value to see if it exists.
Make sure and take advantage of the recursive functions we discussed in class and coded examples I provided!
Use the following function prototypes:
Integer* addNode(Integer*, int value);
Adds an integer to the stack. Returns the new top of the stack.
void printInOrder(Integer*);
Prints out all numbers, starting with the BOTTOM of the stack. Be careful! The pointer passed is pointing to the top of the stack.
void searchTree(Integer*, int value);
Frees all nodes in the tree. Be careful!!!
Sample Output
Select an option:
1: add a number
2: take a number off
3: print numbers
4: quit
Option:
>1
Insert your Number: 10
Select an option:
1: add a number
2: take a number off
3: print numbers
4: quit
Option:
>1
Insert your Number: 11
Select an option:
1: add a number
2: take a number off
3: print numbers
4: quit
Option:
>1
Insert your Number: 12
Select an option:
1: add a number
2: take a number off
3: print numbers
4: quit
Option:
>3
10->11->12
Select an option:
1: add a number
2: take a number off
3: print numbers
4: quit
Option:
>2
Select an option:
1: add a number
2: take a number off
3: print numbers
4: quit
Option:
>3
10->11
Select an option:
1: add a number
2: take a number off
3: print numbers
4: quit
Option:
>4
RUN SUCCESSFUL (total time: 15s)
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Words_
{
char word[20];
struct Words_* leftPtr;
struct Words_* rightPtr;
} Words;
Words* addNode(Words* root, char value[]);
void printInOrder(Words* root);
void searchTree(Words* root, char value[]);
int main()
{
Words *root = NULL;
char word[20];
int choice;
do
{
printf(" Select an option: 1: add a word 2 : take a word off 3 : print words 4 : quit Option : >");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Insert your word: ");
scanf("%s", word);
root = addNode(root, word);
break;
case 2:
searchTree(root, word);
break;
case 3:
printInOrder(root);
break;
case 4:
printf("Quit ");
break;
}
} while (choice != 4);
}
Words* addNode(Words* root, char value[])
{
if (root == NULL)
{
// code to implement root code;
root = (Words*)malloc(sizeof(Words));
strcpy(root->word, value);
root->leftPtr = NULL;
root->rightPtr = NULL;
}
else
{
if (strcmp(value, root->word) < 0)
{
root->leftPtr = addNode(root->leftPtr, value);
}
else
{
root->rightPtr = addNode(root->rightPtr, value);
}
return(root);
}
}
void printInOrder(Words* root)
{
if (root == NULL)
return;
printf("%s ", root->word);
printInOrder(root->leftPtr);
printInOrder(root->rightPtr);
}
void searchTree(Words* root, char value[])
{
Words *cur = root, *prev = NULL;
while (strcmp(value,cur->word)!=NULL)
{
prev = cur;
if (cur->leftPtr!=NULL)
cur = cur->leftPtr;
else
{
if (cur->rightPtr != NULL)
cur = cur->rightPtr;
}
}
if (prev != NULL)
prev->leftPtr = NULL;
free(cur);
}
--------------------------------------------------------------------------------------------
//output
Select an option:
1: add a word
2 : take a word off
3 : print words
4 : quit
Option :
>1
Insert your word: This
Select an option:
1: add a word
2 : take a word off
3 : print words
4 : quit
Option :
>1
Insert your word: is
Select an option:
1: add a word
2 : take a word off
3 : print words
4 : quit
Option :
>1
Insert your word: to
Select an option:
1: add a word
2 : take a word off
3 : print words
4 : quit
Option :
>1
Insert your word: test
Select an option:
1: add a word
2 : take a word off
3 : print words
4 : quit
Option :
>3
This is to test
Select an option:
1: add a word
2 : take a word off
3 : print words
4 : quit
Option :
>2
Select an option:
1: add a word
2 : take a word off
3 : print words
4 : quit
Option :
>3
This is to
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.