Segmentation fault on recursion function c++ - c++

I was working on Pre-order traverse Binary Tree algorithm. But I meet with Segmentation fault:11 error. The codes are shown as follow.
I want to know why this error happens. By the way, I have tried non-recursion algorithm preorder(),segmentation fault still happen.
The environment:
macOS, clang-800.0.38
struct Node{
char val;
Node* left;
Node* right;
};
void preorder(Node *T){
if(T!=NULL){
cout << T->val;
preorder(T->left);
preorder(T->right);
}
}
int main(){
Node *T = (Node *)malloc(sizeof(Node));
Node *p = T;
p->val = 'A';
p->left = (Node *)malloc(sizeof(Node));
p->left->val = 'B';
p->right = (Node *)malloc(sizeof(Node));
p->right->val = 'C';
preorder(T);
return 0;
}

You need to initialize left and right member variables of your nodes to null pointers.
Anyway, if you use C++, use C++ and not C constructs. This is an exemplary C++14 version of your code:
#include <iostream>
#include <memory>
struct Node{
Node(char a) : val(a) { };
char val;
std::unique_ptr<Node> left, right;
};
void preorder(Node* p) {
if (p) {
std::cout << p->val;
preorder(p->left.get());
preorder(p->right.get());
}
}
int main() {
auto root = std::make_unique<Node>('A');
root->left = std::make_unique<Node>('B');
root->right = std::make_unique<Node>('C');
preorder(root.get());
}

The problem is left and right of your child nodes are not initialized to NULL. Due to previous value stored in that memory location the program is encountering non-null value in your if(T!=nullptr) statement and executing the if block.
struct Node{
char val;
Node* left;
Node* right;
};
void preorder(Node *T){
if(T!=nullptr){
cout << T->val;
preorder(T->left);
preorder(T->right);
}
}
int main(){
Node *T = (Node *)malloc(sizeof(Node));
Node *p = T;
p->val = 'A';
p->left = (Node *)malloc(sizeof(Node));
p->left->val = 'B';
p->left->left = nullptr; //Initialize to NULL
p->left->right = nullptr; //Initialize to NULL
p->right = (Node *)malloc(sizeof(Node));
p->right->val = 'C';
p->right->left = nullptr; //Initialize to NULL
p->right->right = nullptr; //Initialize to NULL
preorder(T);
return 0;
}

Related

Binary Search Tree is printing a random 0 and I can't find where its being created

I am working on a BST and when I print out the elements in any order, I get a random '0' appended to it, but I cannot find where its coming from.
I followed the pseudo code thats present in Introduction to algorithms by Cormen and have also looked at Geeks for Geeks but I have no luck getting rid of that 0.
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* LeftChild;
Node* RightChild;
Node(int data){
this->data = data;
this->LeftChild = NULL;
this->RightChild = NULL;
}
//pointers of the class
};
class BST {
private:
Node* root;
public:
BST(){ ///creating an empty tree in Constant Time
root = new Node(NULL);
}
Node* getRoot(){ return this->root; };
int i =0;
void printTree(Node *root)
{
if (root == NULL)
return;
else {
printTree(root->LeftChild);
cout << root->data << " ";
printTree(root->RightChild);
}
}
Node* InsertNode(Node *root,int data)
{
Node *z = new Node(data);
Node *y = new Node(NULL);
Node *x = this->root;
//if(x->data < z->data){
// x = z;
//return x;
//}
while(x!= NULL){
y = x;
if(data < x->data){
x = x->LeftChild;
}
else{
x = x->RightChild;
}
}
if(y== NULL) y= z;
else if(data < y->data){
y->LeftChild = z;
}
else{
y->RightChild =z;
}
return y;
/*
if(this->root->data== NULL){
this->root =z;
return root;
}
else{
this->root =y;
}
*/
//this->root = z;
//return root;
}
bool FindNode(Node *root,int data);
int Largest(Node *root){
return root->data;
}
};
int main()
{
BST myBst;
Node * root = (myBst.getRoot());
root = myBst.InsertNode(root, 24);
myBst.InsertNode(root, 60);
myBst.InsertNode(root, 55);
myBst.InsertNode(root, 32);
myBst.printTree(root);
return 0;
}
Here is the output:
0, 24,32,55,60
The constructor does not make a sense
BST(){ ///creating an empty tree in Constant Time
root = new Node(NULL);
}
There is created a dummy node with initialization of the data member data with NULL.
What you need is just to write
BST() : root( nullptr ) { ///creating an empty tree in Constant Time
}
The function InsertNode must have only one parameter instead of two parameters as you wrote
Node* InsertNode(Node *root,int data){
The pointer root is the data member of the class. So there is no need to pass it to the function. Otherwise the function should be declared as a static member function of the class (that nevertheless does not make a great sense).
That is the function should be declared like
void InsertNode( int data ){
Also the function has at least a memory leak
Node* InsertNode(Node *root,int data){
Node *z = new Node(data);
Node *y = new Node(NULL);
Node *x = this->root;
while(x!= NULL){
y = x;
//...
The function can be written for example the following way
void InsertNode( int data )
{
Node *new_node = Node( data );
Node **current = &root;
while ( *current != nullptr )
{
if ( data < ( *current )->data )
{
current = &( *current )->LeftChild;
}
else
{
current = &( *current )->RightChild;
}
}
*current = new_node;
}

Try tree inplementation

Try to make tree , have a some troubles, first it's print function - it's print not integers that i put, but print random numbers;
Another trouble its append child - its works only one times;
Will be happy if you will help me with this task.
And also give some good articles about linked lists, trees on c and c++;
#include <iostream>
#include <stdio.h>
using namespace std;
struct Node
{
void* m_pPayload;
Node* m_pParent;
Node* m_Children;
};
struct Person
{
int m_Id;
};
//typedef bool (*NodeComparator)(void* pValue, void* pPayload);
/*bool Comp(void* pValue, void* pPayload)
{
Person* pVal = (Person*)pValue;
Person* pPay = (Person*)pPayload;
if (pVal->m_Id == pPay->m_Id)
return true;
else
return false;
}
*/
Node* NewNode(void* pPayload)
{
Node* pNode = new Node;
pNode->m_pParent = nullptr;
pNode->m_Children = 0;
pNode->m_pPayload = pPayload;
return pNode;
}
Person* NewPerson(int id)
{
Person* p = new Person;
p->m_Id = id;
return p;
}
//Node* FindNode(Node* pParent, Node* m_pPayload, NodeComparator comparator);
void AppendChild(Node* pParent, Node* pNode)
{
if (pParent->m_Children == NULL)
pParent->m_Children = pNode;
}
void print(Node* head)
{
Node* current_node = head;
while (current_node != NULL)
{
printf("%d\n ", current_node->m_pPayload);
current_node = current_node->m_Children;
}
}
int main()
{
Node* T = new Node;
T = NewNode(NewPerson(5));
AppendChild(T, NewNode(NewPerson(11)));
AppendChild(T, NewNode(NewPerson(15)));
print(T);
}
printf("%d\n ", current_node->m_pPayload)
is incorrect. %d wants an integer and it's being given a pointer. The results will be unusual, and likely appear to be random garbage.
printf("%d\n ", ((Person*)current_node->m_pPayload)->m_Id);
^ ^
| Get id from Person
treat payload pointer as pointer to Person
will solve the immediate problem.
Your code actually seems to be pretty messed up with a lot of things going on, here sharing my own commented code from few years back, hope it helps
#include <bits/stdc++.h>
using namespace std;
// Single node representation
struct node {
int data;
node *left, *right;
};
// Declaring temp for refference and root to hold root node
node *root, *temp;
// This function only generates a node and return it to the calling function with data stored in it
node* generateNode(int data){
temp = new node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// This function actually adds node to the tree
node* addNode(int data, node *ptr = root){
// If the node passed as ptr is NULL
if(ptr == NULL){
ptr = generateNode(data);
return ptr;
}
// Condition to check in which side the data will fit in the tree
else if(ptr->data < data)
//if its in right, calling this function recursively, with the right part of the tree as the root tree
ptr->right = addNode(data, ptr->right);
else
//In case the data fits in left
ptr->left = addNode(data, ptr->left);
//Note: if there is no data in left or roght depending on the data's valid position, this function will get called with NULL as second argument and then the first condition will get triggered
//returning the tree after appending the child
return ptr;
}
//Driver function
int main ()
{
int c, data;
for (;;){
cin >> c;
switch(c){
case 1:
cout << "enter data: ";
cin >> data;
//Updating root as the tree returned by the addNode function after adding a node
root = addNode(data);
break;
default:
exit(0);
break;
}
}
return 0;
}
Please find below a piece of code that should easily get you started. It compiles and it traverse the tree using recursion.
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
struct Node
{
int m_Id;
vector<Node*> m_Children;
Node(const int& id){
m_Id = id;
}
void AppendChild(Node* pNode) {
m_Children.push_back(pNode);
}
void Print() {
printf("%d\n ", m_Id);
}
};
void traverse(Node* head)
{
Node* current_node = head;
current_node->Print();
for(int i = 0; i<current_node->m_Children.size(); i++) {
traverse(current_node->m_Children[i]);
}
}
int main()
{
Node* T0 = new Node(0);
Node* T10 = new Node(10);
T10->AppendChild(new Node(20));
Node* T11 = new Node(11);
Node* T12 = new Node(12);
Node* T22 = new Node(22);
T22->AppendChild(new Node(33));
T12->AppendChild(T22);
T0->AppendChild(T10);
T0->AppendChild(T11);
T0->AppendChild(T12);
traverse(T0);
}
First for printing the node value
Talking about the current mistake that you had committed is in the above code is:
You have not mentioned its pointer to its child (specifically right or left). Due to which it is showing garbage value every time.
For e.g.: print( node->left);
Since you need to type caste it properly to show the data of data.
For e.g.: printf("%d\n ", ((Person*)current_node->m_pPayload)->m_Id);
There is a specific direction in which you want to print data. For trees, there are three directions in which you can print the data of the node and they are as follow:
Left order or Inorder traversal
Preorder traversal
Postorder traversal
This can give you better information about traversal.
Secondly for adding the node to a tree
This might help explain it better.

Segmentation fault in passing pointer to a linked list

Hello fellow programmers, the below code gives segmentation fault. This code aims to insert an element at the end of a linked list. I tried using print statements to debug it. I think the error is in passing the linked list pointer to insert() function. Please tell me how can I correct it. Thanks in advance.
Below is the code:
#include <iostream>
using namespace std;
class node {
public:
int data;
node *next;
node(int data) {
this->data = data;
this->next = NULL;
}
};
class linked_list {
public:
node *head;
linked_list() {
this->head = NULL;
}
};
void insert(node **head, int data);
void print(linked_list *L);
int main() {
int N;
linked_list *A = new linked_list();
cout << "N: ";
cin >> N;
for(int i=0; i<=N-1; i++) {
int t;
cin >> t;
insert(&(A->head), t);
}
print(A);
return 0;
}
void insert(node **head, int data) {
node *temp = new node(data);
if(*head == NULL) {
*head = temp;
return;
} else {
node *t = *head;
while(t->next != NULL) {
t=t->next;
}
t->next = temp;
return;
}
}
void print(linked_list *L) {
node * t = L->head;
while(t!=NULL) {
cout << t->data << " ";
t = t->next;
}
return;
}
main.cpp:42:14: error: using the result of an assignment as a
condition without parentheses [-Werror,-Wparentheses]
if(*head = NULL) {
~~~~~~^~~~~~
main.cpp:42:14: note: place parentheses around the assignment to
silence this warning
if(*head = NULL) {
^
( )
main.cpp:42:14: note: use '==' to turn this assignment into an
equality comparison
if(*head = NULL) {
^
==
1 error generated.
You're using assignment where you intended to do a comparison.

Returning an object pointer from a recursive function met with access violation

I've created a simple recursive function that performs preorder traversal to return a node that matches a target string.
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
string value;
Node *left, *right;
Node(string value) {
this->value = value;
this->left = NULL;
this->right = NULL;
}
};
class Tree {
public:
Node* preorder(Node* root, string target) {
if (root == NULL) return NULL;
if (root->value == target) {
return root;
}
preorder(root->left, target);
preorder(root->right, target);
}
};
int main() {
Node* a = new Node("a");
Node* b = new Node("b");
Node* c = new Node("c");
Node* d = new Node("d");
a->left = b;
a->right = c;
c->left = d;
Tree t = Tree();
Node* found = t.preorder(a, "d");
cout << found->value << endl;
}
The traversal is done correctly, but the program doesn't print anything. I just get a [Done] exited with code=3221225477 in 2.038 seconds after compiling with g++ and running.
Where did I mess up with the pointers?
preorder doesn't return anything in yhe recursive cases. The behaviour of the program is unrefined.
Hint for future: Use compiler warnings.

g++ -O2 flag giving segmentation fault

The program below is a bst tree which works fine under unoptimized settings but produces a SIGSEGV under special circumstances. Since my debugging skills doesn't extend towards assembly, I can use some input to what is causing this error. Below is the full code so it can be reproduced. There is nothing fancy, a node struct is there to hold node data, a simple insert operation and a method to confirm the height of the tree.
#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct avl_tree_node //node data
{
int data;
int balance{0};
avl_tree_node *left{NULL};
avl_tree_node *right{NULL};
avl_tree_node *parent{NULL};
}node;
class avl
{
private:
node *root;
int get_height(node *head) //calculates the height
{
if (head == NULL)
return -1;
int l_height = get_height(head->left);
int r_height = get_height(head->right);
if (l_height > r_height)
return l_height+1;
return r_height+1;
}
void unbalanced_insert(node *head, int item); //method definition for a simple insert
public:
avl(int data)
{
root->data = data;
root->parent = NULL;
root->left = NULL;
root->right = NULL;
}
int height() //gives the height
{
return get_height(root);
}
void unbalanced_insert(int item) //wrapper
{
unbalanced_insert(root, item);
}
};
void avl::unbalanced_insert(node *head, int item) //inserts node to the tree
{
//cout << "stepped" << endl;
if (item > head->data)
{
if (head->right == NULL)
{
head->right = (node*)malloc(sizeof(node));
head->right->data = item;
head->right->parent = head;
head->right->left = NULL;
head->right->right = NULL;
head->balance = 1;
return;
}
unbalanced_insert(head->right, item);
head->balance++;
return;
}
else
{
if (head->left == NULL)
{
head->left = (node*)malloc(sizeof(node));
head->left->data = item;
head->left->parent= head;
head->left->left = NULL;
head->left->right = NULL;
head->balance = -1;
return;
}
unbalanced_insert(head->left, item);
head->balance--;
return;
}
}
int main()
{
avl a(0);
for (int i = 1; i < 5; i++) //works until i < 4
{
a.unbalanced_insert(i);
}
cout << a.height() << endl;
return 0;
}
Under normal circumstances, I'd be happy that this works with unoptimized flags, but I have to build this with specific flags. One of such is the -O2 flag. The segmentation fault occurs between the avl a(0) object construction and the for loop inside main. The error also seems to be dependent on the boolean check of the for loop. This works fine if i < 4 and executed with: g++ avl.cpp -g -O2 -o program && ./program
One obvious problem, and it occurs on the very first function call in main, i.e. avl a(0):
root->data = data;
The root is uninitialized, thus the behavior is undefined.
I guess when instantiate the object of avl here.
avl a(0);
constructor of class as shown below is called.
avl(int data)
{
root->data = data;
root->parent = NULL;
root->left = NULL;
root->right = NULL;
}
But here I see root pointer is not allocated any memory