Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 1: Implement the Set ADT in a file Set.h as shown below. This data stucture will be implementated on the basis of Binary Search

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Exercise 1: Implement the Set ADT in a file Set.h as shown below. This data stucture will be implementated on the basis of Binary Search Trees. In fact, our Set is a Binary Search Tree // Set.h // after Mark A. Weiss, Chapter 4, Dr. Kerstin Voigt #ifndef SET-H #define SETH - #include #include #include class Set public: Set root nullptr Set) makeEmpty); const C&findMin const assert(!isEmpty)); return findMin( root )->element; const C & findMax) const assert (!isEmpty()); return findMax( root )-element; bool contains( const C& x const return contains( x, root); bool isEmpty) const return root nullptr; void printSet( const if( isEmpty)) cout element else if( t->element left); insert( x, t->right ); ; /1 Duplicate; do nothing else // Internal method to test if an item is in a subtree. // x is item to search for. // t is the node that roots the subtree. bool contains( const C& x, BinaryNode* t) const if( t - nullptr) else if( x element) else if t->element left); return contains( x, t->right ); return true; else // Match void makeEmpty( BinaryNode & t) if( t != nullptr ) makeEmpty( t-left); makeEmpty( t->right); deletet t-nullptr; void printSet( BinaryNode t) const if( t != nullptr ) printSet( t->left) cout element element; iterator & operator++ if (current = nullptr) return *this; if (current->right !- nullptr) current -current->right; while (current->left nullptr) antes.push (current); current-current->left; else if (lantes.empty()) current -antes.top); antes.pop(); else current = nullptr; return *this; iterator operator++(int) iterator old *this ++(*this) return old bool operator(const iterator & rhs) const return currentrhs.current; bool operator!-(const iterator & rhs) const return !(*this rhs); private: BinaryN ode current; stack st): currentp, antes(st) t) friend class Setleft!-nullptr) nstack.push (lmost); lmost - lmost->left; return iterator(lmost,nstack); iterator end() stack emptystack; return iterator (nullptr, emptystack); Go back to your program labe8.cpp and use iterator to print the element values in your set. Compile and run your program, and see what you get The expected result insert the values (stop when entering ): 10 5 20 3 22 6 18 7 9 13 15 4 2 1 19 30 8 8 print the values: 12-34-5 -6-7-8-9- 10-1315 - 1819 20 - 22- 30 remove the values (stop when entering ): 1 11 2 12 3 13 0 print the values: 4.5-6-7-8-9-10-15-18-19-20-22-30)- Print the element values using iterator: 4, 5, 6, 7, 8, 9, 10, 15, 18, 19, 20, 22, 30, $g++ -c Set.h $g++ c lab08.cpp Sg++ labe8.o - 1ab8 $./lab8

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions