I can't figure out how this works, to my mind, once it gets to the answer it doesn't do anything with it.
Node* FindNode(Node *rootNode, int data)
{
if (!rootNode)
return NULL;
else
{
if (rootNode->data == data)
return rootNode;
else
{
FindNode(rootNode->left, data);
FindNode(rootNode->right, data);
}
}
}
It doesn't. It should be:
Node* FindNode(Node *rootNode, int data) {
if (!rootNode) {
return NULL;
}else if (rootNode->data == data) {
return rootNode;
}else if (data < rootNode->data) {
return FindNode(rootNode->left, data);
}else{
return FindNode(rootNode->right, data);
}
}
Note the extra return statements, and the extra else if clause.
EDIT — To sum up the comments below: The only reason the code you posted could be working is if an odd combination of compiler-implementation details and test data came together in your favour. You should definitely fix the problem rather than keeping the code how it was.
This is assuming that the FindNode returns on the first match.
Node* FindNode(Node *rootNode, int data)
{
Node *ptr;
if (!rootNode)
return NULL;
else
{
if (rootNode->data == data)
return rootNode;
else
{
ptr = NULL;
// if either left or right child is there
if(rootNode->left || rootNode->right)
{
// if not found in left subtree
if(NULL == (ptr = FindNode(rootNode->left, data))){
// check in right subtree
ptr = FindNode(rootNode->right, data);
}
}
return ptr;
}
}
}
Related
I want to try make insertion of complete binary tree using recursion . I make a piece of code and I cannot catch problem why value not inserted. I make height function and count nodes function with help of these function and recursive call I want to insert new node in Complete binary tree. In main get root by using get root function then send to insert function
#include<iostream>
#include<math.h>
using namespace std;
struct node{
int data;
node *left,*right;
};
class cbt{
node *root;
public:
cbt()
{
root=NULL;
}
node* get_node()
{
return root;
}
node* newNode(int key)
{
node* temp1 = new node;
temp1->data = key;
temp1->left = temp1->right = NULL;
return temp1;
}
void CBT_inseration(node* temp,int data)
{
node *ptr;
ptr=newNode(data);
if(root==NULL)
{
root=ptr;
return;
}
else
{
height = f_height(temp->left);
int excepted_node = pow(2,height)-1;
int left_tree_node_count = countNumNodes(temp->left);
int right_tree_node_count = countNumNodes(temp->right);
if(left_tree_node_count==right_tree_node_count)
{
CBT_inseration(temp->left,data);
}
else if(excepted_node != left_tree_node_count)
{
if(temp->left == NULL)
{
temp->left = ptr;
return;
}else
{
CBT_inseration(temp->left,data);
}
}
else if(temp->right == NULL)
{
temp->right=ptr;
return;
}
else if(excepted_node != left_tree_node_count)
{
if(temp->left == NULL)
{
temp->left=ptr;
return;
}
else
{
CBT_inseration(temp->right,data);
}
}
}
}
void print(node *root) {
if (root == NULL)
return;
print(root->left);
cout << root->data << " ";
print(root->right);
}
};
int main()
{
cbt obj;
node *r=NULL;
obj.CBT_inseration(obj.get_node(),4);
obj.CBT_inseration(obj.get_node(),3);
obj.CBT_inseration(obj.get_node(),5);
obj.CBT_inseration(obj.get_node(),8);
obj.print(obj.get_node());
return 0;
}
You would need to go through a debugger to see what is wrong with your code. I will tell you how I would do this:
First, you need a function to check if the tree is full. We will reuse your functions to do this:
bool isTreeFull(node* head) {
return head != NULL && countNumNodes(head) == (1 << find_height(head)) - 1;
}
Then for inserting I have to check if the number of nodes on each side are the same. This tells me that I am allowed to move one level deeper on the left side. If the number of nodes aren't the same, then I only move on to insert on the right subtree if the left subtree is full:
void CBT_inseration(int data) {
root = insert(root, data);
}
node* insert(node* head, int data) {
if (head == NULL) {
head = newNode(data);
} else {
int leftCount = countNumNodes(head->left);
int rightCount = countNumNodes(head->right);
if (leftCount == rightCount) {
head->left = insert(head->left, data);
} else {
if (isTreeFull(head->left)) {
head->right = insert(head->right, data);
} else {
head->left = insert(head->left, data);
}
}
}
return head;
}
Then you would call it like:
cbt obj;
obj.CBT_inseration(4);
obj.CBT_inseration(3);
obj.CBT_inseration(5);
obj.CBT_inseration(6);
obj.CBT_inseration(8);
obj.print(obj.get_node()); // 6 3 8 4 5
For binary search tree to see if the tree has duplicated value or not. I took this post order approach.
My goal was to keep the value of the current node and then use other function traverse the tree to see if there is any matching value to that current value, and if it finds any duplicate value it brings "true value". I choose to use recursion as it seems easier to track. but when I ran the program there was no output coming out.
#include "pch.h"
#include <iostream>
using namespace std;
class BSTNode {
public:
int data;
BSTNode* left;
BSTNode* right;
BSTNode() {};
};
BSTNode* newnode(int newdata) { BSTNode *curr = new BSTNode; curr->data = newdata; curr->left = curr->right = nullptr; return curr; }
void print(BSTNode* root) {
if (root != nullptr) {
print(root->left);
cout << root->data << endl;
print(root->right);
}
}
bool checking(BSTNode* parent, int val) {
if (val == parent->data){
bool left = checking(parent->left, val);
bool right = checking(parent->right, val);
return left||right;
}
else
return false;
}
bool assist(BSTNode* parent) {
if (parent != nullptr) {
assist(parent->left);
assist(parent->right);
return checking(parent, parent->data);
}
else return false;
}
int main() {
BSTNode *test = newnode(1);
test->left=newnode(2);
test->right=newnode(3);
test->left->left=newnode(2);
test->right->right=newnode(5);
print(test);
if (assist(test))
cout << "There is duplicated" << endl;
else
cout << "There is no duplicated" << endl;
return 0;
}
Your checking function should look like this:
bool checking(BSTNode* parent, int val) {
if(parent == nullptr) // point 1
return false;
if (val == parent->data){ // point 2
return true;
}
else{
bool left = checking(parent->left, val);
bool right = checking(parent->right, val);
return left||right;
}
}
Your assist function should look something like this:
bool assist(BSTNode* parent) {
if (parent != nullptr) {
if(checking(parent->left, parent->data)) return true; // point 3
if(checking(parent->right, parent->data)) return true;
return assist(parent->left)||assist(parent->right); // point 4
}
else return false;
}
You need to check for null values.
If val is same, why are you still checking? Just stop
You need to check node's value in the left and right subtree.
Recurse it for the child nodes
If you want to check that parent value is different than child values, you might do:
bool checking(const BSTNode* node, int parent_value) {
if (node == nullptr) { return false; }
if (node->data == parent_value) { return true; }
return checking(node->left, node->data)
|| checking(node->right, node->data);
}
bool assist(const BSTNode* parent) {
if (parent == nullptr) {
return false;
}
return checking(parent->left, parent->data)
|| checking(parent->right, parent->data);
}
You could just go through the BST breadth wise with a Deque. Store the values in a set and check if the value is already in the set, if it is return true otherwise wait for the loop to finish and return true. This had the benefit of hash table lookup for values at thr cost of extra storage in O(n) time. Its also easier to follow in my opinion as it's not recursion.
bool hasDuplicate(BSTNode *parent)
{
if (!parent) return false;
std::dueue<BSTNode*> nodes;
std::unordered_set<int> vals;
nodes.push_back(parent);
while(!nodes.empty()) {
BSTNode *node = nodes.pop_front();
int v = nodes->val;
// Check if value exists and return true
if(vals.find(v) != vals.end()) return true;
// Otherwise insert it
vals.insert(v);
// insert left node if exists
if (node->left) nodes.push_back(node->left);
// insert right node if exists
if (node->right) nodes.push_back(node->right);
}
// no dups found
return false;
}
Sorry for bad indents. Did this on phone lol.
I am trying to implement DSs in C++, here is a simple implementation of a Binary Search Tree Class with insert and search functions. The code compiles and gives the output as required.
Someone at codereview pointed out that the search function gives a warning and the code in the search function is broken. The warning is similar to 'not all control paths have a return statement' but I thought that is how a recursive function would look like. Is the warning a problem and how do I get rid of it? Also, how is the code broken? Thank you.
#include <stdio.h>
#include <iostream>
class BstNode{
int data;
BstNode* left;
BstNode* right;
public:
BstNode(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
~BstNode();
void Insert(int data)
{
if(this->data >= data)
{
if (this->left == NULL)
this->left = new BstNode(data);
else
this->left->Insert(data);
}
else
{
if (this->right == NULL)
this->right = new BstNode(data);
else
this->right->Insert(data);
}
}
bool Search(int data)
{
if(this->data == data)
return true;
else if(this->data >= data)
{
if(this->left == NULL)
return false;
else
this->left->Search(data);
}
else
{
if(this->right == NULL)
return false;
else
this->right->Search(data);
}
}
};
int main()
{
BstNode* ptr_root = new BstNode(15);
ptr_root->Insert(10);
ptr_root->Insert(16);
int num;
std::cout<<"Enter the number: \n";
std::cin>> num;
if (ptr_root->Search(num))
std::cout<<"Found\n";
else
std::cout<<"Not Found\n";
return 0;
}
This function Search returns nothing in these paths
else
this->left->Search(data);
and
else
this->right->Search(data);
You have to write
else
return this->left->Search(data);
and
else
return this->right->Search(data);
The function can be defined with a single return statement the following way
bool Search( int data ) const
{
return ( this->data == data ) ||
( this->data >= data ? this->left && this->left->Search( data )
: this->right && this->right->Search( data ) );
}
Actually the condition
this->data >= data
may be substituted for
this->data > data
This is my own BST(binary search tree functions)
When I compile, it occurs segmentation error(core dumped)
which part is wrong?
I think there is no logic error
Please see my codes and give me advices
void Tree::remove(int data){
TreeNode *cursor=this->rootnode;
while(1){
if(cursor == NULL){
cout<<endl;
cout<<data<<" remove failed"<<endl;
return;
}
TreeNode *tNode;
int value=cursor->getData();
if(value==data){
if(cursor->getRight() != NULL && cursor->getLeft() != NULL){
tNode = findMaxNode(cursor->getLeft());
int num=tNode->getData();
cursor->setData(num);
cursor=cursor->getLeft();
remove(num);
}
else
{
tNode = (cursor->getLeft() == NULL) ? cursor->getRight() : cursor->getLeft();
free(cursor);
cout<<data<<" remove success!"<<endl;
return;
}
}
else if(data>value){
cursor=cursor->getRight();
remove(data);
}
else if(data<value){
cursor=cursor->getLeft();
remove(data);
}
}
}
You should to use (more) the recursivity to solve your problem. I hope that this code will help you.
void Tree::remove(int data)
{
if(this->rootnode == NULL)
{
cout<<data<<" remove failed"<<endl;
return;
}
remove(this->rootnode, data);
}
void Tree::remove(TreeNode* &cursor, int data)
{
if (cursor != NULL)
{
if (data == cursor->getData())
{
remove(cursor->getLeft(), data);
remove(cursor->getRight(), data);
delete cursor;
cursor = NULL;
}
}
}
This is part of a lab for school dealing with recursion and binary tree. If I go to insert 4 or 5 numbers and output the result I get just 3 numbers back. Here is the code for insert:
Node *insert(Node *t, int key) {
Node *insertParent;
Node *result=NULL;
if (t!=NULL) {
result=search(t,key,insertParent);
} else {
t=new Node;
t->data=key;
t->leftchild=NULL;
t->rightchild=NULL;
return t;
}
if (result==NULL) {
if (insertParent->data>key) {
insertParent->leftchild=new Node;
insertParent->leftchild->data=key;
insertParent->leftchild->leftchild=NULL;
insertParent->leftchild->rightchild=NULL;
return insertParent->leftchild;
} else if (insertParent->data<key) {
insertParent->rightchild=new Node;
insertParent->rightchild->data=key;
insertParent->rightchild->leftchild=NULL;
insertParent->rightchild->rightchild=NULL;
return insertParent->rightchild;
}
} else
return NULL;
}
But I believe the trouble is within the search function, specifically the node pointer by reference parent:
Node* search(Node *t, int key, Node *&parent) {
if (t!=NULL) {
parent=t;
if (t->data==key)
return t;
else if (t->data>key)
return search(t->leftchild,key,t);
else
return search(t->rightchild,key,t);
} else
return NULL;
}
I have a function that outputs the tree and have checked it against a tree I built manually and it works fine:
void inorder(Node *t)
{
if (t!=NULL) {
if (t->leftchild!=NULL)
inorder(t->leftchild);
cout << t->data << ", ";
if (t->rightchild!=NULL)
inorder(t->rightchild);
}
}
Not looking for an answer just looking for an area I should look at.
Your suspicion is correct. Trace how the top-level 'parent' parameter gets updated once you search more than one node deep.
Node* search(Node *t, int key, Node *&parent)
{
if(t!=NULL)
{
parent=t;
if (t->data==key)
return t;
else if (t->data>key)
return search(t->leftchild, key, parent); // use “parent” because you want to assign parent to this variable
else
return search(t->rightchild,key, parent);
}
else return NULL;
}
So I discovered my problem was with the search function. It did have to do with the reference parent node variable. I had to use four else/ifelse statement to be able to decide which way to go, recursively or not.
Node* search(Node *t, int key, Node *&parent) {
if (t!=NULL) {
if (t->data==key) {
parent=NULL;
return t;
} else if (t->data>key && t->leftchild!=NULL) {
return search(t->leftchild,key,parent); // think this needs returned
} else if (t->data>key && t->leftchild==NULL) {
parent=t;
return NULL;
} else if (t->data<key && t->rightchild!=NULL) {
return search(t->rightchild,key,parent); //think this needs returned
} else if (t->data<key && t->rightchild==NULL) {
parent=t;
return NULL;
}
} else {
parent=NULL;
return NULL;
}
}
This change in the search function necessitated a change in the insert function:
Node *insert(Node *t, int key) {
Node *insertParent=NULL;
Node *result=NULL;
if (t!=NULL) {
result=search(t,key,insertParent);
} else {
t=new Node;
t->data=key;
t->leftchild=NULL;
t->rightchild=NULL;
return t;
}
if (insertParent==NULL && result!=NULL) {
return NULL;
} else if (insertParent!=NULL && result==NULL) {
//key not found need to add
if (insertParent->data>key) {
insertParent->leftchild=new Node;
insertParent->leftchild->data=key;
insertParent->leftchild->leftchild=NULL;
insertParent->leftchild->rightchild=NULL;
return insertParent->leftchild;
} else {
insertParent->rightchild=new Node;
insertParent->rightchild->data=key;
insertParent->rightchild->leftchild=NULL;
insertParent->rightchild->rightchild=NULL;
return insertParent->rightchild;
}
}
}
Thanks to all who helped...
(Also: I answered my own question. Is this what I should of done or should I of edited my post? Would think that people would rather see the whole process and not have me delete the old non-working code...)