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

  • Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

    Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...

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

  • Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1)...

    Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....

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

  • Does not pass the testcase testDTreeAdvancedReRootchangesParent Java I'm trying to extend the implementation of a general...

    Does not pass the testcase testDTreeAdvancedReRootchangesParent Java I'm trying to extend the implementation of a general tree with new operations that change the structure of the tree. I've created 5 classes: Node.java, SimpleNode.java, Tree.java, RerootableTree.java and SimpleTree.java(which is the main java class that I need to change). The code does not pass ONE TESTCASE : testDTreeAdvancedReRootchangesParent The code passes all the other testcases except theone mentioned above. This is because in the SimpleTree.java the method "reRoot(Node newRoot)" is most likely...

  • Removing Nodes from a Binary Tree in Java This section requires you to complete the following...

    Removing Nodes from a Binary Tree in Java This section requires you to complete the following method within BinaryTree.java: public void remove(K key) { } The remove method should remove the key from the binary tree and modify the tree accordingly to maintain the Binary Search Tree Principle. If the key does not exist in the binary tree, no nodes should be removed. In case there is a non-null left child, it should take the place of the removed node....

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
Active Questions
ADVERTISEMENT