Hey guys. Okay so here is a new assignment I have...at the moment I am not sure how to go about taking a vector representing a set and converting it into a tree. Also won't I need to create a new tree for every set? Any help or hints would be great. Thanks in advance.

Here is the assignment:

Write a program library to represent set as a tree, and have the following abstract operations:

vector_to__set – takes a sequence of elements from a vector, and converts into a tree
representation (including removal of duplicate elements), and returns the start pointer of the
tree pointing to the root node.

Set_to_vector – takes a tree and returns a vector by traversing in‐order traversal
Union – takes two sets represented as trees, and performs their union in the tree form and
return the pointer of the resulting tree

Intersection – takes two sets in the form of a tree and returns their intersection and returns the
start pointer of a tree holding the intersection set.

Difference – takes two sets A and B in the form of a tree, and returns the start pointer of a tree
holding the set A – B.

Is_empty_set – checks if the set is empty. It is equivalent to check if it is a null tree
Try your program on the following set operation:

{4, 5, 9, 23} union {5, 23, 18, 16} intersection {4, 23, 99, 45, 16} - { 4, 99, 57}

Here is my code so far:

#include<iostream>
#include<vector>
using namespace std;
//This library will represent set as a tree
class Set 
	{
	private:
		struct tree_node
        {
			tree_node* left;
			tree_node* right;
			int data;
        };
        tree_node* root;
	
	public:
		// default constructor. data not initialized
		Set()
		{}
		
		void vector_to_set(vector<int>& v, int i);
		void set_to_vector();
		void union_of_set();
		void intersection();
		void difference();
		void is_empty_setr();
		
	};

//=====================================================================================================

int main()
{	
	int num, i;
	cout << "Please enter how many numbers you would likes in your vector: ";
	cin >> num;
	
	vector<int> v(num);
	
	for (i = 0; i < num; i++)
	{
		cout << "Enter the numbers you want in the vector: " << i+1 << ": ";
		cin >> v[i];
	}
	
	{
		
		cout << "Here is the vector: " << endl;
		for(i=0; i < v.size(); ++i)
		{
			cout << v[i] << " ";
		}
		cout << endl;
	}
	
	
	return 0;
 /*
 Try your program on the following set operation: 
 {4, 5, 9, 23} U {5, 23, 18, 16} U {4, 23, 99, 45, 16} - {4, 99, 57}
 */

}

//=====================================================================================================

void Set::vector_to_set(vector<int>& v, int i)
{
	int num;
	for (i = 0; i < num; i++)
	{
		
	}
}

void Set::set_to_vector()
{
}

void Set::union_of_set()
{
}

void Set::intersection()
{
}

void Set::difference()
{
}

void Set::is_empty_setr()
{
}

I guess you cannot use std::set so I think you would need to create an ordered binary tree to represent your set. My understanding is the vector does not represent the set, the data from the vector will be converted (losing all duplicates) into the set using your function.

I think you are right you should create a new (tree) for each set because the tree is how the set is impemented.

I don't think you need to pass in the number of elements as you can get this information from vector::size() but you probably don't need it at all.

This article has been dead for over six months. Start a new discussion instead.