Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program performs basic operations on a binary search tree. It reads a sequence of instructions from the standard input stream and outputs the results

This program performs basic operations on a binary search tree.

It reads a sequence of instructions from the standard input stream

and outputs the results to the standard output stream.

The program accepts the following instructions:

- `a value` and the `value` to the tree

- `s` remove the smallest value stored in the current tree (should have no effect

if the current tree is empty)

- `l` remove the largest value stored in the current tree (should have no effect

if the current tree is empty)

- `p` print the values stored in the current tree in order of the inorder traversal

The `main` function in `problem1.c` reads and parses the input stream and calls

appropriate functions to perform the instructions. __Your task is to implement

those functions so that the output produced is correct.__

__Example__

__Input:__

```

a park

a snow

a zoo

a car

a dinner

p

s

l

p

a crab

a rank

a herb

a rabbit

a sand

p

l

s

p

s

s

l

l

p

l

s

p

s

p

```

__Output:__

```

car dinner park snow zoo

dinner park snow

crab dinner herb park rabbit rank sand snow

dinner herb park rabbit rank sand

park rabbit

```

(notice that the last two lines of the output are blanks!).

__Deliverables:__

Implementation of the functions in `bst.c` file. You may add

declarations to the file `bst.h` (you have to upload the modified

file if you do so).

------------------------

bst.h file

#include

#include

#include

#include "bst.h"

void add ( bst_node ** root, char * word ) {

}

void inorder ( bst_node * root ) {

}

char * removeSmallest (bst_node ** root ){

return NULL;

}

char * removeLargest (bst_node ** root ){

return NULL;

}

problem1.c file

---------------------------------

#include

#include

#include

#include "bst.h"

int main() {

bst_node * root = NULL;

char * instruction = (char*) malloc(256*sizeof(char) ) ;

char * word = NULL;

while ( scanf("%s", instruction) > 0 ) {

//printf ( "read: %c ", instruction);

if ( strncmp(instruction,"a", 2) == 0 ) {

//add a value to the tree

word = (char*) malloc (256 * sizeof(char) );

scanf("%s ", word);

add(&root, word);

}

else if ( strncmp(instruction,"s", 2) == 0 ) {

//remove the smallest value in the tree

word = removeSmallest( &root);

//free memory that was allocated for the word

if (word != NULL) {

free(word);

word = NULL;

}

}

else if ( strncmp(instruction,"l", 2) == 0 ){

//remove the largest value in the tree

word = removeLargest( &root);

//free memory that was allocated for the word

if (word != NULL) {

free(word);

word = NULL;

}

}

else if ( strncmp(instruction,"p", 2) == 0 ){

//print the content of the tree in the inorder traversal

inorder ( root );

printf ( " ");

}

else {

printf ("Error: %s is not a valid instruction ", instruction);

}

}

if (word != NULL) {

free(word);

word = NULL;

}

if (instruction != NULL) {

free(instruction);

instruction = NULL;

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Android 4 Application Development

Authors: Reto Meier

3rd Edition

1118223853, 9781118223857

More Books

Students also viewed these Programming questions

Question

Name the biggest tragedy in Malabar rebellion?

Answered: 1 week ago

Question

Write a short note on khan Abdul ghafar khan ?

Answered: 1 week ago

Question

Prepare a short note on dandi March ?

Answered: 1 week ago

Question

Famous slogan in India?

Answered: 1 week ago