Question

Language: C++ ○ For this question I task you with creating an Iterative Search function that...

Language: C++

○ For this question I task you with creating an Iterative Search function that works with the existing binary search tree of type string. This function will be inputted a variable of type string and will search for it within the existing and pre-declared binary tree.

○ The function appears just above the main, and is simply a placeholder. It can be altered as you wish.

○ Within the Main I have a simple program to prompt for a string that will then be run through the iterativeSearch function to return true or false, depending on whether or not the string was found within the binary search tree.

Below is all of the code required to answer this question and run the program! Thank you!

NOTE: It is possible that the tree may need to be changed itself, and if so, feel free to do so.

NOTE: Only the iterativeSearch function is required for this question, so feel free to only post that portion when you have answered the question.

#include

#include

using std::string;

using namespace std;

using std::ifstream;

using std::ofstream;

using std::cout;

using std::cin;

using std::endl;

class Node

{

public:

string key;

Node *left;

Node *right;

int height;

};

// A utility function to get maximum

// of two integers

int max(int a, int b);

// A utility function to get the

// height of the tree

int height(Node *N)

{

if (N == NULL)

return 0;

return N->height;

}

// A utility function to get maximum

// of two integers

int max(int a, int b)

{

return (a > b)? a : b;

}

// Helper function that allocates a

// new node with the given key and

// NULL left and right pointers.

Node* newNode(string key)

{

Node* node = new Node();

node->key = key;

node->left = NULL;

node->right = NULL;

node->height = 1; // new node is initially

// added at leaf

return(node);

}

Node *rightRotate(Node *y)

{

Node *x = y->left;

Node *T2 = x->right;

  

// Perform rotation

x->right = y;

y->left = T2;

  

// Update heights

y->height = max(height(y->left),

height(y->right)) + 1;

x->height = max(height(x->left),

height(x->right)) + 1;

  

// Return new root

return x;

}

// A utility function to left

// rotate subtree rooted with x

// See the diagram given above.

Node *leftRotate(Node *x)

{

Node *y = x->right;

Node *T2 = y->left;

  

// Perform rotation

y->left = x;

x->right = T2;

  

// Update heights

x->height = max(height(x->left),

height(x->right)) + 1;

y->height = max(height(y->left),

height(y->right)) + 1;

  

// Return new root

return y;

}

// Get Balance factor of node N

int getBalance(Node *N)

{

if (N == NULL)

return 0;

return height(N->left) - height(N->right);

}

// Recursive function to insert a key

// in the subtree rooted with node and

// returns the new root of the subtree.

Node* insert(Node* node, string key)

{

// 1. Perform the normal BST insertion

if (node == NULL)

return(newNode(key));

  

if (key < node->key)

node->left = insert(node->left, key);

else if (key > node->key)

node->right = insert(node->right, key);

else // Equal keys are not allowed in BST

return node;

  

// 2. Update height of this ancestor node

node->height = 1 + max(height(node->left),

height(node->right));

  

// 3. Get the balance factor of this ancestor

//node to check whether this node became

//unbalanced

int balance = getBalance(node);

  

// If this node becomes unbalanced, then

// there are 4 cases

  

// Left Left Case

if (balance > 1 && key < node->left->key)

return rightRotate(node);

  

// Right Right Case

if (balance < -1 && key > node->right->key)

return leftRotate(node);

// Left Right Case

if (balance > 1 && key > node->left->key)

{

node->left = leftRotate(node->left);

return rightRotate(node);

}  

// Right Left Case

if (balance < -1 && key < node->right->key)

{

node->right = rightRotate(node->right);

return leftRotate(node);

}

// return the (unchanged) node pointer

return node;

}

bool iterativeSearch(struct Node* root, string word)

{

//CODE NEEDED HERE TO FILL THIS FUNCTION

return false;

}

int main()

{

Node *root = NULL;

     

root = insert(root, "as");

root = insert(root, "app");

root = insert(root, "aaron");

root = insert(root, "bitcoin");

root = insert(root, "bacon");

root = insert(root, "carriage");

  

cout << "Enter a search term: ";

string searchTerm;

cin >> searchTerm;  

if(iterativeSearch(root,searchTerm))

{

cout << "\n'" << searchTerm << "'" << " was found within the Binary Search Tree.\n" << endl;

}

else

{

cout << "\n'" << searchTerm << "'" << " was NOT found within the Binary Search Tree.\n" << endl;

}

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>


using std::string;

using namespace std;

using std::ifstream;

using std::ofstream;

using std::cout;

using std::cin;

using std::endl;

class Node

{

public:

string key;

Node *left;

Node *right;

int height;

};

// A utility function to get maximum

// of two integers

int max(int a, int b);

// A utility function to get the

// height of the tree

int height(Node *N)

{

if (N == NULL)

return 0;

return N->height;

}

// A utility function to get maximum

// of two integers

int max(int a, int b)

{

return (a > b)? a : b;

}

// Helper function that allocates a

// new node with the given key and

// NULL left and right pointers.

Node* newNode(string key)

{

Node* node = new Node();

node->key = key;

node->left = NULL;

node->right = NULL;

node->height = 1; // new node is initially

// added at leaf

return(node);

}

Node *rightRotate(Node *y)

{

Node *x = y->left;

Node *T2 = x->right;


// Perform rotation

x->right = y;

y->left = T2;


// Update heights

y->height = max(height(y->left),

height(y->right)) + 1;

x->height = max(height(x->left),

height(x->right)) + 1;


// Return new root

return x;

}

// A utility function to left

// rotate subtree rooted with x

// See the diagram given above.

Node *leftRotate(Node *x)

{

Node *y = x->right;

Node *T2 = y->left;


// Perform rotation

y->left = x;

x->right = T2;


// Update heights

x->height = max(height(x->left),

height(x->right)) + 1;

y->height = max(height(y->left),

height(y->right)) + 1;


// Return new root

return y;

}

// Get Balance factor of node N

int getBalance(Node *N)

{

if (N == NULL)

return 0;

return height(N->left) - height(N->right);

}

// Recursive function to insert a key

// in the subtree rooted with node and

// returns the new root of the subtree.

Node* insert(Node* node, string key)

{

// 1. Perform the normal BST insertion

if (node == NULL)

return(newNode(key));


if (key < node->key)

node->left = insert(node->left, key);

else if (key > node->key)

node->right = insert(node->right, key);

else // Equal keys are not allowed in BST

return node;


// 2. Update height of this ancestor node

node->height = 1 + max(height(node->left),

height(node->right));


// 3. Get the balance factor of this ancestor

//node to check whether this node became

//unbalanced

int balance = getBalance(node);


// If this node becomes unbalanced, then

// there are 4 cases


// Left Left Case

if (balance > 1 && key < node->left->key)

return rightRotate(node);


// Right Right Case

if (balance < -1 && key > node->right->key)

return leftRotate(node);

// Left Right Case

if (balance > 1 && key > node->left->key)

{

node->left = leftRotate(node->left);

return rightRotate(node);

}

// Right Left Case

if (balance < -1 && key < node->right->key)

{

node->right = rightRotate(node->right);

return leftRotate(node);

}

// return the (unchanged) node pointer

return node;

}

bool iterativeSearch(struct Node* root, string word)

{

while(root != NULL){

if( word.compare(root->key) > 0)

root = root->right;

else if( word.compare(root->key) < 0)

root = root->left;

else

return true;

}

return false;

}

int main()

{

Node *root = NULL;


root = insert(root, "as");

root = insert(root, "app");

root = insert(root, "aaron");

root = insert(root, "bitcoin");

root = insert(root, "bacon");

root = insert(root, "carriage");


cout << "Enter a search term: ";

string searchTerm;

cin >> searchTerm;

if(iterativeSearch(root,searchTerm))

{

cout << "\n'" << searchTerm << "'" << " was found within the Binary Search Tree.\n" << endl;

}

else

{

cout << "\n'" << searchTerm << "'" << " was NOT found within the Binary Search Tree.\n" << endl;

}

return 0;

}

======================================

SEE OUTPUT

PLEASE COMMENT if there is any concern.

==========================================

Add a comment
Know the answer?
Add Answer to:
Language: C++ ○ For this question I task you with creating an Iterative Search function that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++: PLEASE HELP~!!! ~~~~~~~~ Implement bool AVLTree::deleteNode(string ss) method. Function deleteNode() tries to delete the node...

    C++: PLEASE HELP~!!! ~~~~~~~~ Implement bool AVLTree::deleteNode(string ss) method. Function deleteNode() tries to delete the node containing value ss. If there is no such node, it returns false. Otherwise, it deletes the node, check the balance of the tree, rebalance the tree if it is necessary. When you delete a node, consider three different scenarios: -The node is a leaf -The node has only ONE child subtree -The node has two child subtrees Important: When you implement this project, do...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • Having code issues wth my C++ program. My program checks if two binary trees are similar...

    Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...

  • In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write...

    In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

  • Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak...

    Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak if the pointer is pointing to null. if (p==NULL) {     p = new node;     p->key=x;     p->left=NULL;     p->right=NULL; } else {     //Cheak if the element to be inserted is smaller than the root.     if (x < p->key)     {       //call the function itself with new parameters.       insert(x,p->left);     }     //cheak if the alement to be inserted...

  • 3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that...

    3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that returns the height of the tree. The height of the tree is the number of levels it contains. Demonstrate the function in a driver program. CPP FILE CODE: #include "BinaryTree.h" #include <iostream> using namespace std; BinaryTree::BinaryTree() { root = NULL; } BinaryTree::~BinaryTree() { destroy(root); } bool BinaryTree::search(int data) { return search(data, root); } void BinaryTree::insert(int data) { insert(data, root); } void BinaryTree::traverseInOrder() { traverseInOrder(root);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT