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. -
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;
-
}
-
1 3798
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Sims |
last post by:
Hi
I have a table with something like
ID PARENT
0 | -1
1 | -1
2 | 1
3 | 1
|
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
|
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
|
by: Ying Yang |
last post by:
<snippet>
class Node1
{
Node2* link;
public:
void insertNode2(Node2* innerNode)
{
link->getNext() = innerNode; //***error -- non-lvalue in
|
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:
******************************************
| |
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...
|
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
|
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
|
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
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |