Question

C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Th

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

1. The requested code is below:-

//----------------------------------------------------------------------------------------

#include <iostream>
#include <vector>

using namespace std;

void input(vector<int> &nums)
{
   int n=0,num=0;
   cout<<"Enter Limit: ";
   cin>>n;
   for(int i=0;i<n;i++)
   {
       cout<<"Enter an Integer: ";
       cin>>num;
       nums.push_back(num);
   }
}

void display(vector<int> nums)
{
   for(int x: nums)
   {
       cout<<x<<" ";
   }
   cout<<endl;
}

void insertAfter(vector<int> &nums,int firstValue,int secondValue)
{
   int i=0;
   for(int num: nums)
   {
       i++;
       if(num==firstValue)
           break;
   }
   nums.insert(nums.begin()+i,secondValue);
}

// BST node
struct Node
{
int data;
struct Node* left;
struct Node* right;
};

typedef struct Node Node;

// Utility function to create a new Node
Node* createNode(int data)
{
Node* node = (struct Node*) malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
  
return node;
}

Node* insert(Node* node,int data)
{
   if(node == NULL)
       return createNode(data);
   if (data <= node->data)
       node->left = insert(node->left, data);
   else if (data > node->data)
       node->right = insert(node->right, data);
   return node;
}
  
// Function to perform inorder traversal
void inorder(Node* root)
{
if (root == NULL)
       return;
  
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
  
// Function to check if two BSTs
// are identical
int isIdentical(Node* root1, Node* root2)
{
// Check if both the trees are empty
if (root1 == NULL && root2 == NULL)
return 1;
// If any one of the tree is non-empty
// and other is empty, return false
else if (root1 != NULL && root2 == NULL)
return 0;
else if (root1 == NULL && root2 != NULL)
return 0;
else
   { // Check if current data of both trees equal
// and recursively check for left and right subtrees
if (root1->data == root2->data && isIdentical(root1->left, root2->left) && isIdentical(root1->right, root2->right))
return 1;
else
return 0;
}
}

Node* buildTree(Node *root,vector<int> nums)
{
   for(int x: nums)
   {
       root=insert(root,x);
   }
   return root;
}

int sum(Node*root)
{
   if (root == NULL)
return 0;
else
       return (root->data + sum(root->left) + sum(root->right));
}

int compareTree(Node* root1, Node* root2)
{
   if(isIdentical(root1,root2))
   { // if both trees are same return zero
       return 0;
   }
   else
   {
       int sum1=sum(root1); // sum of first tree
       int sum2=sum(root2); // sum of second tree
       if(sum1>sum2)
       {
           return 1;
       }
       else if(sum1<sum2)
       {
           return -1;
       }
       else // sum1 == sum2
       {
           return 2;
       }
   }
}

int main(int argc,char**argv)
{
   int cmp=0;
   Node *root1=NULL;
   Node *root2=NULL;
   // Part 1 :-
   vector<int> nums(0);
   input(nums);
   insertAfter(nums,6,5);
   cout<<"Vector of Integers:-"<<endl;
   display(nums);
   // Part 2 :-
   root1 = buildTree(root1,nums);
   cout<<"Binary Tree 1 :-"<<endl;
   inorder(root1);
   cout<<endl;
   root2 = insert(root2,9);
   root2 = insert(root2,8);
   root2 = insert(root2,7);
   root2 = insert(root2,6);
   root2 = insert(root2,5);
   cout<<"Binary Tree 2 :-"<<endl;
   inorder(root2);
   cout<<endl;
   cmp = compareTree(root1,root2);
   cout<<"Result = "<<cmp<<endl;
   return 0;
}

//----------------------------------------------------------------------------------------

2.Screenshot of the output:-

- o + 7 C:\Users\vipul\Documents\SourceCode\HighLvl\data_structures.cpp - Notepad++ File Edit Search View Encoding Language S

Add a comment
Know the answer?
Add Answer to:
C++ Vectors and Binary Search Trees • Write a program that takes from the user n...
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
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