Question: Data Structure in C++ In this assignment youre going to implement splay trees, and then re-use some of your code from assignment 1 to create
Data Structure in C++
In this assignment youre going to implement splay trees, and then re-use some of your code from assignment 1 to create a tree register machine, a machine with four registers whose values are search trees.
Part 1: AVL Trees
Implement AVL trees (with parent pointers).
Implement it as a binary search tree with integer keys (i.e., an ordered set). Your nodes should have type
struct node { int key; node* left; node* right; node* parent; int height; // Other members as needed by your tree implementation... }; Note that maintaining parent pointers complicates some of the algorithms! I would suggest implementing the basic, unbalanced BST operations first, and then adding the parent pointers and making sure everything still works, and finally adding the balancing code.
You must implement the following tree operations:
node*& find(node*& root, int value); // Search void insert(node*& root, int value); // Insertion void remove(node*& root, int value); // Deletion void print(std::ostream& out, node* root); // Print node* merge(node* t1, node* t2); // Tree-merge bool check(node* root); // Check tree for correctness
Be sure to correctly handle the case where root == nullptr (i.e., the empty tree)!
Depending on how you feel about references vs. pointers, you might prefer to change the *& (reference to a pointer) parameters to ** (double-pointer). A single pointer will not suffice, however.
The print function is mostly for your convenience in testing your code; you can print your trees in any format youd like, though Id suggest using an inorder traversal, as that will print the elements of the tree in numerical order (hopefully!). Likewise, the check function should return true if the tree has the correct tree structure (ordering, pointers connected correctly, etc.).
To merge two trees, you can simply insert all the nodes of one into the other, although there are more efficient ways of doing it
Part 2: Tree Register Machine
Built an interpreter for a tree register machine supporting the following commands:
| Command | Description |
|---|---|
| clear r | Reset r to contain the empty tree |
| insert v r | Insert the value v into the tree in register r |
| remove v r | Remove the value v from register r, if it exists |
| merge r1 r2 r | Merge trees in r1 and r2 into register r |
| test v r | Print TRUE if v is in rs tree, FALSE otherwise |
| print r | Print the tree in register r |
As with assignment 1, your machine has 4 registers: a, b, c, d.
Use this to test your implementation
My code for assignment 1:
#include
#include
#include
#include
using namespace std;
int main()
{
string cmd;
cin >> cmd;
int pp=0,c=0;
for(pp=0;cmd[pp]!='\0';pp++)
{
if(cmd[pp]==' ')c++;
if(c>0)
{
}
}
map
commands.insert(make_pair("stop",0));
commands.insert(make_pair("store",1));
commands.insert(make_pair("print",2));
commands.insert(make_pair("add",3));
commands.insert(make_pair("sub",4));
commands.insert(make_pair("mul",5));
commands.insert(make_pair("cmp",6));
int commandType = 0;
std::map
itr = commands.find(cmd);
if(itr!= commands.end())
int commandType = 0;
std::map
itr = commands.find(cmd);
if(itr!= commands.end())
commandType = itr->second;
else
cout << "this commands not exist" << endl;
map
reg.insert(make_pair('a',-999));
reg.insert(make_pair('b',-999));
reg.insert(make_pair('c',-999));
reg.insert(make_pair('d',-999));
while(commandType != 0)
{
std::map
// cout <<"command Type is: " < switch(commandType) { case 1: { int value; char regist; cin >> value >> regist; //modified //cout << "value :"< if(value=='a'&&value=='b'&&value=='c'&&value=='d') { cout<<"Error:invalid command entered"< return value; } if(regist!='a'&®ist!='b'&®ist!='c'&®ist!='d') { cout<<"Error:invalid command entered"< return regist; } it = reg.find(regist); it->second = value; break; } case 2: { char r; cin >> r; //modified //cout << "r :"< if(r!='a'&&r!='b'&&r!='c'&&r!='d') { cout<<"Command format worng!!!"< return 0; } it = reg.find(r); cout << it -> second << endl; break; } case 3: { char x,y; char r; cin >> x >> y >> r; //modified if(x!='a'&&x!='b'&&x!='c'&&x!='d') { cout<<"Command format worng!!!"< return x; } if(y!='a'&&y!='b'&&y!='c'&&y!='d') { cout<<"Command format worng!!!"< return y; } it = reg.find(r); it->second = (reg.find(x)->second + reg.find(y)->second); break; } case 4: { char x,y; char r; cin >> x >> y >> r; //modified if(x!='a'&&x!='b'&&x!='c'&&x!='d') { cout<<"Command format worng!!!"< return x; } if(y!='a'&&y!='b'&&y!='c'&&y!='d') { cout<<"Command format worng!!!"< return y; } it = reg.find(r); it->second = (reg.find(y)->second - reg.find(x)->second);; break; } case 5: { char x,y; char r; cin >> x >> y >> r; //modified if(x!='a'&&x!='b'&&x!='c'&&x!='d') { cout<<"Command format worng!!!"< return x; } if(y!='a'&&y!='b'&&y!='c'&&y!='d') { cout<<"Command format worng!!!"< return y; } it = reg.find(r); it->second = (reg.find(x)->second * reg.find(y)->second); break; } case 6: { char x,y; char r; cin >> x >> y >> r; //modified if(x!='a'&&x!='b'&&x!='c'&&x!='d') { cout<<"Command format worng!!!"< return x; } if(y!='a'&&y!='b'&&y!='c'&&y!='d') { cout<<"Command format worng!!!"< return y; } it = reg.find(r); int val1 = reg.find(x)->second; int val2 = reg.find(y)->second; if(val1 == val2) it->second = 0; else if(val1 < val2) it->second = -1; else it->second = 1; break; } default: { break; } } cin >> cmd; std::map itr = commands.find(cmd); if(itr!= commands.end()) commandType = itr->second; else { char x,y,z; cin >> x >> y>> z ; cout << "No command with that name exist" << endl; commandType = -1; } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
