I got this problem in the line "my_tree = insert(my_tree, 50)". How can I fix it?

#include <iostream>
using namespace std;
template <typename T>
struct tree_node {
T key;
tree_node* left = nullptr, *right = nullptr;
tree_node(T key)
{
this->key = key;
}
};
template <typename T>
void insert(tree_node<int>*& my_tree, T key)
{
if (my_tree == nullptr) my_tree = new tree_node<T>{ key };
if (key < my_tree->key) insert(my_tree->left, key);
else insert(my_tree->right, key);
}

template <typename T>
void visit_every_member(const tree_node<T>* my_tree)
{
// This uses an inorder traversal
// 1. traverse left subtree
// 2. visit the root
// 3. traverse right subtree

if (my_tree == nullptr) return; // nothing to visit for an empty tree
visit_every_member(my_tree->left); // Step 1. Traverse left subtree
std::cout << my_tree->key << std::endl; //Step 2. Visit the root
visit_every_member(my_tree->right); // Step 3. Traverse right subtree
}
template <typename T>
T FindingPredecessorSuccessor(tree_node<T>* my_tree, tree_node<T>*& pre, tree_node<T>*& suc, T key)
{
if (my_tree != nullptr) return 0;
if (my_tree->key == key)
{
if (my_tree->left != nullptr)
{
tree_node<T>* tmp = my_tree->left;
while (tmp->right)
{
tmp = tmp->right;
}
pre = tmp;
}
if (my_tree->right != nullptr)
{
tree_node<T>* tmp = my_tree->right;
while (tmp->left)
{
tmp = tmp->left;
}
suc = tmp;
}
return 0;
}
if (my_tree->key > key)
{
suc = my_tree;
FindingPredecessorSuccessor(my_tree->left, pre, suc, key);
}
else
{
pre = my_tree;
FindingPredecessorSuccessor(my_tree->right, pre, suc, key);
}

}
int main()
{
tree_node<int>* pre = nullptr, *suc = nullptr;
tree_node<int>* my_tree = nullptr;
int key = 65;
my_tree = insert(my_tree, 50);
insert(my_tree, 50);
insert(my_tree, 30);
insert(my_tree, 20);
insert(my_tree, 40);
insert(my_tree, 70);
insert(my_tree, 60);
insert(my_tree, 80);
visit_every_member(my_tree);

FindingPredecessorSuccessor(my_tree, pre, suc, key);
if (pre != nullptr)
cout << "Predecessor is " << pre->key << endl;
else
cout << "There is no Predecessor" << endl;
if (suc != nullptr)
cout << "Successor is " << suc->key;
else
cout << "There is no Successor" << endl;

}
What is the problem? Why make us work more to figure out what your problem is?

Your insert function's return type is void. Do you understand what that means?
Last edited on
In addition to you needing to use code tags, it would be nice to know what the problem is.

Is it a compiler error?
Is it a linker error?
Is it a core dump during execution?
Do you get the wrong result during execution?

If it is compiler or linker, paste the error message you got.
If it is a core dump, say so.
If it is the wrong result, tell us what you expected and what you received.

I think I got it working with 3 changes. Edit your post to use code tags so I can refer to line numbers and guide you through the changes. Edit the post. Highlight the code and click the "<>" button to the right of the edit window.
Good day. A lot of people face this problem. In the article: https://light-it.net/blog/things-you-should-know-before-you-create-telemedicine-app/ you can see the correct code and all the answers to your questions. Initialize "left" and "right." It is necessary to write to the tree and display in a formatted format a directory of files (such as windows)
Topic archived. No new replies allowed.