🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #52

Re: AVL tree insertion - programming exercise

#52
From: BGB
Date: Thu, 08 Oct 2026 01:10
152 lines
4985 bytes
On 10/7/2026 2:13 PM, Tim Rentsch wrote:
> fir <profesor.fir@gmail.com> writes:
> 
>> Tim Rentsch pisze:
>>
>>> I offer this small programming exercise for anyone who is
>>> interested.
>>>
>>> Write a function to insert a value into an AVL tree.  Here
>>> is an outline of the basic data structure involved:
>>>
>>>       #include <stdint.h>
>>>
>>>       typedef uintptr_t Link;
>>>       typedef signed int Key;
>>>
>>>       typedef struct {
>>>           Link sons[2];
>>>           Key  key;
>>>       } Node;
>>>
>>>       static Node *
>>>       as_tree( Link link ){
>>>           return  (Node*)(link & ~(0?link:1));
>>>       }
>>>
>>>       static _Bool
>>>       is_tall( Link link ){
>>>           return  link & 1;
>>>       }
>>>
>>> A Node is an element in the tree, with two child pointers, the
>>> sons[2] array.  These links are basically pointers, but held
>>> as uintptr_t so they can take one of the bits to mean "tall",
>>> which means taller than the other subtree of the node.  The
>>> tree structure should maintain an invariant that at most one
>>> of the two sons of each node is "tall", and if set then the
>>> height of that subtree is one larger than the height of the
>>> other subtree.  Of course if both subtrees are the same height
>>> then neither should be labeled as tall.
>>>
>>> What we're looking for is a function to add a key to an existing
>>> tree (possibly empty), which is held as a Node *.  Example:
>>>
>>>       Node *root = 0;
>>>
>>>       add_key( &root, 1 );
>>>       add_key( &root, 2 );
>>>       add_key( &root, 3 );
>>>       add_key( &root, 4 );
>>>       add_key( &root, 5 );
>>>
>>> should leave root as a reference to a tree with five values.
>>>
>>> Thus the exercise is to define the function
>>>
>>>       void add_key( Node **, Key );
>>>
>>> to add values to the AVL tree held in what the first argument
>>> points to.
>>
>> im not interested in this personally but you may take my ai answer
>> if you want [...]
> 
> My interest here is to see how people would write the code.  I
> know how to program an AVL tree and don't have any particular
> interest in looking at AI-generated code.  But thank you for the
> effort.


I guess I will weigh in, in something closer to my style, while mostly 
following the original pattern (untested):

typedef struct AVL_Node_s AVL_Node;

struct AVL_Node_s {
   AVLNode *ln;
   AVLNode *rn;
   int key;
   char depth;
};

AVL_Node *AVL_AllocNode()
{
   AVL_Node *tmp;
   tmp=malloc(sizeof(AVL_Node));
   memset(tmp, 0, sizeof(AVL_Node));
   return(tmp);
}

int AVL_GetDepth(AVL_Node *n)
   { return(n?n->depth:(-1)); }

int AVL_RecalcDepth(AVL_Node *n)
{
   int dl, dr, d;
   if(!n)return(-1);
   dl=AVL_GetDepth(n->nl);
   dr=AVL_GetDepth(n->nr);
   d=max(dl, dr)+1;
   n->depth=d;
   return(d);
}

void AVL_AddKeyR(AVL_Node **rnr, int key)
{
   AVL_Node *n, *ln, *rn;
   int dl, dr;

   n=*rnr;
   if(!n)
   {
     n=AVL_AllocNode();
     n->key=key;
     *rnr=n;
     return;
   }
   if(n->key==key)
     return;
   if(key<n->key)
     AVL_AddKeyR(&n->ln, key);
   else
     AVL_AddKeyR(&n->rn, key);

   ln=n->ln;
   rn=n->rn;
   dl=AVL_GetDepth(ln);
   dr=AVL_GetDepth(rn);
   if(dl>(dr+1))
   {
     /* rotate tree */
     n->ln=ln->rn;
     ln->rn=n;
     AVL_RecalcDepth(n);
     AVL_RecalcDepth(ln);
     *rnr=ln;
     return;
   }
   if(dr>(dl+1))
   {
     /* rotate tree */
     n->rn=rn->ln;
     rn->ln=n;
     AVL_RecalcDepth(n);
     AVL_RecalcDepth(rn);
     *rnr=rn;
     return;
   }
   AVL_RecalcDepth(n);
}

Technical Details
Message-ID: <11a6qi4$3cv2b$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <86ece2kppv.fsf@linuxsc.com> <11a5h8m$2t8e8$1@dont-email.me> <865wzdl3xz.fsf@linuxsc.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: BGB <cr88192@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: AVL tree insertion - programming exercise
Date: Wed, 7 Oct 2026 20:10:14 -0500
Organization: A noiseless patient Spider
Lines: 152
Message-ID: <11a6qi4$3cv2b$1@dont-email.me>
References: <86ece2kppv.fsf@linuxsc.com> <11a5h8m$2t8e8$1@dont-email.me>
 <865wzdl3xz.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 08 Oct 2026 01:10:29 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3570763"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+AEy1mNGO8aTiaFd154M2avXUH0ualFcs="; posting-host="fea6bc69bd1c56a4128644838748e144"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Y3TDBodAOnm4LBA2VTe/uNmtajs= sha256:Uy9kVSHgvcowmkrVrmHubMgHUnYXmM5f3gwGlTZc1KI=
	sha1:vT1VA3SbTKk21IR07jlxO7BQFFY= sha256:Nop47T3YQnfLE2FKiBiH1vdi0RUpsakhiBDTtMrn6HU=
Content-Language: en-US
In-Reply-To: <865wzdl3xz.fsf@linuxsc.com>