Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following gives me a segmentation fault. Please debug the following code. The issue should be in DeleteV, but may be in isIn or insertV.

The following gives me a segmentation fault. Please debug the following code. The issue should be in DeleteV, but may be in isIn or insertV.

-----------------------------------------

p4.cpp

-----------------------------------------

#include // Provides cout #include // Provides assert function #include // Provides math stuff #include "p4.h" // The header file using namespace std; // Allows all standard library items to be used

//***PRIVATE FUNCTIONS***// // delete and isin need fixed. int iBST::findMin(iNode *ptr){ while(ptr->left != NULL){ ptr = ptr->left; } return ptr->key; }

bool iBST::insertV(int v, iNode * &p) { bool rc; // Sets rc to bool. if (p == NULL) { // If there is no root p = new iNode(v); p->left = p->right = NULL; // Set the branches to NULL tCount++; rc = true; // Return true. }else if (v < p -> key) { rc = insertV(v, p -> left); }else if (v > p -> key) { rc = insertV(v, p -> right); } return rc; }

bool iBST::deleteV(int v, iNode *&p) { bool rc; if(v < p->key){ rc = deleteV(v, p->left); }else if(v > p->key){ rc = deleteV(v, p->right); }else if(p->left && p->right){ v = findMin(p->right); p->key; rc = deleteV(p->key, p->right); }else{ if(p->left == NULL) p = p->right; else if(p->right == NULL) p = p->left; delete p; } return p; }

bool iBST::isIn(int v, iNode *&p) { // RUNS. Needs testing. bool rc = false; if (v < p -> key) { // if v is less than what root is pointing to rc = isIn(v, p->left); // set rc to the left search }else if (v > p -> key) { // send the deletion pointer to the right rc = isIn(v, p -> right); // set rc to the right search }else if (p -> key == v){ rc = true; // nothing happens }else{ rc = false; } return rc; // returns the proper int value }

void iBST::printIt(iNode *p) { // RUNS FINE if(p != NULL) { // if p is null printIt(p -> left); // send the cursor to the left cout << p -> key << endl; // print this printIt(p -> right); // send the cursor to the right } }

void iBST::clear(iNode *p) { // RUNS if (p != NULL) { // if p exists clear(p -> left); // clear the left side of the tree clear(p -> right); // clear the right side of the tree delete p; // delete the root tCount = 0; } }

//***PUBLIC STUFF***// // Everything public runs. iBST::iBST() { // FINE. root = NULL; // Create a root. tCount = 0; }

iBST::~iBST() { // FINE. clear(); // Delete a root }

iBST::iNode::iNode(int v) { // FINE v = key; // set v variable to key inside of the iNode class left = right = NULL; // null the directional pointers }

bool iBST::insertV(int v){ // FINE return (insertV(v, root));// return recursive insert }

bool iBST::deleteV(int v) { // FINE return (deleteV(v, root));// use recursive delete to get the root }

bool iBST::isIn(int v) { // FINE return (isIn(v, root)); // calls the recursive method support }

void iBST::printIt() { // FINE. printIt(root); // print root recursively }

int iBST::count() { // FINE. return tCount; // send count }

void iBST::clear() { // FINE. clear(root); // clear recursively tCount = 0; // set tCount to 0 }

In case you need it, the following is the header file:

----------------------------------------------

p4.h

----------------------------------------------

#ifndef P4_H #define P4_H

class iBST { private: class iNode { public: iNode *left; // Creates the left side of the BST iNode *right; // Creates the right side of thr BST int key; iNode(int v); }; iNode *root; // Creates the BST by creating a parent node. int tCount; // Sets the count as usual. int findMin(iNode *ptr); /* Precondition: There must be a parent to read from. Postcondition: Finds the min value in the tree that the ptr points at. */ bool insertV(int v, iNode *&p); /* Precondition: No precondition. Postcondition: Inserts value v */ bool deleteV(int v, iNode *&p); /* Precondition: There must be content in the tree Postcondition: Deletes a given value that is found in the tree. */ bool isIn(int v, iNode *&p); /* Precondition: There must be content in the tree Postcondition: Finds a given value for delete or insert to read. */ void printIt(iNode *p); /* Precondition: There must be values available to print Postcondition: Prints whatever is being pointed to. */ void clear(iNode *p); /* Precondition: No precondition Postcondition: Clears the values found in the tree. */ public: iBST(); // Constructor /* Precondition: No precondition Postcondition: Creates a BST */ ~iBST(); // Deconstructor /* Precondition: There must be a root to delete. Postcondition: Deletes the BST */ bool insertV(int v); // Inserts v into the tree /* Precondition: The private version of this method is completed. Postcondition: Inserts value v as a node into the tree by finding where it needs to go. */ bool deleteV(int v); // Removes the node with value v. /* Precondition: There must be a value present to delete. Private version must be complete. Postcondition: Deletes a value found in the tree. */ bool isIn(int v); // returns true if the node with value v is in the tree. /* Precondition: There must be something in the tree. Private version must be complete. Postcondition: Lets the user know if a given value is in the tree. */ void printIt(); // Returns BST values being printed in ascending order. /* Precondition: There must be content to print. Private version must be complete. Postcondition: Prints the value found in the tree. */ int count(); // Returns the number of nodes in the tree /* Precondition: No precondition. Postcondition: Returns the number of nodes in the tree. */ void clear(); // Removes all nodes from the BST, making BST empty /* Precondition: Private version must be complete. Postcondition: Empties the tree. */ };

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spomenik Monument Database

Authors: Donald Niebyl, FUEL, Damon Murray, Stephen Sorrell

1st Edition

0995745536, 978-0995745537

More Books

Students also viewed these Databases questions

Question

Example. Evaluate 5n+7 lim 7-00 3n-5

Answered: 1 week ago

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago