AVL insert

Hello Guys, I am assigned an AVL tree to write and I'm stuck on the insert!!

The function I'm writing is recursive but I get stuck when I do the very first recursive call.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Atree::Node *Atree::insert(int data, bool taller, Node *root)
{
  if (m_root == NULL)
  {
    m_root = new Node(data,root);
    taller = true;
  }
 else if (root != NULL)
  {
   if(data == m_root->m_data)
   {
     duplicate_error(data);
     taller = false;
   }
   if(data < m_root->m_data)
   {
     if(insert(data,taller,m_root->m_left))
     {
       cout<<m_root->m_left->m_data << endl;
     }
   }
  }
}


The Debugger found:
1
2
3
4
5
6
7
Program received signal SIGSEGV, Segmentation fault.
0x000000000040147d in Atree::insert (this=0x7fffffffde60, data=100, taller=false, root=0x605260) at avl.cpp:90
90             cout<<m_root->m_left->m_data << endl;
(gdb) where
#0  0x000000000040147d in Atree::insert (this=0x7fffffffde60, data=100, taller=false, root=0x605260) at avl.cpp:90
#1  0x0000000000401259 in Atree::add (this=0x7fffffffde60, data=@0x7fffffffde8c) at avl.cpp:31
#2  0x0000000000400eba in main (argc=2, argv=0x7fffffffe208) at avlmain.cpp:50 


the cout is for testing purpose that it inserted the <100> into the left pointer of m_root..
what is getting me is the recursion. Thanks in advance
any help would be appreciated ! :)
The "this" pointer in the debug output looks a bit suspicious to me. Did you call the insert(...) function with something like
p->insert(100, false, ???)
where p is a dangling pointer?
Topic archived. No new replies allowed.