Question

I am trying to create a function in c++ that checks if a binary search tree...

I am trying to create a function in c++ that checks if a binary search tree has duplicate values. It should be very simple and not call any outside values like "data".

please use this as a starting point:

template<typename X>

bool tree_duplicate(tree_node<X>* root) {

//code here

return true;

}

I don't need anything else created other then a basic function to tell if the tree has duplicates, and if it doesnt return FALSE.

if it helps I have a struct created:

struct tree_node {

X val;

Tree_node* left;

Tree_node* right;

Tree_node(X v) : val(v), left(NULL), right(NULL){}

}

Please do not use any outside variables. Everything needed to test/run the code is created so dont worry about that just create the basic check function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
template<typename X>
bool tree_duplicate(tree_node<X> *root) {
    if (root == NULL) {
        return false;
    } else {
        return tree_duplicate(root->left)
               || tree_duplicate(root->right)
               || (root->left != NULL && root->val == root->left->val)
               || (root->right != NULL && root->val == root->right->val);
    }
}
Add a comment
Know the answer?
Add Answer to:
I am trying to create a function in c++ that checks if a binary search tree...
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
  • I am trying to create a function in c++ that checks if a binary search tree...

    I am trying to create a function in c++ that checks if a binary search tree has duplicate values. It should be very simple and not call any outside values like "data". please use this as a starting point: template<typename X> bool tree_duplicate(tree_node<X>* root) { //code here return true; } I don't need anything else created other then a basic function to tell if the tree has duplicates, and if it doesnt return FALSE. if it helps I have a...

  • PROMPT: Consider a binary tree (not necessarily a binary search tree) with node structure. QUESTION: Prove that findMax works by mathematical induction. struct Node int val; struct Node * left; struc...

    PROMPT: Consider a binary tree (not necessarily a binary search tree) with node structure. QUESTION: Prove that findMax works by mathematical induction. struct Node int val; struct Node * left; struct Node* right; The following function findMax returns the largest value 'val in the tree; and returns -1 if the tree is empty. You may assume that all the values 'val' in the tree are nonnegative. struct Node * findMax(struct Node root) if (rootNULL) return -1; maxval = root->val; maxval...

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

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

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

  • Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing...

    Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...

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

  • Create a binary search tree in C++, where it will have an add_node(int i) function which...

    Create a binary search tree in C++, where it will have an add_node(int i) function which when called in the main (i.e. add_node(5)) it will add it to the tree. This tree will also place the nodes added in the correct order (if the head node is 40, all the nodes that are added that are less than 40 go to the left of the tree, and all the nodes larger than 40 go to the right) Also add a...

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

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