Article View: comp.lang.c
Article #30Re: AVL tree insertion - programming exercise
From: fir
Date: Wed, 07 Oct 2026 13:25
Date: Wed, 07 Oct 2026 13:25
329 lines
7504 bytes
7504 bytes
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
#include <stdint.h>
#include <stdlib.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 & ~(uintptr_t)1);
}
static int is_tall(Link link)
{
return (int)(link & 1);
}
static Link make_link(Node *p,int tall)
{
return (Link)p | (Link)tall;
}
/*
balance:
-1 = left subtree is taller
0 = equal
+1 = right subtree is taller
*/
static int balance(Node *n)
{
if(is_tall((*n).sons[0])) return -1;
if(is_tall((*n).sons[1])) return 1;
return 0;
}
static void set_balance(Node *n,int b)
{
(*n).sons[0]&=~(Link)1;
(*n).sons[1]&=~(Link)1;
if(b<0)
(*n).sons[0]|=1;
else if(b>0)
(*n).sons[1]|=1;
}
/* right rotation
x y
/ \ / \
y d -> a x
/ \ / \
a c c d
*/
static Node *rotate_right(Node *x)
{
Node *y=as_tree((*x).sons[0]);
Node *c=as_tree((*y).sons[1]);
(*x).sons[0]=make_link(c,0);
(*y).sons[1]=make_link(x,0);
set_balance(x,0);
set_balance(y,0);
return y;
}
/* left rotation
x y
/ \ / \
a y -> x d
/ \ / \
c d a c
*/
static Node *rotate_left(Node *x)
{
Node *y=as_tree((*x).sons[1]);
Node *c=as_tree((*y).sons[0]);
(*x).sons[1]=make_link(c,0);
(*y).sons[0]=make_link(x,0);
set_balance(x,0);
set_balance(y,0);
return y;
}
/*
Left-right rotation.
x z
/ \ / \
y d -> y x
/ \ / \ / \
a z a b c d
/ \
b c
*/
static Node *rotate_left_right(Node *x)
{
Node *y=as_tree((*x).sons[0]);
Node *z=as_tree((*y).sons[1]);
Node *b=as_tree((*z).sons[0]);
Node *c=as_tree((*z).sons[1]);
int bz=balance(z);
(*y).sons[1]=make_link(b,0);
(*x).sons[0]=make_link(c,0);
(*z).sons[0]=make_link(y,0);
(*z).sons[1]=make_link(x,0);
set_balance(y,bz==1 ? -1 : 0);
set_balance(x,bz==-1 ? 1 : 0);
set_balance(z,0);
return z;
}
/*
Right-left rotation.
x z
/ \ / \
a y -> x y
/ \ / \ / \
z d a b c d
/ \
b c
*/
static Node *rotate_right_left(Node *x)
{
Node *y=as_tree((*x).sons[1]);
Node *z=as_tree((*y).sons[0]);
Node *b=as_tree((*z).sons[0]);
Node *c=as_tree((*z).sons[1]);
int bz=balance(z);
(*y).sons[0]=make_link(c,0);
(*x).sons[1]=make_link(b,0);
(*z).sons[0]=make_link(x,0);
(*z).sons[1]=make_link(y,0);
set_balance(x,bz==1 ? -1 : 0);
set_balance(y,bz==-1 ? 1 : 0);
set_balance(z,0);
return z;
}
static Node *insert_node(Node *n,Key key,int *grew)
{
if(!n)
{
n=(Node*)calloc(1,sizeof(Node));
(*n).key=key;
*grew=1;
return n;
}
int d;
if(key<(*n).key)
d=0;
else if(key>(*n).key)
d=1;
else
{
*grew=0;
return n;
}
Node *child=as_tree((*n).sons[d]);
child=insert_node(child,key,grew);
if(!*grew)
return n;
(*n).sons[d]=make_link(child,is_tall((*n).sons[d]));
int b=balance(n);
if(d==0)
b--;
else
b++;
if(b==0)
{
set_balance(n,0);
*grew=0;
return n;
}
if(b==-1 || b==1)
{
set_balance(n,b);
*grew=1;
return n;
}
/* left heavy */
if(b==-2)
{
Node *y=as_tree((*n).sons[0]);
if(balance(y)<=0)
{
*grew=0;
return rotate_right(n);
}
*grew=0;
return rotate_left_right(n);
}
/* right heavy */
if(b==2)
{
Node *y=as_tree((*n).sons[1]);
if(balance(y)>=0)
{
*grew=0;
return rotate_left(n);
}
*grew=0;
return rotate_right_left(n);
}
return n;
}
void add_key(Node **root,Key key)
{
int grew=0;
*root=insert_node(*root,key,&grew);
}
ai also adds:
With:
Node *root=0;
add_key(&root,1);
add_key(&root,2);
add_key(&root,3);
add_key(&root,4);
add_key(&root,5);
you will get a properly balanced AVL tree.
One important thing: using bit 1 in Link assumes that malloc/calloc
returns addresses aligned to at least 2 bytes, which is safe on normal
platforms. as_tree() clears this bit before using the pointer.
Technical Details
Message-ID:
<11a5h8m$2t8e8$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-mailReferences:
<86ece2kppv.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: fir <profesor.fir@gmail.com> Newsgroups: comp.lang.c Subject: Re: AVL tree insertion - programming exercise Date: Wed, 7 Oct 2026 15:25:39 +0200 Organization: A noiseless patient Spider Lines: 329 Message-ID: <11a5h8m$2t8e8$1@dont-email.me> References: <86ece2kppv.fsf@linuxsc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 07 Oct 2026 13:25:43 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3056072"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX187bDDgffCVewwlOf9QtDZRis8Jjp0elQQ="; posting-host="f07c9b093bd94440653930cbb1e1ee13" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0 SeaMonkey/2.53.24 Cancel-Lock: sha1:9hIwjAK82tCu/mIVVjWVkFG69yA= sha256:qAphNw5QDq+BT/0uyBAw64QoWE42Vujbypa+h1sv/eE= sha1:wibLI/b2nrjjSn843odp1fmWihw= sha256:SDIpOdLI4xpgCqX4+KONT5utg0oqHzL02VcgHAKTPr0= In-Reply-To: <86ece2kppv.fsf@linuxsc.com>