0% found this document useful (0 votes)
3 views8 pages

C++ Set and Tree Operations Code

The document contains multiple C++ programs that demonstrate different data structures and algorithms. The first program computes the union of two sets of strings, the second generates all subsets of a vector of strings, and the third inserts elements into a binary search tree. Each program reads input from a file and outputs results to another file.

Uploaded by

anee.cse8.bu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

C++ Set and Tree Operations Code

The document contains multiple C++ programs that demonstrate different data structures and algorithms. The first program computes the union of two sets of strings, the second generates all subsets of a vector of strings, and the third inserts elements into a binary search tree. Each program reads input from a file and outputs results to another file.

Uploaded by

anee.cse8.bu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

#include <bits/stdc++.h>
using namespace std;
bool rightOrNot(char x);
set<string>Input();
void printSet(set<string> &SET);
set<string>U(set<string>&A,set<string>&B);
int main()
{
freopen("[Link]","r",stdin);
freopen("[Link]","w",stdout);

set<string> A =Input();
set<string> B =Input();

set<string> U_SET = U(A, B);

printSet(U_SET);

return 0;
}

bool rightOrNot(char x)
{
if (x >= '0' && x <= '9') return true;
if (x >= 'A' && x <= 'Z') return true;
if (x >= 'a' && x <= 'z') return true;
return false;
}
set<string>Input()
{
string m;
getline(cin, m);
int n = [Link]();

set<string>allElements;

for (int i = 2; i < n; i++) {


if (rightOrNot(m[i])) {
string c;
while (rightOrNot(m[i])) {
c+=m[i];
i++;
}
[Link](c);
}
}

return allElements;
}
set<string>U(set<string>&A,set<string>&B)
{
set<string>UnionSet;
for(auto &u:A)[Link](u);
for(auto &u:B)[Link](u);

return UnionSet;
}
void printSet(set<string> &SET)
{
cout << "Output=";

bool flag = true;


for (auto &v : SET) {
if(flag==true)cout<<v;
else
{
cout<<", "<<v;

flag=false;
}
cout<<"\n";

}
3.
#include <bits/stdc++.h>
using namespace std;

bool rightOrNot(char &x);


void print(vector<string> &A);
vector<string> Input();

int main()
{
freopen("[Link]","r",stdin);
freopen("[Link]","w",stdout);

vector<string> A = Input();
print(A);

return 0;
}
vector<string> Input()
{
string m;
getline(cin, m);
int n = [Link]();

vector<string> allElements;

for (int i = 0; i < n; i++) {


if (rightOrNot(m[i])) {

string c;
while (rightOrNot(m[i])) {
c+= m[i];
i++;
}
allElements.push_back(c);
}
}

return allElements;
}

bool rightOrNot(char &x)


{
if (x >= '0' && x <= '9') return true;
if (x >= 'A' && x <= 'Z') return true;
if (x >= 'a' && x <= 'z') return true;
return false;
}

void print(vector<string> &A)


{
int n = [Link]();
int totalSubSet = (1 << n);
cout << "Subset of A = ";

for (int i = 0; i < (1 << n); i++) {


cout << "{";
bool flag = true;

for (int j = 0; j < n; j++) {


int check = i & (1 << j);
if (check!=0) {
if(flag==true)cout<<A[j];
else
{
cout<<", "<<A[j];
}
flag = false;
}
}
if(i==totalSubSet-1)cout<<"}\n";
else cout<<"}, ";

}
cout << "|P(A)| = " << totalSubSet << endl;
}

5.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;

Node(int val) : data(val), left(nullptr), right(nullptr) {}


};

Node* insert(Node* root, int data) {

if (!root) {
return new Node(data);
}

if (data < root->data) {


root->left = insert(root->left, data);
}
else if (data > root->data) {
root->right = insert(root->right, data);
}

return root;
}
int main() {
freopen("[Link]","r",stdin);
freopen("[Link]","w",stdout);

Node* root = nullptr;

int items[6];
for(int i=0;i<6;i++)cin>>items[i];
int n = sizeof(items) / sizeof(items[0]);

for (int i = 0; i < n; i++) {


root = insert(root, items[i]);
}

return 0;
}

You might also like