Help with Parent Pointer in Recursive AVL Tree

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bnchs
    New Member
    • Mar 2007
    • 9

    #1

    Help with Parent Pointer in Recursive AVL Tree

    This is C code. I am trying to fill each node's Parent field with its parent because I am drawing nodes to the screen. However, I have not been able to get this working. Reading the output from the code and tracing it, it seems that the code does not continue recursing down into the tree to insert. Can someone please point me to what is wrong and help me understand why it is wrong? Thank you.

    Code:
    struct AvlNode* Insert( gint X, struct AvlNode* T )
    {          
       static struct AvlNode *Parent;
       static struct AvlNode *Root;
       static int level = 0;
       
       if(Parent && T != NULL)
         printf("\n\nThe Root is %d and the Parent is %d", T->Element, Parent->Element);
       
         if(T!= NULL)
         printf(" But T is %d \n", T->Element);
       
       
       if( T == NULL )
       {
            /* Create and return a one-node tree */
            T = g_malloc( sizeof( struct AvlNode ) );
         
            if( T == NULL )
              printf( "Error: Out of space!" );
             
            else
            {                     
                T->Element = X; T->Height = 0;
                T->Left = T->Right = NULL;
               
                //Special Case for Root Node
                if(FIRST_TIME)
                {             
                  T->rect_x = app.drawing_area->allocation.width/2 - 25;
                  T->rect_y = 0;      
         
                  draw_node(T, app);
         
                  Root = T;
                  FIRST_TIME = 0;
                }
                
             }
        }
               
        else
           if( X < T->Element )
           {
               printf("\n%d is less than %d\n", X, T->Element);
               
               Parent = T;
               
               ++level; 
                
               T = Insert( X, T->Left );
               
               --level; //Finished Insert Call, decrement recursive level
               
               if(level == 0)
               T->Parent = Parent;
                     
              //Draw T's Left Node to Screen
              LEFT_NODE = 1;
              T->rect_x = T->Parent->rect_x;
              T->rect_y = T->Parent->rect_y;
              
              draw_node(T, app);
              LEFT_NODE = 0;
              
                    
                if( Height( T->Left ) - Height( T->Right ) == 2 )
                {
                    printf("Performing Left Rebalancing\n");
                    
                    if( X < ( (T->Left)->Element ) )
                        T = SingleRotateWithLeft( T );
                        
                    else
                      T = DoubleRotateWithLeft( T );
                }
            }
     
    
            else
              if( X > T->Element )
              {
                printf("\n%d is greater than %d\n", X, T->Element);
              
                 Parent = T;
                 ++level;
              
                 T = Insert( X, T->Right );
              
              
                 --level;
              
                 if(level == 0)
                 T->Parent = Parent;
    
    
                 //Draw Right Node
                 RIGHT_NODE = 1;
                 T->rect_x = T->Parent->rect_x;
                 T->rect_y = T->Parent->rect_y;
            
                    
                 //Draw T's Right Node to Screen
                 draw_node(T, app);
                 RIGHT_NODE = 0;
               
                     
                 if( Height( T->Right ) - Height( T->Left ) == 2 )
                 {  
                      
                      printf("Performing Right Rebalancing\n");
                      if( X > ( (T->Right)->Element) ) 
                            T = SingleRotateWithRight( T );
                      
                      else
                        T = DoubleRotateWithRight( T );
                 }    
                 
                 
            }
    
      
       /* Else X is in the tree already; we'll do nothing */
       T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1;
      
       
       return T;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Havwe you stepped through this with your debugger or are you relying on those printf() statements?

    A debugger session should find your problem right away.

    Comment

    Working...