Question

In: Computer Science

Implement a function to find a node in a binary search tree. Using the following class...

Implement a function to find a node in a binary search tree. Using the following class and function definition:

class BTNode {
public:
   int item;
   BTNode *left;
   BTNode *right;
   BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){}
};

BTNode *root = nullptr;

BTNode* find(int i) {
     // implement
}

If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr.

#include <iostream>

class BTNode {
public:
int item;
BTNode *left;
BTNode *right;
BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){}
};

BTNode *root = nullptr;

BTNode *find(int item)
{
return nullptr;
}

int main()
{   
root = new BTNode(10, new BTNode(1), new BTNode(20));

BTNode *t1 = find(10);
if (t1)
std::cout << "Found " << t1->item << std::endl;
else
std::cout << "Should have found 10." << std::endl;

  
BTNode *t2 = find(1);
if (t1)
std::cout << "Found " << t2->item << std::endl;
else
std::cout << "Should have found 1." << std::endl;

BTNode *t3 = find(20);
if (t3)
std::cout << "Found " << t3->item << std::endl;
else
std::cout << "Should have found 20." << std::endl;

BTNode *t4 = find(100);
if (t4)
std::cout << "Should have found 20." << std::endl;   
else
std::cout << "Did not find 100." << std::endl;
  

return 0;
}

Solutions

Expert Solution

Solution

#include <iostream>

class BTNode {
public:
int item;
BTNode *left;
BTNode *right;
BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){}
};

BTNode *root = nullptr;

BTNode *find(int item)
{
BTNode *temp = root;
while(temp != nullptr)
{
if(temp->item < item)
temp = temp->right;
else if(temp->item > item)
temp = temp->left;
else
return temp;
}
return nullptr;
}

int main()
{
root = new BTNode(10, new BTNode(1), new BTNode(20));
BTNode *t1 = find(10);
if (t1)
std::cout << "Found " << t1->item << std::endl;
else
std::cout << "Should have found 10." << std::endl;


BTNode *t2 = find(1);
if (t1)
std::cout << "Found " << t2->item << std::endl;
else
std::cout << "Should have found 1." << std::endl;

BTNode *t3 = find(20);
if (t3)
std::cout << "Found " << t3->item << std::endl;
else
std::cout << "Should have found 20." << std::endl;

BTNode *t4 = find(100);
if (t4)
std::cout << "Should have found 20." << std::endl;
else
std::cout << "Did not find 100." << std::endl;

return 0;
}

Output:

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback. Your feedback is very precious to us, so don't give negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid plagiarism.
Thank you.


Related Solutions

Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
Implement a function to remove a leaf node from a binary search tree. Using the following...
Implement a function to remove a leaf node from a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode * remove_leaf(int item, bool &removed) { // implement } The remove function will return the node with the item if it's present in the tree. If the node is a leaf node and is removed by the function,...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
I was trying to implement a simple binary search tree using this given class of bst...
I was trying to implement a simple binary search tree using this given class of bst in c++ public: BST(); ~BST(); void insertKey(int newKey); bool hasKey(int searchKey); std::vector<int> inOrder(); int getHeight(); however; i am still required to use another class for the nodes as a pointer and i need to manage memory leak. in main we should ask for the numbers we need to insert in the binary search tree and also let the user end it with a letter...
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
Binary Search Trees with Lazy Deletion Implement binary search tree class with lazy deletion that has...
Binary Search Trees with Lazy Deletion Implement binary search tree class with lazy deletion that has TreeNode as nested class in Java. Design the class, TreeNode to have following class variables: int key; // All Keys are in the range 1 to 99 TreeNode leftChild; TreeNode rightChild; boolean deleted; Your program method must have routines to do the following operations. 1. insert //Should insert a new element to a leaf node. If new element is aduplicatethen do nothing. If the...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary Tree Node structure class Node: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None class BSTree(): def __init__(self, rootdata): self.root = Node(rootdata)    def insert(self, data, cur_node): if data < cur_node.data: if cur_node.left == None: cur_node.left = Node(data) else: self.insert(data, cur_node.left)    elif data > cur_node.data: if cur_node.right == None: cur_node.right = Node(data) else:...
C++ Build a binary tree using a binary tree class member function from the following array...
C++ Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
Implement a function that returns all the items in a binary search tree in order inside...
Implement a function that returns all the items in a binary search tree in order inside a std::vector. Use the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; std::vector<int> inorder_traversal(BTNode *node) { // implement } If the BST has no values, return a vector with no items in it. #include <iostream> #include <vector> class BTNode { public: int item; BTNode *left; BTNode...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT