Question
can someone fix my codes and get it to work please. I need to delete all Even numbers in BST using recursive. I am having
can someone fix my codes and get it to work please. I need to delete all Even numbers in BST using recursive. I am having a segmentation fault on the highlighted line below. I am not sure if the rest of the codes are correct since I was not able to run it. THANKS!
//THE IMPLEMENTATION FILE: (bst.cpp)
int table::removeEven() { if(!root) return 0; else { return scan_nodes(root); } }
int table::scan_nodes(node * root) { if(root != NULL) { scan_nodes(root->left); scan_nodes(root->right); if(root->data %2 == 0) { node * temp; removeEven(root, temp); } } return 0; }
int table::removeEven(node *& root, node *& temp) { if(root->left == NULL && root->right == NULL) { delete root; root = NULL; return 0; } //second case : one child. else if(root->left == NULL) { temp = root; root = root->right; delete temp; return 0; }
else if(root->right == NULL) { temp = root; root = root->left; delete temp; return 0; } //third case : three children. else { temp = root->right; inorder_successor(root, temp); } return 0; }
//This function will take the lowest data of the left leaf of the right //side of the tree and replace root with that data.
int table::inorder_successor(node *& root, node *& temp) { if(temp == NULL) { root->data = temp->data; //**********************segmentation fault on this line ****************************** delete temp; return 0; } return inorder_successor(root, temp->left); }
//***************************************************************************************
//THIS IS THE HEADER FILE (bst.h)
#include #include #include using namespace std;
struct node { int data; node * left; node * right; };
class table { public: //These functions are supplied already table(); ~table(); void build(); void display();
/* ************** PLACE YOUR PROTOTYPE HERE ***************** */
int removeEven(); int inorder_successor(node *& root, node *& temp); int scan_nodes(node * root); int removeEven(node *& root, node *& temp);
private:
node * root;
};
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started