Tree in Data structures

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

class treefun;

class treeNode
{
friend class treefun;
private:
int data;
treeNode * right;
treeNode * left;
public:
treeNode(){
right =left =NULL;
};


};
class treefun
{
private:
treeNode * root;
public:
treefun(){
root= NULL;};
bool isEmpty();
void Insert(int list[],int size);
void Print();
void inorder();
void inorder(treeNode * node);
void dummy(int list[]);
};

int main()
{


int list[16]={-1,1,2,3,4,5,6,7,-1,-1,-1,-1,-1,-1,-1,-1};
treefun t;
//t.dummy(list);

t.Insert(list,16);



t.inorder();

return 0;}

///////////////////////////////
bool treefun::isEmpty()
{

return root == NULL;
}
//////////////////////////////////
void treefun::Insert(int list[],int size)
{
cout<<"Insert"<<endl;
treeNode arr[16];

for (int i=0;i<size;i++)
{

int c=list[i];
arr[i].data=c;
}


for (int h=0;h<size;h++)
{

//cout<<endl<<arr[h].data<<endl;
}
int d=0;


for(int k=1;k<size;k++)
{
if(isEmpty())
{

root=&arr[k];
}
for(int l=0;l<2;l++)
{
d=k*2;
if(arr[d].data == -1)
{
cout<<"L"<<endl;
arr[k].left=NULL;
}
else
arr[k].left=&arr[d];

d++;
if(arr[d].data == -1)
{
cout<<"R"<<endl;
arr[k].right=NULL;
}
else
arr[k].right=&arr[d];
}

d=0;
}


cout<<endl<<root->right->left->data<<endl;
}

///////////////////////////////////////////////
void treefun::inorder()
{

inorder(root);
}

void treefun::inorder(treeNode * node)
{ static int c=0;
cout<<endl<<root->right->left->data<<endl;
if (node)
{
c++;
cout<<endl<<c<<endl;
inorder (node->left); // L

inorder (node->right); // R
cout <<endl<< node->data; // V
}
}

/////////////////////////////////////////////

PLEASE FIND THE BUG .......
pLease help me ?????
on this ......
Use the #format when uploading code and give some hints about how your program works if you post a lot of code.

I'm not sure about this, because i'm not familiar with null-pointers, but i think that thats your problem. The following code gives the same run-time error:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
    int * pointer;
    pointer=NULL;
    *pointer=5;
    cout<<pointer;
    cin.ignore();
    return 0;
}
I am not getting ur point in this code ...
Again, i'm not sure about this:

You call the function treefun::inorder() without parameters. That function calls treefun::inorder(treeNode * node) with as parameter root, wich is on this moment a NULL-pointer. In the function treefun::inorder(treeNode * node) you use this parameter several times, wich causes problems. That was my point in my code above: using NULL-pointers gives problems.

I am sure about this: the problem is in these lines (i'm sure, because if you delete them, the program runs without problems):
1
2
3
inorder (node->left); // L

inorder (node->right); // R 


Hope this helps

Last edited on
Thanks Buddy !!! , i found out the problem .....
Yeah, deferencing a NULL pointer causes undefined behavior.
so if we want to assign Null , so what can we do ??
From the tutorial on this site:

Null pointer
A null pointer is a regular pointer of any pointer type which has a special value that indicates that it is not pointing to any valid reference or memory address. This value is the result of type-casting the integer value zero to any pointer type.
int * p;
p = 0; // p has a null pointer value


Do not confuse null pointers with void pointers. A null pointer is a value that any pointer may take to represent that it is pointing to "nowhere", while a void pointer is a special type of pointer that can point to somewhere without a specific type. One refers to the value stored in the pointer itself and the other to the type of data it points to.



You can understand that you cant store a value into "nowhere". You first need to give the pointer an addres: type* pointer=&variable
Topic archived. No new replies allowed.