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

NOTE : Complete your activity and submit it to the Dropbox by clicking on the Dr

ID: 3539427 • Letter: N

Question


NOTE: Complete your activity and submit it to the Dropbox by clicking on the Dropbox tab at the top of the course frame and choosing the correct Weekly Activity.

If needed, click on the Help button above for more information on Course Tools/Using the Dropbox.


Directions
The file must be called week7Prog.cpp
Main Method
  1. Create Binary Search Tree that will accept integers in this order: 35, 18, 48, 72, 60, 25
  2. Refer to page 537 Example 10-8 for example code.
  3. Ask user for input and search tree utilizing integer input.
  4. Return %u201CTrue%u201D if integer is found or %u201CFalse%u201D if number is not found in tree.
  5. Methods utilized are up to your discretion.
  6. See example below for desired output.
  7. Include: system("PAUSE"); after your output to pause the screen.



Upload your page to the Dropbox.

NOTE: Complete your activity and submit it to the Dropbox by clicking on the Dropbox tab at the top of the course frame and choosing the correct Weekly Activity.

If needed, click on the Help button above for more information on Course Tools/Using the Dropbox.




Example output of your program Enter Integer to search for: 12 False Enter Integer to search for: 60 True

Explanation / Answer

#include <iostream>

#include <stdlib.h>

using namespace std;


struct node

{

int val;

node *left,*right;

};


node * insert(node *tree,int key)

{

node * n = new node;

n->val = key;

n->left = NULL;

n->right = NULL;

if(tree==NULL)

return n;

  

int flag;

node *curr = tree;

while(1)

{

if(key<curr->val)

{

if(curr->left!=NULL)

curr=curr->left;

else

{

flag=1;

break;

}

}

else

{

if(curr->right!=NULL)

curr=curr->right;

else

{

flag=2;

break;

}

}

}

if(flag==1)

{

curr->left = n;

}

else

{

curr->right = n;

}

return tree;

}


bool search(node * tree, int key)

{

while (tree!=NULL)

{

if(tree->val == key)

return true;

else if(tree->val>key)

tree = tree->left;

else

tree = tree->right;

}

return false;

}


int main()

{


node *tree = NULL;

tree = insert(tree,35);

tree = insert(tree,18);

tree = insert(tree,48);

tree = insert(tree,72);

tree = insert(tree,60);

tree = insert(tree,25);


int num;

cout<<"Enter Integer to search for : ";

cin>>num;

if(search(tree,num))

cout<<"True"<<endl;

else

cout<<"False"<<endl;


cout<<"Enter Integer to search for : ";

cin>>num;

if(search(tree,num))

cout<<"True"<<endl;

else

cout<<"False"<<endl;

system("pause");


}