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

write in ++ The file must be called week7Prog.cpp 1 Main Method Create Binary Se

ID: 3539650 • Letter: W

Question

write in ++

The file must be called week7Prog.cpp 1


Main Method

Create Binary Search Tree that will accept integers in this order: 35, 18, 48, 72, 60, 25

Refer to page 537 Example 10-8 for example code.

Ask user for input and search tree utilizing integer input.

Return %u201CTrue%u201D if integer is found or %u201CFalse%u201D if number is not found in tree.

Methods utilized are up to your discretion.

See example below for desired output.

Include: system("PAUSE"); after your output to pause the screen.




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");


}