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

  • C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use...

    C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use nullptr instead #include <cstddef> #include <iostream> #include "node.hpp" template<class T> class BST{ public: // Constructor for the BST class, creates an empty tree BST(void); // Destructor for the BST class, destroys the tree ~BST(void); // Inserts data into the tree // param: The data to be inserted into the tree void insert(T); // Removes data from the tree // param: The data to be...

  • Language: C++ ○ For this question I task you with creating an Iterative Search function that...

    Language: C++ ○ For this question I task you with creating an Iterative Search function that works with the existing binary search tree of type string. This function will be inputted a variable of type string and will search for it within the existing and pre-declared binary tree. ○ The function appears just above the main, and is simply a placeholder. It can be altered as you wish. ○ Within the Main I have a simple program to prompt for...

  • 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 method that determines the key of the successor of the root node in a...

    Write a method that determines the key of the successor of the root node in a binary search tree. For any input binary search tree, find the key of successor of the root node.Note: Successor is the node with the next highest key,  should work for any binary search tree - not just the given example input. #include <iostream> using namespace std; class Node { private: int key; string val; Node* left; Node* right; friend class BinarySearchTree; }; class BinarySearchTree {...

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

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