Question

C++ : Implement the ordered list type using a pointer-based binary search tree. The elements of...

C++ : Implement the ordered list type using a pointer-based binary search tree.

The elements of the class's lists are integers, but it should be easy to change this type.

The class should implement the following operations on ordered lists of its element type:

  • Initialize a list to be empty (the default constructor).
  • A destructor that deletes the dynamic memory in the tree that represents a list.
  • Re-initialize an existing list to be empty.
  • Insert a value into a list, in the appropriate position. If the value is already present, the list is unchanged.
  • Remove a value from a list. If the value is not present, the list is unchanged.
  • Return the length of a list: the number of values it contains.
  • Report whether or not a particular value is present in a list.
  • Write out the values in a list, in order, to an output stream.

OTHER REQUIREMENTS

Use a typedef statement to specify the item type in the class's list type, so that the item type can be changed easily - From an integer-based list to a character-based list.

Consider whether functions associated with the class should be member functions, friend functions, or nonmember functions.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:-

#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
typedef struct BST
{
   int data;
   struct BST *left, *right;
}node;
class Binary_Tree
{
   public:
   void insert(node *root, node *new_node)
   {
       if (new_node->data < root->data)
       {  
           if (root->left == NULL)  
               root->left = new_node;  
           else  
               insert(root->left, new_node);  
       }  
       if (new_node->data > root->data)  
       {  
           if (root->right == NULL)  
               root->right = new_node;  
           else  
               insert(root->right, new_node);  
       }
   }
   public:
   node *search(node *root, int key, node **parent)
   {
       node *temp;
       temp = root;
       while (temp != NULL)
       {
           if (temp->data == key)
           {
               cout<<"\nThe Element "<<temp->data<<"is Present"<<endl;
               return temp;
           }
           *parent = temp;
           if (temp->data > key)
               temp = temp->left;
           else
               temp = temp->right;
       }
       return NULL;
   }
   public:
   void inorder(node *temp)
   {
       if (temp != NULL)
       {
           inorder(temp->left);
           cout<<"\t"<<temp->data;
           inorder(temp->right);
       }
   }
   public:
   node *get_node()
   {
       node *temp;
       temp = (node *) malloc(sizeof(node));
       temp->left = NULL;
       temp->right = NULL;
       return temp;
   }
};
int main()
{
   Binary_Tree obj;
   int option;
   char ans = 'N';
   int key;
   node *new_node, *root, *tmp, *parent;
   node *get_node();
   root = NULL;
   cout<<"\n ** Program For Binary Search Tree **\n"<<endl;
   while(1)
   {
       cout<<"\n1.Insert The Element";
       cout<<"\n2.Search The Element";
       cout<<"\n3.Print Tree in inorder";
       cout<<"\n4.Exit";
       cout<<"\nSelect your choice :";
       cin>>option;
       switch (option)
       {
           case 1:
           do
           {
               new_node = obj.get_node();
               cout<<"\nEnter The Element: ";
               cin>>new_node->data;
               if (root == NULL)
                   root = new_node;
               else
                   obj.insert(root, new_node);
               cout<<"\nDo you Want To insert More Elements?(y/n)";
               ans = getch();
           }
           while (ans == 'y');
               break;
           case 2:
           cout<<"\nEnter Element to be searched: ";
           cin>>key;
           tmp=obj.search(root, key, &parent);
           cout<<"\nParent of node "<<tmp->data<<" is "<<parent->data<<endl;
           break;
           case 3:
           if (root == NULL)
           {
               cout<<"\nTree Is Empty"<<endl;
           }
           else
           {
               cout<<"\nThe Inorder display: ";
               obj.inorder(root);
           }
           break;
           case 4:
           exit(0);
       }
   }
}

Code Screenshots:-

Output:-

Please UPVOTE thank you...!!!

Add a comment
Know the answer?
Add Answer to:
C++ : Implement the ordered list type using a pointer-based binary search tree. The elements of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ Given a pointer to the root of a binary search tree (has left, right,...

    In C++ Given a pointer to the root of a binary search tree (has left, right, and parent pointers as well as a data section ) write a function (or functions) which will return an STL list (you should not define this class, it’s already included) with all of the values from the tree in sorted order. Your code should run in theta(N) time. for the second part,.given a pointer to the first node of a linked list, you are...

  • Programming in C++ Implement a BST (Binary Search Tree) and test your program. (Linked List based.)...

    Programming in C++ Implement a BST (Binary Search Tree) and test your program. (Linked List based.) You should test at least the three major functions. (Insert, Retrieve, and Delete).

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • A collection of nodes is arranged as a binary search tree ordered on the field INFO which contain...

    A collection of nodes is arranged as a binary search tree ordered on the field INFO which contains distinct positive integers for each of the nodes. In addition to INFO, LLINK and RLINK, each node has three other fields CLASS SUCC and PRED CLASS is an information field containing a single letter that denotes the class to which the node belongs (there being up to 26 classes). The nodes in each class are arranged as a doubly-linked circular list with...

  • I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement...

    I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement the following operations for an ordered list of integers ordered in ascending order using a doubly linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are. You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or sentinel nodes. • OrderedList ()...

  • C++ (b) Consider a binary search tree of positive integers without duplicates. Implement (write out) a...

    C++ (b) Consider a binary search tree of positive integers without duplicates. Implement (write out) a program to find the second greatest element in the tree. The Second function is a member function of class BinarySearchTree. The function returns zero if the tree is empty or has only one element, otherwise it returns the value of the second greatest element. Based on the classes provided below, write the implementation of Second in the solution box, including any helper functions (if...

  • Consider a binary search tree of positive integers without duplicates. Implement (write out) a program to...

    Consider a binary search tree of positive integers without duplicates. Implement (write out) a program to find the second greatest element in the tree. The Second function is a member function of class BinarySearchTree. The function returns zero if the tree is empty or has only one element, otherwise it returns the value of the second greatest element. Based on the classes provided below, write the implementation of Second in the solution box, including any helper functions (if any) that...

  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Using the following definition (List.h file) for a list, implement the member functions (methods) for the...

    Using the following definition (List.h file) for a list, implement the member functions (methods) for the List class and store the implementation in a List.cpp file. Use a doubly linked list to implement the list. Write the client code (the main method and other non-class methods) and put in file driver.cpp. file: List.h typedef int ElementType; class node{ ​ElementType data; ​node * next; node* prev; }; class List { public: List(); //Create an empty list bool Empty(); // return true...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT