So, I have the below code where I created functions to insert, delete, and search numbers in a binary search tree.
My functions look one (as far as I know), but when I try to call my insert function, it tells me "Use of undeclared identifier "insert"".
I'm not sure what's going on or where I'm messing up here, but I ned it to be able to add a list of numbers (3,1,5,7,9,2), delete to of those, then search for a number.
I'm currently stuck and could use another set of eyes to help me catch what I'm missing, thanks!
#include <iostream>
#include <cstddef>
using std::cout;
using std::endl;
class Node {
public:
int value;
Node* left; // left child
Node* right; // right child
Node* p; // parent
Node(int data) {
value = data;
left = NULL;
right = NULL;
p = NULL;
}
~Node() {
}
int d() {
return value;
}
void print() {
std::cout << value << std::endl;
}
};
void insert(Node* insert_node, Node* tree_root) {
if (insert_node->d() < tree_root->d()) {
if (tree_root->left == NULL) {
tree_root->left = insert_node;
insert_node->p = tree_root;
}
else {
insert(insert_node, tree_root->left);
}
}
else {
if (tree_root->right == NULL) {
tree_root->right = insert_node;
insert_node->p = tree_root;
}
else {
insert(insert_node, tree_root->right);
}
}
}
void delete_node(int value, Node* tree_root) {
if (value < tree_root->d()) {
if (tree_root->left == NULL) {
return;
}
else {
delete_node(value, tree_root->left);
}
}
else if (value > tree_root->d()) {
if (tree_root->right == NULL) {
return;
}
else {
delete_node(value, tree_root->right);
}
}
else {
if (tree_root->left == NULL && tree_root->right == NULL) {
if (tree_root->p->left == tree_root) {
tree_root->p->left = NULL;
}
else {
tree_root->p->right = NULL;
}
delete tree_root;
}
else if (tree_root->left == NULL) {
if (tree_root->p->left == tree_root) {
tree_root->p->left = tree_root->right;
}
else {
tree_root->p->right = tree_root->right;
}
delete tree_root;
}
else if (tree_root->right == NULL) {
if (tree_root->p->left == tree_root) {
tree_root->p->left = tree_root->left;
}
else {
tree_root->p->right = tree_root->left;
}
delete tree_root;
}
else {
Node* temp = tree_root->right;
while (temp->left != NULL) {
temp = temp->left;
}
tree_root->value = temp->value;
delete_node(temp->value, tree_root->right);
}
}
}
Node* search(int value, Node* tree_root) {
if (value < tree_root->d()) {
if (tree_root->left == NULL) {
return NULL;
}
else {
return search(value, tree_root->left);
}
}
else if (value > tree_root->d()) {
if (tree_root->right == NULL) {
return NULL;
}
else {
return search(value, tree_root->right);
}
}
else {
return tree_root;
}
}
int main(int argc, const char* argv[])
{
//USE THIS TO TEST INSERT FUNCTION
insert(2); //ERROR MESSAGE HERE
insert(1); // "" "" ""
Insert(4); // "" "" ""
Insert(5); // "" "" ""
Insert(9); // "" "" ""
Insert(3); // "" "" ""
Insert(6); // "" "" ""
Insert(7); // "" "" ""
Insert(10); // "" "" ""
Insert(12); // "" "" ""
Insert(11); // "" "" ""
Insert(4); // "" "" ""
//PRINT TREE IN ORDER
PrintTree(tree_root);
return 0;