What path doesn't return the value? - c++

I have a function to find the node in BST by its string key, using recursion.
I'm getting the warning for second function : c4715 "not all control paths return a value". I can't understand what exactly path doesn't return the value..
Here's my functions:
TreeNode* Tree::findNodeByKey(const string &str) {
if (root == NULL) {
cout << "Tree is empty, nothing found" << endl;
return nullptr;
}
else {
return findNodeByKeyHelper(root, str);
}
}
TreeNode* Tree::findNodeByKeyHelper(TreeNode *node, const string &str) {
if (node->data == str) {
cout << "node is found" << endl;
return node;
}
else if (str < node->data) {
if (node->left == nullptr) {
cout << "element was not found" << endl;
return nullptr;
}
else {
findNodeByKeyHelper(node->left, str);
}
}
else if (str > node->data) {
if (node->right == nullptr) {
cout << "element was not found" << endl;
return nullptr;
}
else {
findNodeByKeyHelper(node->right, str);
}
}
}

These paths
else if (str < node->data) {
if (node->left == nullptr) {
cout << "element was not found" << endl;
return nullptr;
}
else {
findNodeByKeyHelper(node->left, str);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
}
else if (str > node->data) {
if (node->right == nullptr) {
cout << "element was not found" << endl;
return nullptr;
}
else {
findNodeByKeyHelper(node->right, str);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
}
return nothing.
You should insert the return keyword. For example
return findNodeByKeyHelper(node->right, str);
And substitute the last else-if for else. For example
if (node->data == str) {
//...
}
else if (str < node->data) {
//...
}
else {
^^^^^^^
//...
}

Related

Binary search tree C++ need the search and display functions to work

Need a void function Display that will display the binary search tree in tree shape
Need a value return or void function Search that will search for a specific data.
Not really sure What to do from this point except trial and error and trying to read off of others code...
I need help
Help
Here is my code...
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
int data;
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
bool isEmpty() const { return root == NULL; }
void print_inorder();
void inorder(tree_node*);
void print_preorder();
void preorder(tree_node*);
void print_postorder();
void postorder(tree_node*);
void insert(int);
void remove(int);
bool Search(int, tree_node*);
void Display(tree_node*, int);
};
int main()
{
BinarySearchTree b;
int ch, tmp, tmp1;
while (1)
{
system("cls");
cout << endl << endl;
cout << " Binary Search Tree Operations " << endl;
cout << " ----------------------------- " << endl;
cout << " 1. Insertion/Creation " << endl;
cout << " 2. In-Order Traversal " << endl;
cout << " 3. Pre-Order Traversal " << endl;
cout << " 4. Post-Order Traversal " << endl;
cout << " 5. Removal " << endl;
cout << " 6. Display " << endl;
cout << " 7. Display Message From Creator " << endl;
cout << " 8. EXIT " << endl;
cout << " Enter your choice : ";
cin >> ch;
switch (ch)
{
case 1: cout << " Enter Number to be inserted : ";
cin >> tmp;
b.insert(tmp);
break;
case 2: cout << endl;
cout << " In-Order Traversal " << endl;
cout << " -------------------" << endl;
b.print_inorder();
break;
case 3: cout << endl;
cout << " Pre-Order Traversal " << endl;
cout << " -------------------" << endl;
b.print_preorder();
break;
case 4: cout << endl;
cout << " Post-Order Traversal " << endl;
cout << " --------------------" << endl;
b.print_postorder();
break;
case 5: cout << " Enter data to be deleted : ";
cin >> tmp1;
b.remove(tmp1);
break;
case 6: cout << " Enter data to be deleted : ";
b.Display(NULL, tmp);
break;
case 7: cout << " I think you are a great person. Smile be happy ";
case 8:
return 0;
}
}
}
bool BinarySearchTree::Search(int d, tree_node* curr){
if (curr == NULL) {
return 0;
}
if (curr->data == d) {
return 1;
}
if (d > curr->data) {
return Search(d, curr->right);
}
else {
return Search(d, curr->left);
}
}
// Smaller elements go left
// larger elements go right
void BinarySearchTree::insert(int d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
// is this a new tree?
if (isEmpty()) root = t;
else
{
//Note: ALL insertions are as leaf nodes
tree_node* curr;
curr = root;
// Find the Node's parent
while (curr)
{
parent = curr;
if (t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if (t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
void BinarySearchTree::remove(int d)
{
//Locate the element
bool found = false;
if (isEmpty())
{
cout << " This Tree is empty! " << endl;
return;
}
tree_node* curr;
tree_node* parent;
curr = root;
while (curr != NULL)
{
if (curr->data == d)
{
found = true;
break;
}
else
{
parent = curr;
if (d>curr->data) curr = curr->right;
else curr = curr->left;
}
}
if (!found)
{
cout << " Data not found! " << endl;
return;
}
// 3 cases :
// 1. We're removing a leaf node
// 2. We're removing a node with a single child
// 3. we're removing a node with 2 children
parent = NULL;
// Node with single child
if ((curr->left == NULL && curr->right != NULL) || (curr->left != NULL
&& curr->right == NULL))
{
if (curr->left == NULL && curr->right != NULL)
{
if (parent->left == curr)
{
parent->left = curr->right;
delete curr;
}
else
{
parent->right = curr->right;
delete curr;
}
}
else // left child present, no right child
{
if (parent->left == curr)
{
parent->left = curr->left;
delete curr;
}
else
{
parent->right = curr->left;
delete curr;
}
}
return;
}
//We're looking at a leaf node
if (curr->left == NULL && curr->right == NULL)
{
if (parent->left == curr) parent->left = NULL;
else parent->right = NULL;
delete curr;
return;
}
//Node with 2 children
// replace node with smallest value in right subtree
if (curr->left != NULL && curr->right != NULL)
{
tree_node* chkr;
chkr = curr->right;
if ((chkr->left == NULL) && (chkr->right == NULL))
{
curr = chkr;
delete chkr;
curr->right = NULL;
}
else // right child has children
{
//if the node's right child has a left child
// Move all the way down left to locate smallest element
if ((curr->right)->left != NULL)
{
tree_node* lcurr;
tree_node* lcurrp;
lcurrp = curr->right;
lcurr = (curr->right)->left;
while (lcurr->left != NULL)
{
lcurrp = lcurr;
lcurr = lcurr->left;
}
curr->data = lcurr->data;
delete lcurr;
lcurrp->left = NULL;
}
else
{
tree_node* tmp;
tmp = curr->right;
curr->data = tmp->data;
curr->right = tmp->right;
delete tmp;
}
}
return;
}
}
void BinarySearchTree::print_inorder()
{
inorder(root);
}
void BinarySearchTree::inorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) inorder(p->left);
cout << " " << p->data << " ";
if (p->right) inorder(p->right);
}
else return;
}
void BinarySearchTree::print_preorder()
{
preorder(root);
}
void BinarySearchTree::preorder(tree_node* p)
{
if (p != NULL)
{
cout << " " << p->data << " ";
if (p->left) preorder(p->left);
if (p->right) preorder(p->right);
}
else return;
}
void BinarySearchTree::print_postorder()
{
postorder(root);
}
void BinarySearchTree::postorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) postorder(p->left);
if (p->right) postorder(p->right);
cout << " " << p->data << " ";
}
else return;
}
void BinarySearchTree::Display(tree_node *curr, int indent)
{
if (curr != NULL)
{
cout << "My Tree. The left is top and right is bottom" << endl;
Display(curr->left, indent + 4);
if (indent > 0)
cout << setw(indent) << " ";
cout << curr->data << endl;
Display(curr->right, indent + 4);
}
}

C++ Segmentation fault BST

I'm currently working on a c++ implementation of a binary search tree. Everything seems to be working perfectly, but my search function is giving me issues.
BinarySearchTree::node* BinarySearchTree::SEARCH(node* x, int key)
{
if(root == NULL) {
cout << "This is an empty tree." << endl;
return NULL;
} else {
if(x->key == key) {
return x;
}
if(x == NULL) {
cout << "Value not in tree." << endl;
return x;
}
if(key < x->key ) {
return SEARCH(x->left, key);
} else {
return SEARCH(x->right, key);
}
}
}
This gives me a segmentation fault every time I search for a key value that is not in the tree, and when the node value is NULL (such as a value that would be either the max or min if it were included).
Check NULL pointer first and then the rest. If x is NULL, accessing key by x->key will give you segmentation fault.
if(x == NULL) {
cout << "Value not in tree." << endl;
return x;
}
if(x->key == key) {
return x;
}
...

Binary Search Tree Potentially Uninitiated Parent Pointer

I couldnt seem to resolve the unitialized local pointer "Parent". There isnt any other issue with this code other than the local pointer error I keep receiving Its driving me insane.
this is just me writing to post the simplest of questions
please aide me
I couldnt seem to resolve the unitialized local pointer "Parent". There isnt any other issue with this code other than the local pointer error I keep receiving Its driving me insane.
this is just me writing to post the simplest of questions
please aide me
//Binary Search Tree
#include <iostream>
#include <cstdlib>
using namespace std;
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
int data;
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
bool IsEmpty() const {
return root == NULL;
}
void print_inorder();
void inorder(tree_node*);
void print_preorder();
void preorder(tree_node*);
void print_postorder();
void postorder(tree_node*);
void insert(int);
void remove(int);
};
void BinarySearchTree::insert(int d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
if (IsEmpty()) root = t;
else
{
tree_node* curr;
curr = root;
while (curr)
{
parent = curr;
if (t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if (t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
void BinarySearchTree::remove(int d)
{
tree_node* curr;
tree_node* parent;
bool found= false;
if (IsEmpty())
{
cout << " This Tree is Empty!" << endl;
return;
}
curr = root;
while (curr != NULL)
{
if (curr->data == d)
{
found = true;
break;
}
else {
parent = curr;
if (d > curr->data) curr = curr->right;
else curr = curr->left;
}
}
if (!found)
{
cout << " Data is not found!" << endl;
return;
}
if ((curr->left == NULL && curr->right != NULL) || (curr->left != NULL && curr->right == NULL))
{
if (curr->left == NULL && curr->right !=NULL)
{
if (parent -> left == curr)
{
parent->left = curr->right;
delete curr;
}
else {
parent->right = curr->right;
delete curr;
}
}
else //Left child
{
if (parent->left == curr)
{
parent->left = curr->left;
delete curr;
}
else
{
parent->right = curr->left;
delete curr;
}
}
return;
}
if (curr->left == NULL && curr->right == NULL)
{
if (parent->left == curr) parent->left = NULL;
else parent->right = NULL;
delete curr;
return;
}
//Node with 2 Children
if (curr->left != NULL && curr->right != NULL)
{
tree_node* chkr;
chkr = curr->right;
if ((chkr->left == NULL) && (chkr->right == NULL))
{
curr = chkr;
delete chkr;
curr->right = NULL;
}
else
{
if ((curr->right)->left != NULL)
{
tree_node* lcurr;
tree_node* lcurrp;
lcurrp = curr->right;
lcurr = (curr->right)->left;
while(lcurr ->left != NULL)
{
lcurrp = lcurr;
lcurr = lcurr->left;
}
curr->data = lcurr->data;
delete lcurr;
lcurrp->left = NULL;
}
else {
tree_node* tmp;
tmp = curr->right;
curr->data = tmp->data;
curr->right = tmp->right;
delete tmp;
}
}
return;
}
}
void BinarySearchTree::print_inorder()
{
inorder(root);
}
void BinarySearchTree::inorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) inorder(p->left);
cout << " " << p->data << " ";
if (p->right) inorder(p->right);
}
else return;
}
void BinarySearchTree::print_preorder()
{
preorder(root);
}
void BinarySearchTree::preorder(tree_node* p)
{
if (p != NULL)
{
cout << " " << p->data << " ";
if (p->left)preorder(p->left);
if (p->right) preorder(p->right);
}
else return;
}
void BinarySearchTree::postorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) postorder(p->left);
if (p->right) postorder(p->right);
cout << " " << p->data << " ";
}
else return;
}
int main()
{
BinarySearchTree b;
int ch, tmp, tmp1;
while (1)
{
cout << endl << endl;
cout << " Binary Search Tree Operations " << endl;
cout << "---------------------------------" << endl;
cout << " 1. Insertion/Creation " << endl;
cout << " 2. In-Order Traversal " << endl;
cout << " 3. Pre-Order Traversal " << endl;
cout << " 4. Post-Order Traversal " << endl;
cout << " 5. Removal " << endl;
cout << " 6. Exit " << endl;
cout << " Enter your choice: ";
cin >> ch;
switch (ch)
{
case 1: cout << "Enter Number to be Inserted: ";
cin >> tmp;
b.insert(tmp);
break;
case 2: cout << endl;
cout << " In-Order Traversal: " << endl;
cout << "---------------------" << endl;
b.print_inorder();
break;
case 3: cout << endl;
cout << " Pre-Order Traversal: " << endl;
cout << "------------------------" << endl;
b.print_preorder();
break;
case 4: cout << endl;
cout << " Post-Order Traversal: " << endl;
cout << "--------------------------" << endl;
b.print_postorder();
break;
case 5: cout << " Enter data to be deleted: ";
cin >> tmp1;
b.remove(tmp1);
break;
case 6: system("pause");
return 0;
break;
}
}
}
tree_node* parent;
You declare parent but never assign it a tree_node memory address to point to. Thus, performing operations on it or accessing its members (like parent->left) will cause issues, and raise the error you are getting.
Solution: Assign a value to parent before accessing it's members.

How to fix segmentation error 11 in c++

I am currently writing a binary tree project in c++ and it is giving me some errors that I am struggling to understand. The error in question is a segmentation error that I have no idea how to fix. Below is the code from one one the files where all the functions are written. The function in particular that is giving me an issue is the last one - the print_pre_order_private function.
#include <iostream>
#include <cstdlib>
#include "BT.h"
using namespace std;
BT::BT()
{
root = NULL; //makes sure pointer isn't pointing at anything
}
BT::node* BT::create_leaf(int key)
{
node* n = new node;
n->key = key;
n->left = NULL;
n->right = NULL;
return n;
}
void BT::add_leaf(int key)
{
add_leaf_private(key, root);
}
void BT::add_leaf_private(int key, node* Ptr)
{
if(root == NULL)
{
root = create_leaf(key);
}
else if(key < Ptr->key)
{
if(Ptr->left != NULL)
{
add_leaf_private(key, Ptr->left);
}
else
{
Ptr->left = create_leaf(key);
}
}
else if(key > Ptr->key)
{
if(Ptr->right != NULL)
{
add_leaf_private(key, Ptr->right);
}
else
{
Ptr->right = create_leaf(key);
}
}
else
{
cout << "The key " << key << " has already been added to the tree\n";
}
}
void BT::print_in_order()
{
print_in_order_private(root);
}
void BT::print_in_order_private(node* Ptr)
{
if(root != NULL)
{
if(Ptr->left != NULL)
{
print_in_order_private(Ptr->left);
}
cout << Ptr->key << " ";
if(Ptr->right != NULL)
{
print_in_order_private(Ptr->right);
}
}
else
{
cout << "The tree is empty\n";
}
}
BT::node* BT::return_node(int key)
{
return return_node_private(key, root);
}
BT::node* BT::return_node_private(int key, node* Ptr)
{
if(Ptr != NULL)
{
if(Ptr->key == key)
{
return Ptr;
}
else
{
if(key < Ptr->key)
{
return return_node_private(key, Ptr->left);
}
else
{
return return_node_private(key, Ptr->right);
}
}
}
else
{
return NULL;
}
}
int BT::return_root_key()
{
if(root != NULL)
{
return root->key;
}
else
{
return -1;
}
}
void BT::print_children(int key)
{
node* Ptr = return_node(key);
if(Ptr != NULL)
{
cout << "Parent Node = " << Ptr->key << endl;
Ptr->left == NULL ?
cout << "Left child = NULL\n":
cout << "Left child = " << Ptr->left->key << endl;
Ptr->right == NULL ?
cout << "Right child = NULL\n":
cout << "Right child = " << Ptr->right->key << endl;
}
else
{
cout << "Key " << key << " is not in the tree\n";
}
}
int BT::find_smallest()
{
return find_smallest_private(root);
}
int BT::find_smallest_private(node* Ptr)
{
if(root == NULL)
{
cout << "The tree is empty\n";
return -1;
}
else
{
if(Ptr->left != NULL)
{
return find_smallest_private(Ptr->left);
}
else
{
return Ptr->key;
}
}
}
void BT::remove_node(int key)
{
remove_node_private(key, root);
}
void BT::remove_node_private(int key, node* parent)
{
if(root != NULL)
{
if(root->key == key)
{
remove_root_match();
}
else
{
if(key < parent->key && parent->left != NULL)
{
parent->left->key == key ?
remove_match(parent, parent->left, true) :
remove_node_private(key, parent->left);
}
else if(key < parent->key && parent->right != NULL)
{
parent->right->key == key ?
remove_match(parent, parent->right, true) :
remove_node_private(key, parent->right);
}
else
{
cout << "The key " << key << " was not found in the tree\n";
}
}
}
else
{
cout << "The tree is empty\n";
}
}
void BT::remove_root_match()
{
if(root != NULL)
{
node* delPtr = root;
int root_key = root->key;
int smallest_right_subtree;
//case 0 - 0 children
if(root->left == NULL && root->right == NULL)
{
root = NULL;
delete delPtr;
}
//case 1 - 1 child
else if(root->left == NULL && root->right != NULL)
{
root = root->right;
delPtr->right = NULL;
delete delPtr;
cout << "The root node with key " << root_key << " was deleted. "
<< " The new root contains key " << root->key << endl;
}
else if(root->right == NULL && root->left != NULL)
{
root = root->left;
delPtr->left = NULL;
delete delPtr;
cout << "The root node with key " << root_key << " was deleted. "
<< " The new root contains key " << root->key << endl;
}
//case 2 - 2 children
else
{
smallest_right_subtree = find_smallest_private(root->right);
remove_node_private(smallest_right_subtree, root);
root->key = smallest_right_subtree;
cout << "The rout key containing key " << root_key
<< " was overwritten with key " << root->key << endl;
}
}
else
{
cout << "Cannot remove root. Tree is empty\n";
}
}
void BT::remove_match(node* parent, node* match, bool left)
{
if(root != NULL)
{
node* delPtr;
int match_key = match->key;
int smallest_right_subtree;
//case 0 - 0 children
if(match->left == NULL && match->right == NULL)
{
delPtr = match;
left == true ? parent->left = NULL : parent->right = NULL;
delete delPtr;
cout << "The node containing key " << match_key << " was removed\n";
}
//case 1 - 1 child
else if(match->left == NULL && match->right != NULL)
{
left == true ? parent->left = match->left : parent->right = match->right;
match->left = NULL;
delPtr = match;
delete delPtr;
cout << "The node containing key " << match_key << " was removed\n";
}
//case 2 - 2 children
else
{
smallest_right_subtree = find_smallest_private(match->right);
remove_node_private(smallest_right_subtree, match);
match->key = smallest_right_subtree;
}
}
else
{
cout << "Cannot remove match. The tree is empty";
}
}
void print_pre_order()
{
print_pre_order_private(root);
}
void print_pre_order_private(node* Ptr)
{
if(root != NULL)
{
cout << Ptr->key << " ";
print_pre_order_private(Ptr->left);
print_pre_order_private(Ptr->right);
}
}
Any help in fixing this situation is greatly appreciated. Thanks in advance. :)
In print_pre_order_private(node* Ptr) function. Shouldn't this be "if(Ptr != NULL)" instead of "if(root != NULL)" ?. I think there is the crash.
Since you have designed "print_pre_order_private()" function as a recursive function you should check for the validity of "Ptr" instead of always checking for the validity of "root", since the left/right/both of the child may be NULL at the very bottom of the tree depending on your input.

Printing an expression tree

I am able to print my expression tree in inorder. But I need to be able to print it in inorder with parenthesis. for example postorder 53+ should output (5+3)
I currently have:
void printTree(struct TreeNode *tree)
{
if (tree != NULL)
{
if (isalpha(tree->info) || isdigit(tree->info))
cout << "(";
printTree( tree->left);
cout<< tree->info;
printTree(tree->right);
if (isalpha(tree->info) || isdigit(tree->info))
cout << ")";
}
}
But this gives me incorrect output. If i enter postfix expression 62/5+ it gives me (6)/(2)+(5)
:(
You need to distinguish between leaf nodes and non-leaf nodes. Only non-leaf nodes are enclosed in parantheses.
bool isLeaf(struct TreeNode* tree) {
return tree->left == 0 && tree->right == 0;
}
void printTree(struct TreeNode* tree) {
if (tree != NULL) { // this test could be omitted if the printed tree is not empty
if (isLeaf(tree)) {
cout << tree->info;
}
else {
cout << "(";
printTree(tree->left);
cout << tree->info;
printTree(tree->right);
cout << ")";
}
}
}
Try reversing the if conditions that decide whether the parentheses should be printed :
void printTree(struct TreeNode *tree)
{
if (tree != NULL)
{
if (!(isalpha(tree->info) || isdigit(tree->info)))
cout << "(";
printTree( tree->left);
cout<< tree->info;
printTree(tree->right);
if (!(isalpha(tree->info) || isdigit(tree->info)))
cout << ")";
}
}
Or probably even better :
void printTree(struct TreeNode *tree)
{
if (tree != NULL)
{
if (isoperator(tree->info))
cout << "(";
printTree( tree->left);
cout<< tree->info;
printTree(tree->right);
if (isoperator(tree->info))
cout << ")";
}
}
where isoperator is appropriately defined to distinguish operators from operands.