Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In c++, using the following code, program a binary search tree with Person objec

ID: 3741157 • Letter: I

Question

In c++, using the following code, program a binary search tree with Person objects (name, age, height, weight). Implement the following functionality:

Insert: build by alphabetical order of names

Preorder traversal

Inorder traversal

Postorder traversal

Levelorder traversal

Delete: Deleting a value without sending the deleted value back

DeleteBack: Deleting a value and sending it back to the calling function

Minimum

Maximum

Search (search by name)

SearchBack (search by name and return the value back)

----------------------------

class person

{ //all members of the class are declared public so they can be accessed outside the class

public:

string name;

int age;

int height;

int weight;

person()

{}

person(string name1, int age1,int height1,int weight1)

{

name = name1;

age = age1;

height = height1;

weight = weight1;

}

};

int main()

{

person p[10];

p[0] = { "Barnie", 1,78,350 };

p[1] = { "Ted", 65,2,250 };

p[2] = { "Mike", 5,13,25 };

p[3] = { "Ashley", 5,5,5 };

p[4] = { "Katy", 25,25,25 };

p[5] = { "Sally", 15,15,15 };

p[6] = { "Jack", 32,32,32 };

p[7] = { "Carl", 19,19,19 };

p[8] = { "Geoff ", 10,10,12 };

p[9] = { "Zach", 27,55,128 };

Tree* tree = new Tree();

}

Explanation / Answer

1) The overloading of constructors, person(string, int, int,int), Node(int, Node*, Node*) cannot be called ever, because as basic definition of constructor states that, it will automatically be called at the time of initializing the object for the class, that is,

the consturcor person(), will imediately be called when the array of object person p[10] is defined, then again the consturctor for initializing given input values be not called that is,

p[0] = { "Barnie", 1,78,350 };

p[1] = { "Ted", 65,2,250 };

p[2] = { "Mike", 5,13,25 };

p[3] = { "Ashley", 5,5,5 };

p[4] = { "Katy", 25,25,25 };

p[5] = { "Sally", 15,15,15 };

p[6] = { "Jack", 32,32,32 };

p[7] = { "Carl", 19,19,19 };

p[8] = { "Geoff ", 10,10,12 };

p[9] = { "Zach", 27,55,128 };

above statements will leads to an error, the solution for the above is to define a member function inside the class person, say init, be like

init(string name1, int age1, int height1, int weight1)

{

name= name1;

age= age1;

height = height1;

weight = weight1;

}

and then call the above function as follows

p[0].init( "Barnie", 1,78,350 );

p[1] .init( "Ted", 65,2,250 );

p[2].init( "Mike", 5,13,25 );

p[3].init ( "Ashley", 5,5,5 );

p[4].init( "Katy", 25,25,25 );

p[5].init ( "Sally", 15,15,15 );

p[6].init( "Jack", 32,32,32 );

p[7].init ( "Carl", 19,19,19 );

p[8].init( "Geoff ", 10,10,12 );

p[9].init ( "Zach", 27,55,128 );

Edit your code as follows, to insert the above data in a Binary Search Tree (I just edited the above mentioned code to work with the person data),

class person

{ //all members of the class are declared public so they can be accessed outside the class

public:

string name;

int age;

int height;

int weight;

person()

{}

init(string name1, int age1,int height1,int weight1)

{

name = name1;

age = age1;

height = height1;

weight = weight1;

}

};

{

node=node->left;

cout<<node->data<<" ';

node=node->right;

cout<<node->data<<" ";

node=node->left;

}

int main()

{

person p[10];

p[0] = { "Barnie", 1,78,350 };

p[1] = { "Ted", 65,2,250 };

p[2] = { "Mike", 5,13,25 };

p[3] = { "Ashley", 5,5,5 };

p[4] = { "Katy", 25,25,25 };

p[5] = { "Sally", 15,15,15 };

p[6] = { "Jack", 32,32,32 };

p[7] = { "Carl", 19,19,19 };

p[8] = { "Geoff ", 10,10,12 };

p[9] = { "Zach", 27,55,128 };

int choice;

Node *nd[10];

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

nd[i].data=p[i];

Tree* tree = new Tree();

cout<<"Select from the following options: 1. Insert a node 2. Preorder Traversal 3.PostOrder Traversal 4. InorderTraversal 5.Levelorder traversal 6.Search 7.SearchBack8.Delete 9.DeleteBack 10.Minimum 11.Maximum ";

cin>>choice;

switch(choice)

{

case 1:for(int i=0;i<10;i++)

tree.Insert(root, nd[i]);

break;

case 2: tree.PrintPreOrder(root);

break;

case 3: tree.PrintPostOrder(root);

break;

case 4: tree.PrintInorder(root);

break;

case 5;tree.PrintLevelOrder(root);

break;

case 6: tree.Search(root, nd);

break;

case 7: tree.searchBack(root, nd);

break;

case 8: tree.Delete(root);

break;

case 9: tree.DeleteBack(root);

break;

case 10: tree.Minimum(root);

break;

case 11: tree.maximu(root);

break;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote