Question

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 = val;
newNode->left = newNode->right = NULL;
return newNode;
}
  
else if (val < node->data){
node->left = insert(node->left, val);
}
else if (val > node->data) {
node->right = insert(node->right, val);
}
return node;
}

bool isSimilar(struct BinarySearchTree *node, struct BinarySearchTree *node1)
{
if(node ==NULL && node1 == NULL) {
return true;
}
  
if(node!=NULL && node1!=NULL) {
if(node->data == node1->data &&
isSimilar(node->left, node1->left) && isSimilar(node->right, node1->right)) {
return true;
}
}
  
else {
return false;
}
  
}
  
  
  
  


int main() {
//Local variables
int n, n2;
int node;
std::string prompt;
  
//Intializing the root
BinarySearchTree *root = NULL;
BinarySearchTree *root2 = NULL;
  
  
start:
  
//User input
std::cout<<"Enter the amount of nodes you want to add to the first BST: ";
std::cin>>n;
  
//Intializing the root of the tree
root = insert(root, 5);
root2 = insert(root, 5);
  

//Inserting Values into the BST
for(int i=0; i<n; i++)
{
std::cout<<"Enter the "<<i+1<<" node: ";
cin>>node;
insert(root, node);
}
  
  
std::cout<<"Enter the amount of nodes you want to add to the second BST: ";
std::cin>>n2;
  
for(int i=0; i<n2; i++)
{
std::cout<<"Enter the "<<i+1<<" node: ";
cin>>node;
insert(root2, node);
}
  
  
if(isSimilar(root, root2))
{
std::cout<<"Binary trees are similar"<<std::endl;
}
  
else
{
std::cout<<"Binary trees are not similar"<<std::endl;
}
  
std::cout<<"Would you like to try again? Type Y or N: ";
std::cin>>prompt;
  
prompt = toupper(prompt[0]);
  
if(prompt == "Y")
{
goto start;
}

  
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You have done a single mistake in the above code and that is in line 70(including empty lines) where you have initialized the roots of the tree in the second initialization you have passed the root of first tree which was the reason for the wrong output.

code :

root = insert(root, 5);
root2 = insert(root, 5);

Right answer is :

root = insert(root, 5);
root2 = insert(root2, 5); //you have to pass the root of that tree only instead of passing the root of another tree

Output :

Add a comment
Know the answer?
Add Answer to:
Having code issues wth my C++ program. My program checks if two binary trees are similar...
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
  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

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

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct...

    Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct TreeNode { int val; TreeNode *left; TreeNode *right; }; The Problem Complete the printFromButtom function that accepts a BST TreeNode and returns the nodes' value from left to right, level by level from leaf to root. This function will return vector<vector int which similar to a 2-D array. Function std: reverse (myvector.begin myVector en might be helpful. Definition for a binary tree node: struct...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

  • Take the following code for a binary source tree and make it include the following operations....

    Take the following code for a binary source tree and make it include the following operations. bool replace(const Comparable & item, const Comparable & replacementItem); int getNumberOfNodes() const; (a) The replace method searches for the node that contains item in a binary search tree, if it is found, it replaces item with replacementItem. The binary tree should remain as a binary search tree after the replacement is done. Add the replace operation to the BinarySearchTree class. Test your replace using...

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

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

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

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