Question

Answer the following question(s) concerning implementing recursive functions to perform operations on linked lists of nodes...

Answer the following question(s) concerning implementing recursive functions to perform operations on linked lists of nodes arranged as binary trees. For these questions, use the following struct definition for the nodes of the tree (we will not templatize the node here, so you do not have to write template functions for these questions, just assume trees of <int> values):

struct BinaryTreeNode
{  
  int item;
  BinaryTreeNode* left;
  BinaryTreeNode* right;
};

----------------------------------------------

Given a node for a Binary Tree and an (integer) item to search for, return true if the item is in the tree, and false otherwise. For this problem, the tree is not guaranteed to be arranged as a binary search tree, e.g. it is not necessarily the case that the item in the left child is smaller than a given nodes item nor that the right child is larger than the nodes item. So the nodes are still arranged as a binary tree, but they do not have the guaranteed ordering of the left and right children in relation to the parent nodes item.

So you need to search the whole tree, and return a boolean true if the item being searched for is in the tree, and false if the item is not in the tree. Your base case of course is a response of false if you are searching an empty node, otherwise you need to test your nodes item to see if you found what you are searching for, and if not, you then have to use recursion to check the left and right children.

The signature for the function you are to write is as follows:

/** search a Binary Tree 
 * Search given binary tree starting at the given node for the indicated
 * value.  The tree is not a Binary Search Tree, so we can assume that
 * left child values are less than the node value nor that right child
 * values are all greater than the node value.  This function returns
 * a boolean result of true if the search succeeds and we find the item,
 * and false if the item is not in the tree.
 *
 * @param node The node of the (sub)tree we are to begin searching from.
 * @param searchItem The (int valued) item we are to search for in the
 *   binary tree.
 *
 * @returns bool true if search is successful and searchItem is somewhere
 *   in the tree, false if the item is not in the tree./** search a Binary Tree 
 */
bool searchTree(BinaryTreeNode* node, int searchItem)
{
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The completed code is given below-

bool searchTree(BinaryTreeNode* node, int searchItem)
{
   if(node == NULL)
       return false;
  
   if(node->item == searchItem)
       return true;
  
   if(searchTree(node->left, searchItem) == true)
       return true;
   else
       return searchTree(node->right, searchItem);
}

Add a comment
Know the answer?
Add Answer to:
Answer the following question(s) concerning implementing recursive functions to perform operations on linked lists of nodes...
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
  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • Write a recursive function (C++) that searches a binary tree that is NOT ordered like a...

    Write a recursive function (C++) that searches a binary tree that is NOT ordered like a BST. In other words, there is no ordering relationship among the parent, child or sibling node values. Use the following prototype and data structure: struct node { node *left; node *right; int val; }; // First parameter: pointer to a node // Second parameter: the value to find bool searchValue(node *, int); The function searchValue() should return true if the integer value in the...

  • (true/false) All nodes in the right subtree of a node must be smaller in value than...

    (true/false) All nodes in the right subtree of a node must be smaller in value than their parent (true/false) Each node in a binary search tree may contain zero, one, or two children nodes. (true/false) It is possible to recursively process a binary search tree to print all the data in a binary search tree in sorted order. (true/false) The value of a parent must be less than all its children. (true/false) All nodes in the right subtree of a...

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

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

  • NEED HELP IN C!! Answer in C programming language. Question: Functions and .h file: Test function:...

    NEED HELP IN C!! Answer in C programming language. Question: Functions and .h file: Test function: part 1: part 2: For the assignment use the following structs for Binary Trees and Binary Search Trees. struct Binode { int value; struct Binode* left; struct BTnode* right; struct BTnode* parent; }; typedef struct Binode BTnode_t; typedef struct BST { BTnode_t* root; BST_t; Question 2 [10 points] Write a function that gets a binary tree and returns the sum of its elements. //...

  • Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has...

    Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has fields Node *headPtr, Node *tailPtr and int length, where the struct type Node has fields prev and next of type Node* along with data of type T. The prev and next pointers of each Node points to the previous and next Nodes in the list (or are respectively null in the case of the list’s head or tail node). We wish to detect "invalid"...

  • 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);...

  • Which of the following is true for a Binary Tree? Question 7 options: A binary tree...

    Which of the following is true for a Binary Tree? Question 7 options: A binary tree is a tree, since no node has more than 2 children, they are known as the left child and the right child The root node is the node without a parent A leaf node is a node without child nodes All of the above

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
ADVERTISEMENT