AvlTree

Hello everyone.So i have this problem that i cant call my class functions in the main.Any help would be appreciated.I runned Debugger and it says that it has segmetation fault.(at r->d = v;)

#include <iostream>
#include <algorithm>

using namespace std;

struct avl
{
int d; //d is the data
struct avl *l; //l is left
struct avl *r; // r is right
};
class Avl_Tree
{
public:
avl *insert(avl* r,int v);
avl *Balance(avl *t);
int Difference(avl *t);
int Height(avl *t);
avl *ll_rotation(avl *parent);
avl *rr_rotation(avl *parent);
avl *lr_rotation(avl *parent);
avl *rl_rotation(avl *parent);
avl *FindMin(avl *parent);
avl *FindNumber(avl *parent,int key);
avl *DeleteNumber(avl *parent,int key);
int Getsize(){return counter;}

protected:

private:

int counter = 0;
};


//Inserting new item to the tree
avl *Avl_Tree::insert(avl *r,int v)
{
if (r == NULL)
{
r->d = v;
r->l=NULL;
r->r=NULL;
return r;
}
else if(v < r->d)
{
r->l=insert(r->l,v);
r=Balance(r);
}
else if(v>=r->d)
{
r->r=insert(r->r,v);
r=Balance(r);
}
counter++;
return r;
}

//Balancing the tree
avl *Avl_Tree::Balance(avl *t)
{
int b =Difference(t);
if (b>1)
{
if (Difference (t->l) > 0)
t= ll_rotation(t);
else
t= lr_rotation(t);
}
else if (b< -1)
{
if (Difference(t->r) > 0)
t= rl_rotation(t);
else
t= rr_rotation(t);
}
return t;

}

//Difference of the tree's height
int Avl_Tree::Difference(avl *t)
{
int l_height = Height(t->l);
int r_height = Height(t->r);
int b = l_height - r_height;
return b;

}

//Height of the tree
int Avl_Tree::Height(avl *t)
{
int h = 0;
if (t != NULL)
{
int l_height = Height(t->l);
int r_height = Height(t->r);
int max_height = std::max(l_height, r_height);
h = max_height + 1;
}
return h;
}

//Left-Left rotation
avl *Avl_Tree::ll_rotation(avl *parent)
{
avl *t;
t = parent->l;
parent->l = t->r;
t->r = parent;
return t;
}

//Right-Right rotation
avl *Avl_Tree::rr_rotation(avl *parent)
{
avl *t;
t = parent->r;
parent->r = t->l;
t->l = parent;
return t;
}

//Right-Left rotation
avl *Avl_Tree::rl_rotation(avl *parent)
{
avl *t;
t = parent->l;
parent->l = rr_rotation(t);
return ll_rotation(parent);

}

//Left-Right rotation
avl *Avl_Tree::lr_rotation(avl *parent)
{
avl *t;
t = parent->r;
parent->r = ll_rotation(t);
return rr_rotation(parent);

}

//Finds the minimum value of the tree
avl *Avl_Tree::FindMin(avl *parent)
{
avl *current = parent;
while (current->l != NULL)
{
current = current->l;
}
return current;
}

//Finds the chosen number in the tree
avl *Avl_Tree::FindNumber(avl *parent,int key)
{

if (parent == NULL)
{
return parent;
}
else if (key < parent->d)
{
parent->l = FindNumber(parent->l,key);
}
else if (key > parent->d)
{
parent->r = FindNumber(parent->r,key);
}
else if (parent->d == key)
{
return parent;
}
}

//Deletes the chosen number
avl *Avl_Tree::DeleteNumber(avl *parent,int key)
{
avl *temp;
if (parent == NULL)
{
return NULL;
}
else if (key < parent->d)
{
parent->l = DeleteNumber(parent->l,key);
}
else if (key > parent->d)
{
parent->r = DeleteNumber(parent->r,key);
}
//If the parent have 2 children
else if (parent->l !=NULL && parent->r != NULL)
{
temp = FindMin(parent->r);
parent->d = temp->d;
parent->r = DeleteNumber(parent->r,parent->d);
}
//If the parent have one child or none
else
{
temp = parent;
if (parent->l == NULL)
{
parent = parent->r;
}
else if (parent->r == NULL)
{
parent = parent->l;
}
free(temp);
}
return Balance(parent);


}




int main()
{
Avl_Tree avltree;
avl *root = NULL;
root = avltree.insert(root,30);
/*
and so on
*/
}
Last edited on
In your insert function, r is a NULL pointer, so doesn't have d, l, r members. You need to allocate some new node on the heap with new (or malloc).
Thanks for the help.I've fixed it and nowit works perfetly.
Topic archived. No new replies allowed.