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. 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 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

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

#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 != NULL)
        {
                if (item > temp->item)
                        temp = temp->right;

                else if (item < temp->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;
}

Related Solutions

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.
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:...
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 {...
c++ Binary Search Tree: find parent of a node please insert comment to explain what the...
c++ Binary Search Tree: find parent of a node please insert comment to explain what the code do. Use struct Node{ int data; Node* left; Node* right};
Question - 1 Using the structural node and methods discussed in Binary Search Tree lectures, create...
Question - 1 Using the structural node and methods discussed in Binary Search Tree lectures, create a method for the Binary Search Tree that takes an unsorted input list and constructs a Binary Search Tree based on its values. Any duplicate value will only appear once on the tree. This method outputs a Binary Search Tree structure (not an enumeration of the tree). Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly...
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
In java, create a binary search tree (without using additional libraries) of stacks where each node...
In java, create a binary search tree (without using additional libraries) of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT