Article View: comp.lang.c
Article #37Re: AVL tree insertion - programming exercise
From: fir
Date: Wed, 07 Oct 2026 19:28
Date: Wed, 07 Oct 2026 19:28
84 lines
4114 bytes
4114 bytes
Tim Rentsch pisze:
> 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.
>
well i answered for two reasons...
1) this grooup is to small and there are not many people who want do
some engaging task in small group of people (this not stops me to
posting, but just becouse if im focused on topic i often had something
to say)
2) ai is tremendously good thig epecially in programing also in c
programming and i find it good to encourage people to use it - as i
suspect some could still not use it and not fully realize how its great
for stright answer i got no time..
Technical Details
Message-ID:
<11a66hg$35t55$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> <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: fir <profesor.fir@gmail.com> Newsgroups: comp.lang.c Subject: Re: AVL tree insertion - programming exercise Date: Wed, 7 Oct 2026 21:28:48 +0200 Organization: A noiseless patient Spider Lines: 84 Message-ID: <11a66hg$35t55$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: Wed, 07 Oct 2026 19:28:49 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3339429"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+IKWH3yACjRMntNZO12f8OW/fbpP2IRA="; 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:a2TIa5fasoOZAOURgtXyVCmxVYc= sha256:TnP07Mj2TwGYTLcrWKV7kSlGp1frhYiYpA2sl+m8xfI= sha1:dGhTgFemDdkAUhCUnJzq9ii5Jsg= sha256:5WJ6T+Pe5n4pdDgqDJUAdUN3xgtxuJCIosFUDGz2xZk= In-Reply-To: <865wzdl3xz.fsf@linuxsc.com>