Как я могу исправить prblem
#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; }
Что я уже пробовал:
Я получил эту проблему в строке "my_tree = insert(my_tree, 50);" на главной функции.
Он показывает, что "тип значения 'void' не может быть присвоен какой-либо сущности типа "tree_node<int>*".
Как я могу это исправить?