473,699 Members | 2,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Parent Pointer in Recursive AVL Tree

9 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. struct AvlNode* Insert( gint X, struct AvlNode* T )
  2. {          
  3.    static struct AvlNode *Parent;
  4.    static struct AvlNode *Root;
  5.    static int level = 0;
  6.  
  7.    if(Parent && T != NULL)
  8.      printf("\n\nThe Root is %d and the Parent is %d", T->Element, Parent->Element);
  9.  
  10.      if(T!= NULL)
  11.      printf(" But T is %d \n", T->Element);
  12.  
  13.  
  14.    if( T == NULL )
  15.    {
  16.         /* Create and return a one-node tree */
  17.         T = g_malloc( sizeof( struct AvlNode ) );
  18.  
  19.         if( T == NULL )
  20.           printf( "Error: Out of space!" );
  21.  
  22.         else
  23.         {                     
  24.             T->Element = X; T->Height = 0;
  25.             T->Left = T->Right = NULL;
  26.  
  27.             //Special Case for Root Node
  28.             if(FIRST_TIME)
  29.             {             
  30.               T->rect_x = app.drawing_area->allocation.width/2 - 25;
  31.               T->rect_y = 0;      
  32.  
  33.               draw_node(T, app);
  34.  
  35.               Root = T;
  36.               FIRST_TIME = 0;
  37.             }
  38.  
  39.          }
  40.     }
  41.  
  42.     else
  43.        if( X < T->Element )
  44.        {
  45.            printf("\n%d is less than %d\n", X, T->Element);
  46.  
  47.            Parent = T;
  48.  
  49.            ++level; 
  50.  
  51.            T = Insert( X, T->Left );
  52.  
  53.            --level; //Finished Insert Call, decrement recursive level
  54.  
  55.            if(level == 0)
  56.            T->Parent = Parent;
  57.  
  58.           //Draw T's Left Node to Screen
  59.           LEFT_NODE = 1;
  60.           T->rect_x = T->Parent->rect_x;
  61.           T->rect_y = T->Parent->rect_y;
  62.  
  63.           draw_node(T, app);
  64.           LEFT_NODE = 0;
  65.  
  66.  
  67.             if( Height( T->Left ) - Height( T->Right ) == 2 )
  68.             {
  69.                 printf("Performing Left Rebalancing\n");
  70.  
  71.                 if( X < ( (T->Left)->Element ) )
  72.                     T = SingleRotateWithLeft( T );
  73.  
  74.                 else
  75.                   T = DoubleRotateWithLeft( T );
  76.             }
  77.         }
  78.  
  79.  
  80.         else
  81.           if( X > T->Element )
  82.           {
  83.             printf("\n%d is greater than %d\n", X, T->Element);
  84.  
  85.              Parent = T;
  86.              ++level;
  87.  
  88.              T = Insert( X, T->Right );
  89.  
  90.  
  91.              --level;
  92.  
  93.              if(level == 0)
  94.              T->Parent = Parent;
  95.  
  96.  
  97.              //Draw Right Node
  98.              RIGHT_NODE = 1;
  99.              T->rect_x = T->Parent->rect_x;
  100.              T->rect_y = T->Parent->rect_y;
  101.  
  102.  
  103.              //Draw T's Right Node to Screen
  104.              draw_node(T, app);
  105.              RIGHT_NODE = 0;
  106.  
  107.  
  108.              if( Height( T->Right ) - Height( T->Left ) == 2 )
  109.              {  
  110.  
  111.                   printf("Performing Right Rebalancing\n");
  112.                   if( X > ( (T->Right)->Element) ) 
  113.                         T = SingleRotateWithRight( T );
  114.  
  115.                   else
  116.                     T = DoubleRotateWithRight( T );
  117.              }    
  118.  
  119.  
  120.         }
  121.  
  122.  
  123.    /* Else X is in the tree already; we'll do nothing */
  124.    T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1;
  125.  
  126.  
  127.    return T;
  128. }
  129.  
Apr 6 '08 #1
1 3793
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Apr 6 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
2223
by: Sims | last post by:
Hi I have a table with something like ID PARENT 0 | -1 1 | -1 2 | 1 3 | 1
0
3446
by: ghiro | last post by:
as subiect says, i already know how to print a tree structure using a recursive function but what i want is to save the results in a multidimensiona array so i will be able to format them in a table with graphic etc the funciont that i am using to simple print the structure is: function display_children($parent, $level) { // retrieve all children of $parent
9
2802
by: JP SIngh | last post by:
Hi All I am trying to write a recursive function to list the managers and his employees in a tree like sctructure Manager 1 Emp1 Emp1.1 Emp 1.2 Emp 2
6
2532
by: Ying Yang | last post by:
<snippet> class Node1 { Node2* link; public: void insertNode2(Node2* innerNode) { link->getNext() = innerNode; //***error -- non-lvalue in
16
2668
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an option. Let's call the id of a class "cid" (for "class id"). The function signature should look like this: ******************************************
2
5305
by: dannielum | last post by:
Hi all, I am trying to write a Binary Search Tree that each of its node will have 3 node pointers: left, right and parent. I need a parent pointer for some the purpose of my project. Without the pointer to the parent node, the program will be inefficient and slow. It works fine at first. However, when I started to build the remove function, it destroys the tree when I delete a node. I already changed the parent pointer whenever I delete a...
4
9017
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working, even the removal, I just don't know how to keep track of the parent, so that I can set its child to the child of the node to be removed. IE - if I had C / \ B D
8
1753
by: jason | last post by:
Hello everyone, I am looking for an algorithm that would take an incremental value and map that to a case-inspecific alphanumeric string. However, I don't want the string to simply step through 0000, 0001 ... ZZZZ. I would ideally like the value to appear to jump around randomly, but still be traced back to an incrementing value. So, for example, while a simple standard mapping might look like: foo(1) => 0000
1
1336
by: none | last post by:
I'm trying to create a recursive function to evaluate the expressions within a list. The function uses eval() to evaluate the list. Like a lisp interpreter but very limited. What I'm looking for is a function to recursively traverse the list and provide answers in place of lists, so that ... Example = , ] Becomes: Example = Becomes: Example = 7 *Functions are defined in the script
0
8705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9197
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8941
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8897
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7785
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.