Article View: comp.lang.c
Article #36Re: AVL tree insertion - programming exercise
From: Tim Rentsch
Date: Wed, 07 Oct 2026 19:13
Date: Wed, 07 Oct 2026 19:13
67 lines
3348 bytes
3348 bytes
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.
Technical Details
Message-ID:
<865wzdl3xz.fsf@linuxsc.com>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>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: Tim Rentsch <tr.17687@z991.linuxsc.com> Newsgroups: comp.lang.c Subject: Re: AVL tree insertion - programming exercise Date: Wed, 07 Oct 2026 12:13:12 -0700 Organization: A noiseless patient Spider Lines: 67 Message-ID: <865wzdl3xz.fsf@linuxsc.com> References: <86ece2kppv.fsf@linuxsc.com> <11a5h8m$2t8e8$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Wed, 07 Oct 2026 19:13:13 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3329622"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19mcDhBFPDZ796U4EzsqUWJ+/qzqgQuJ54="; posting-host="f116a0ff36505fb6c1fca8f439768e04" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:JjWwO9hsOwmtIVaE5Z2LzDgpPUs= sha1:GAVKGjA/hItT/eki687iRgsAel0= sha256:CautXYvpgT89JSMgQBJsQn6atn3HGls2s70G5+9ou9c= sha1:LacQa1tEsY6KmUGr0RyNxZV3K0g= sha256:g5IWnfVRJHG110wYzAjKOfvwrpa5BAGDnrzEiuL28Rc=