Thread View: comp.lang.c
114 messages
114 total messages
Page 1 of 3
Started by Tim Rentsch
Wed, 07 Oct 2026 06:08
Page 1 of 3 • 114 total messages
AVL tree insertion - programming exercise
Author: Tim Rentsch
Date: Wed, 07 Oct 2026 06:08
Date: Wed, 07 Oct 2026 06:08
55 lines
2734 bytes
2734 bytes
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.
Re: AVL tree insertion - programming exercise
Author: 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.
Re: AVL tree insertion - programming exercise
Author: 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.
Re: AVL tree insertion - programming exercise
Author: 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..
Re: AVL tree insertion - programming exercise
Author: BGB
Date: Thu, 08 Oct 2026 01:10
Date: Thu, 08 Oct 2026 01:10
152 lines
4985 bytes
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);
}
Re: AVL tree insertion - programming exercise
Author: Tim Rentsch
Date: Thu, 08 Oct 2026 12:31
Date: Thu, 08 Oct 2026 12:31
16 lines
1765 bytes
1765 bytes
fir <profesor.fir@gmail.com> writes: > Tim Rentsch pisze: [...] >> 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... > > [...] I appreciate you making an effort to contribute. Unfortunately any AI-generated code doesn't help with discussion I was hoping to see.
Re: AVL tree insertion - programming exercise
Author: Tim Rentsch
Date: Thu, 08 Oct 2026 12:56
Date: Thu, 08 Oct 2026 12:56
104 lines
5040 bytes
5040 bytes
BGB <cr88192@gmail.com> writes:
> 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):
>
> [.. code follows ..]
A few comments.
The posted code has a few typos. They were easy to fix.
Related to that, if you have some dyslexia, you might want to use
longer names for variables and members.
What you are calling "depth" in your code is customarily called
"height". The height of a (sub-)tree is the length of the longest
path from the root of the sub-tree to a leaf. For a node in a tree,
the depth of the node is the length of the path from the root of the
tree to that node. So height measures "down" whereas depth measures
"up".
I have run some test cases and the code looks like it produces trees
that are well-structured. I didn't do any tests to check for
whether the trees produced are balanced.
Unfortunately what the code is doing is not an AVL tree. AVL trees
have the property that they store only two extra bits per node, not
a full height. Furthermore after an insertion the bits don't need
to be recalculated by walking the tree - new values are determined
based on local information rather than a tree walk calculation.
I learned about AVL trees from The Art of Computer Proggramming, by
Knuth. I'm sure there are other explanations available but I don't
have any other pointers to give you.
Re: AVL tree insertion - programming exercise
Author: BGB
Date: Thu, 08 Oct 2026 20:09
Date: Thu, 08 Oct 2026 20:09
232 lines
10224 bytes
10224 bytes
On 10/8/2026 7:56 AM, Tim Rentsch wrote:
> BGB <cr88192@gmail.com> writes:
>
>> 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):
>>
>> [.. code follows ..]
>
> A few comments.
>
> The posted code has a few typos. They were easy to fix.
>
> Related to that, if you have some dyslexia, you might want to use
> longer names for variables and members.
>
I don't really think I have dyslexia that I am aware of.
I do have ASD though (what was once called Asperger's, now apparently
classified as "level 2 autism"; where "level 1" is mostly the ones that
actually have life-skills and can mostly pass for "normal", *1).
I also do have an issue of "small fonts at 100% zoom on a 4K monitor"
means at times I fail to see or notice typos as easily (but then 200% UI
scaling defeats the point of having a 4K monitor, and 125% or 150%
causes many programs to look like blurry crap).
So, turning it all into a little bit of a dilemma sometimes.
Some other various sensory issues, but none that relate to typos.
This area is sort of an odd grab bag.
My personal list of oddities that I have noticed is probably not
something I will go into at the moment (and can turn into some whole
"nature of the experience as my existence as myself" kind of thing).
*1:
Well, and also some form of affective alexithymia, but this doesn't
really effect coding skills. Not great for social stuff though, and I
have gotten a non-zero number of "Mr. Spock" jokes and similar over the
years; as apparently my way of speaking and self-presentation do come
off kinda like the Vulcans from Star Trek, but with a combination of
both ASD associated vocal inflection patterns but also a tendency to
speak in monotone, which isn't always beneficial for social
interactions; and seems to frequently lead to avoidance.
Well, it seems to vary (sometimes "Mr. Spock", sometimes "Sheldon").
There are some worse labels people could try to apply, but at least from
my own looking into them, they would seem not to apply. Some ironically
in the category of: If one feels worried that they could apply, they
don't apply (because in the minds of the people with these defects, they
would not perceive them as being defects). None the less one can still
worry at times about how they are perceived by others. But, at the same
times, sometimes caution, hostility, or avoidance are the most rational
options from the others' perspective, so they can't be held at fault as
such (and trying to convince them otherwise would be what would be
expected of those for whom such labels would apply; as such leaving the
only valid option as accepting their hostility or avoidance as an
inevitability).
...
> What you are calling "depth" in your code is customarily called
> "height". The height of a (sub-)tree is the length of the longest
> path from the root of the sub-tree to a leaf. For a node in a tree,
> the depth of the node is the length of the path from the root of the
> tree to that node. So height measures "down" whereas depth measures
> "up".
>
> I have run some test cases and the code looks like it produces trees
> that are well-structured. I didn't do any tests to check for
> whether the trees produced are balanced.
>
OK.
As can be noted, I typed it out directly in the post, no testing
involved here.
Didn't notice some of the typos until after I posted it.
> Unfortunately what the code is doing is not an AVL tree. AVL trees
> have the property that they store only two extra bits per node, not
> a full height. Furthermore after an insertion the bits don't need
> to be recalculated by walking the tree - new values are determined
> based on local information rather than a tree walk calculation.
>
I usually stored the depth/height because it was cheaper than fully
recalculating it using a tree walk, and it makes it cheaper to run the
rebalance logic by caching it.
But, this is what I remembered an AVL tree as being, so this is what I
went with here.
I had understood it as being more about them being a balanced binary
tree with the goal of maintaining a +/- 1 balance for the left and right
sub-trees (as opposed to, say, an unbalanced binary tree).
The tag bit or "node coloring" approach was (I thought) a different type
of tree (like a red/black tree).
> I learned about AVL trees from The Art of Computer Proggramming, by
> Knuth. I'm sure there are other explanations available but I don't
> have any other pointers to give you.
I read about them at some point when I was younger, and used them
occasionally.
Often their use-cases compete with B-Trees and hash-tables, which I more
often use.
For better or worse I did use a similar tree structure for the
directories in one of my filesystem designs:
Allows faster lookup than linear search;
Has lower constant overhead and scales better than hash chaining;
Can give directory entries (mostly) in sorted order;
Less overkill than B-Trees in this case (*1).
*1: Using B-Trees for directories doesn't really beat a binary tree
until the directory is unreasonably large, and in this case the relative
space overhead of the tree structure was modest if compared with the
filename field and inode number.
Though, the FS in question only stored 48 bytes (UTF-8) per dirent
directly, and would switch to multi-part dirents (like VFAT) for longer
names. Personally I felt this less bad than either overly large
fixed-size dirents, or the variable-length dirents approach that EXTn used.
Though, after implementing it, I was left to debate whether the
complexity was worthwhile and whether I should have just gone with
linear search instead, but alas...
For the directories though, did relax the balancing to +/- 2, as I noted
that this would significantly reduce the number of node rotations during
insertions or deletions with only a minor effect on overall balance.
In this case, the code for dealing with adding or removing directory
entries became one of the more complicated parts of the filesystem.
Otherwise, it was a design sorta like a hybrid of EXT2 and NTFS, aiming
more for simplicity (rather then the excessive complexity and
over-engineering of NTFS).
...
Re: AVL tree insertion - programming exercise
Author: fir
Date: Thu, 08 Oct 2026 20:56
Date: Thu, 08 Oct 2026 20:56
18 lines
2212 bytes
2212 bytes
BGB pisze: > > I also do have an issue of "small fonts at 100% zoom on a 4K monitor" > means at times I fail to see or notice typos as easily (but then 200% UI > scaling defeats the point of having a 4K monitor, and 125% or 150% > causes many programs to look like blurry crap) its maybe kinda curious but if someone starts winapi app and not call SetProcessDPIAware then the program is like being tricked he works in lower resolution he really is (i not readed into it into details but i may somewhat guess thet the resolution he is tricked depends of windows scaling - the more scaling you get the more program is tricked he work in lower resolution) calling this dpi aware makes app is not tricked and reckognizes real resolution
Re: AVL tree insertion - programming exercise
Author: BGB
Date: Thu, 08 Oct 2026 22:29
Date: Thu, 08 Oct 2026 22:29
63 lines
3785 bytes
3785 bytes
On 10/8/2026 3:56 PM, fir wrote: > BGB pisze: >> >> I also do have an issue of "small fonts at 100% zoom on a 4K monitor" >> means at times I fail to see or notice typos as easily (but then 200% >> UI scaling defeats the point of having a 4K monitor, and 125% or 150% >> causes many programs to look like blurry crap) > > > its maybe kinda curious but if someone starts winapi app and not call > SetProcessDPIAware then the program is like being tricked he works in > lower resolution he really is > > (i not readed into it into details but i may somewhat guess thet the > resolution he is tricked depends of windows scaling - the more scaling > you get the more program is tricked he work in lower resolution) > > calling this dpi aware makes app is not tricked and reckognizes real > resolution Dunno... A lot of times, native Windows programs deal better with UI zoom. But stuff that uses GTK or similar, or renders UI via raw bitmaps, or that goes through an X11 translation layer, tends to look pretty much awful when UI zoom is used. But, I mostly end up accepting the comparably small text of default font sizes on a 4K monitor, even if at times it seems like my eyesight is not good enough to reliably see typos (particularly those involving similar looking characters). Checking: The tip of my index finger on my monitor covers roughly 5 lines of text. Measures my finder, roughly 0.570 inches, so around 0.114" per line of text, read from a distance a little longer than the length of my arm. Checking: Length of arm: 28 inches; Distance from head to monitor, roughly 36 inches. Not sure the font Thunderbird uses, but it is slightly smaller and more difficult to read than the 9pt Fixedsys I am using in my text editor (but alas, Windows programs don't just let you use Fixedsys for everything, they seemingly want to use thin line and narrow variable width fonts for pretty much everything, and will then reset settings to defaults whenever the next time they update if you try to change these settings). ... OTOH: I realized after posting that my mention of issues with alexithymia and social difficulties was probably a little much for the topic at hand (too serious of a tone). It is hard sometimes to maintain a good balance in these areas. ...
Re: AVL tree insertion - programming exercise
Author: Janis Papanagnou
Date: Thu, 08 Oct 2026 23:39
Date: Thu, 08 Oct 2026 23:39
127 lines
7238 bytes
7238 bytes
On 2026-10-08 22:09, BGB wrote:
> On 10/8/2026 7:56 AM, Tim Rentsch wrote:
>> [...]
>> A few comments.
>>
>> The posted code has a few typos. They were easy to fix.
>>
>> Related to that, if you have some dyslexia, you might want to use
>> longer names for variables and members.
>
> I don't really think I have dyslexia that I am aware of. [...]
[ extensive personal/medical explanations deleted ]
All I have to say on that is that I'd wish all people write as good
as you, especially given that there's a couple pathological persons
around WRT (beyond typos) their writing peculiarities.
>
>> What you are calling "depth" in your code is customarily called
>> "height". The height of a (sub-)tree is the length of the longest
>> path from the root of the sub-tree to a leaf. For a node in a tree,
>> the depth of the node is the length of the path from the root of the
>> tree to that node. So height measures "down" whereas depth measures
>> "up".
>>
>> I have run some test cases and the code looks like it produces trees
>> that are well-structured. I didn't do any tests to check for
>> whether the trees produced are balanced.
>
> OK.
>
> As can be noted, I typed it out directly in the post, no testing
> involved here.
>
> Didn't notice some of the typos until after I posted it.
>
>> Unfortunately what the code is doing is not an AVL tree. AVL trees
>> have the property that they store only two extra bits per node, not
>> a full height. Furthermore after an insertion the bits don't need
>> to be recalculated by walking the tree - new values are determined
>> based on local information rather than a tree walk calculation.
>
> I usually stored the depth/height because it was cheaper than fully
> recalculating it using a tree walk, and it makes it cheaper to run the
> rebalance logic by caching it.
>
> But, this is what I remembered an AVL tree as being, so this is what I
> went with here.
>
> I had understood it as being more about them being a balanced binary
> tree with the goal of maintaining a +/- 1 balance for the left and right
> sub-trees (as opposed to, say, an unbalanced binary tree).
Concerning the height/balance property you are completely right, and
all sources I inspected are speaking about balancing factors - and
these can be "derived" directly from the stored height information.
(The invariants have to be guaranteed after insertion and deletion.)
I think the previous misconception was that the OP mixed AVL-property,
the definition of what constitutes an AVL tree, with the implementation
decision, the concrete model.
Online I found (for various languages) of course code with the height
attribute. There's also code descriptions (e.g. a Wirth book) that use
-1..+1 (probably with an implicit temporary +/-2 overflow to prevent
in some implementations I've seen).
(N.B.: I'm unsure what the intention of the OP actually is when he
explains "My interest here is to see how people would write the code."
where he knows how to write an implementation. - From his reply to you
I suspect he just wants to teach people how to do things "right" as to
his judgement. - Implementing such code in "C", so I'd suppose, would
also not show any fundamental new insights; most people here are more
or less "C" experts. - We need a 'struct' for the node, the attributes
key, some height/balance, and two pointers to nodes. The AVL-algorithms
are simple to copy/transfer from any book explaining this type of tree.
So what insights are to be expected? - It would have been helpful, as
so often, if the OP would have been less obscure and just say what he
concretely wants to learn from the responses.)
>
> The tag bit or "node coloring" approach was (I thought) a different type
> of tree (like a red/black tree).
RB-trees are like AVL-trees binary trees with dynamic adaption to not
let them degenerate arbitrarily but to guarantee some depth properties
(to thus get better access times). The AVL criterion is stricter. The
red/black color tags serve a similar purpose as the AVL-balance factor.
(I looked up my written notes from the lectures of R. Bayer back then;
he invented the B-trees and the RB-trees by another name ("symmetric
binary B-trees"). But RB-trees were obviously not part of his course
back then. Though I see that the Wikipedia entries are also okay.)
>
>> I learned about AVL trees from The Art of Computer Proggramming, by
>> Knuth. I'm sure there are other explanations available but I don't
>> have any other pointers to give you.
N. Wirth, Ottmann/Widmayer, Denert/Franck (all about data-structures),
are some sources I have that explain them. And de.wikipedia.org and
web-searches also provide both, descriptions and concrete source code.
>
> I read about them at some point when I was younger, and used them
> occasionally.
>
> Often their use-cases compete with B-Trees and hash-tables, which I more
> often use.
While balanced trees and hash-tables are used for the implementation of
dictionary-like [semantical] data structures both are very different in
their properties and implementation; they don't quite compare per se.
B-trees (and B*-trees) have own different properties (and thus different
typical application cases). I won't expand on that further here.
BTW, I had intended to write (for another language than "C") an AVL-tree
implementation. (Just finished it, but yet it needs verification of the
AVL-invariants that I intended to add.) It also uses a height attribute
and a balance function (based on the height attributes) to determine the
necessary tree-balancing operations. - The OP's post at least made me
overcome my laziness and implement that beast. :-)
Janis
> [...]
Re: AVL tree insertion - programming exercise
Author: Tim Rentsch
Date: Fri, 09 Oct 2026 06:53
Date: Fri, 09 Oct 2026 06:53
119 lines
5581 bytes
5581 bytes
BGB <cr88192@gmail.com> writes:
> On 10/8/2026 7:56 AM, Tim Rentsch wrote:
>
>> BGB <cr88192@gmail.com> writes:
>>
>>> 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):
>>>
>>> [.. code follows ..]
>>
>> A few comments.
>>
>> The posted code has a few typos. They were easy to fix.
>>
>> Related to that, if you have some dyslexia, you might want to use
>> longer names for variables and members.
>
> I don't really think I have dyslexia that I am aware of.
I should add, nothing negative intended. Some of the best
developers I have met were dyslexic.
>> Unfortunately what the code is doing is not an AVL tree. AVL trees
>> have the property that they store only two extra bits per node, not
>> a full height. Furthermore after an insertion the bits don't need
>> to be recalculated by walking the tree - new values are determined
>> based on local information rather than a tree walk calculation.
>
> I usually stored the depth/height because it was cheaper than
> fully recalculating it using a tree walk, and it makes it cheaper
> to run the rebalance logic by caching it.
>
> But, this is what I remembered an AVL tree as being, so this is
> what I went with here.
>
> [...]
My intention was that people keep the interface I gave, including
the definition of the Node struct.
>> I learned about AVL trees from The Art of Computer Proggramming,
>> by Knuth. I'm sure there are other explanations available but I
>> don't have any other pointers to give you.
>
> I read about them at some point when I was younger, and used them
> occasionally. [...]
I encourage you to read the description in Knuth TAOCP.
Re: AVL tree insertion - programming exercise
Author: Tim Rentsch
Date: Fri, 09 Oct 2026 07:03
Date: Fri, 09 Oct 2026 07:03
24 lines
2241 bytes
2241 bytes
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes: > On 2026-10-08 22:09, BGB wrote: > >> On 10/8/2026 7:56 AM, Tim Rentsch wrote: >> >>> [...] >>> A few comments. >>> >>> The posted code has a few typos. They were easy to fix. [...] > (N.B.: I'm unsure what the intention of the OP actually is when > he explains "My interest here is to see how people would write the > code." where he knows how to write an implementation. I meant what I said and I said what I meant. And not more than that. > - From his reply to you I suspect he just wants to teach people > how to do things "right" as to his judgement. [...] You're wrong. My interest here is in learning, not teaching.
Re: AVL tree insertion - programming exercise
Author: fir
Date: Fri, 09 Oct 2026 07:56
Date: Fri, 09 Oct 2026 07:56
51 lines
3758 bytes
3758 bytes
BGB pisze: > On 10/8/2026 3:56 PM, fir wrote: >> BGB pisze: >>> >>> I also do have an issue of "small fonts at 100% zoom on a 4K monitor" >>> means at times I fail to see or notice typos as easily (but then 200% >>> UI scaling defeats the point of having a 4K monitor, and 125% or 150% >>> causes many programs to look like blurry crap) >> >> >> its maybe kinda curious but if someone starts winapi app and not call >> SetProcessDPIAware then the program is like being tricked he works in >> lower resolution he really is >> >> (i not readed into it into details but i may somewhat guess thet the >> resolution he is tricked depends of windows scaling - the more scaling >> you get the more program is tricked he work in lower resolution) >> >> calling this dpi aware makes app is not tricked and reckognizes real >> resolution > > Dunno... > > A lot of times, native Windows programs deal better with UI zoom. > > But stuff that uses GTK or similar, or renders UI via raw bitmaps, or > that goes through an X11 translation layer, tends to look pretty much > awful when UI zoom is used. > > But, I mostly end up accepting the comparably small text of default font > sizes on a 4K monitor, even if at times it seems like my eyesight is not > good enough to reliably see typos (particularly those involving similar > looking characters). > those aps are probably assumed to be run in low res... windows realizes it (i guess) and it tricks the aplications they are runing in low res (like rea desktop res/scaling probably) im writing on blitter so its in fact no problem to chnge resolution of this inner bitmap/bitsvreen area even in runtime..i can increase it and decrease in runtime like setting it to 320 pixels or 3200 pixels and then it is mapped on desktop i can also recreate fonts with those size changes and just use frame_size_y/25 font height so fonts are also proper size (small problem is the font creating api needs font height to be integer so there are not fluid changes but ugly jumping infont sizes..but then i need to worry to rescale those fonts probably on my side and i could eventually worry it also wouldnt look very fluid - i would need to check)
Re: AVL tree insertion - programming exercise
Author: David Brown
Date: Fri, 09 Oct 2026 08:34
Date: Fri, 09 Oct 2026 08:34
44 lines
3622 bytes
3622 bytes
On 09/10/2026 08:53, Tim Rentsch wrote: > BGB <cr88192@gmail.com> writes: > >> On 10/8/2026 7:56 AM, Tim Rentsch wrote: > >>> I learned about AVL trees from The Art of Computer Proggramming, >>> by Knuth. I'm sure there are other explanations available but I >>> don't have any other pointers to give you. >> >> I read about them at some point when I was younger, and used them >> occasionally. [...] > > I encourage you to read the description in Knuth TAOCP. I would not recommend that, any more than I would recommend "The C Programming Language" to someone wanting to learn C. Knuth's detailed mathematical treatment of algorithms is second to none, and his contribution to computer science is legendary. His writing style is wonderful, and I thoroughly enjoyed the TeXbook (though it left me with a serious disability - I am now unable to read text without being annoyed at typographic flaws or inconsistencies!). But it is /not/ a book you use to learn a new topic. It is not a book you use to learn about AVL trees (it does not even mention them by that name), either in terms of what they are used for, how they work, or how they may be implemented. You will learn more that is relevant to you from an animated gif on the Wikipedia page than from reading the entire chapter in "Fundamental Algorithms". If you want to understand how you can prove the algorithmic complexities of different tree structures and their algorithms, and to better understand how to prove the tree invariants, and to better understand how to make your own interesting tree structures - /then/ studying that part Knuth's book could be useful. It is not a book you read, it is something you have to study. And like "The C Programming Language", it suffers from age - you can't learn modern C from a 50 year old book, and you can't learn modern coding from a 60 year old book. Far and away the biggest mistake of the books - IMHO - is the use of a mythical assembly as the language for the programs instead of a high-level pseudo-code that made the interesting stuff clear instead of bogging it down in irrelevant detail.
Re: AVL tree insertion - programming exercise
Author: fir
Date: Fri, 09 Oct 2026 09:12
Date: Fri, 09 Oct 2026 09:12
22 lines
2045 bytes
2045 bytes
Tim Rentsch pisze: > fir <profesor.fir@gmail.com> writes: > >> Tim Rentsch pisze: > [...] >>> 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... >> >> [...] > > I appreciate you making an effort to contribute. Unfortunately > any AI-generated code doesn't help with discussion I was hoping > to see. > i was never using any of "trees" of such kind - so i cant answer, never yet seen any usage of it
Re: AVL tree insertion - programming exercise
Author: fir
Date: Fri, 09 Oct 2026 09:12
Date: Fri, 09 Oct 2026 09:12
25 lines
2216 bytes
2216 bytes
fir pisze: > Tim Rentsch pisze: >> fir <profesor.fir@gmail.com> writes: >> >>> Tim Rentsch pisze: >> [...] >>>> 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... >>> >>> [...] >> >> I appreciate you making an effort to contribute. Unfortunately >> any AI-generated code doesn't help with discussion I was hoping >> to see. >> > > i was never using any of "trees" of such kind - so i cant answer, never > yet seen any usage of it > its also not practical to me to learn something other than what im just doing or at least close
Re: AVL tree insertion - programming exercise
Author: Janis Papanagnou
Date: Fri, 09 Oct 2026 10:27
Date: Fri, 09 Oct 2026 10:27
77 lines
5302 bytes
5302 bytes
On 2026-10-09 10:34, David Brown wrote: > On 09/10/2026 08:53, Tim Rentsch wrote: >> BGB <cr88192@gmail.com> writes: >>> On 10/8/2026 7:56 AM, Tim Rentsch wrote: >> >>>> I learned about AVL trees from The Art of Computer Proggramming, >>>> by Knuth. I'm sure there are other explanations available but I >>>> don't have any other pointers to give you. >>> >>> I read about them at some point when I was younger, and used them >>> occasionally. [...] >> >> I encourage you to read the description in Knuth TAOCP. > > I would not recommend that, any more than I would recommend "The C > Programming Language" to someone wanting to learn C. > > Knuth's detailed mathematical treatment of algorithms is second to none, > and his contribution to computer science is legendary. His writing > style is wonderful, and I thoroughly enjoyed the TeXbook (though it left > me with a serious disability - I am now unable to read text without > being annoyed at typographic flaws or inconsistencies!). (Hah! - And I thought I'm a rare case here, being [in my vicinity] the only one who seems to be suffering from bad typesetting. - I didn't need his book, though - actually I haven't read his book on TeX - since these bad feelings appeared as soon as MS word came out, and Ariel fonts, and similar typographical steps backwards. It's characteristic; you look at a book and can immediately make an educated guess whether it had been typeset using TeX, Nroff, MSWord, or something else.) > > But it is /not/ a book you use to learn a new topic. It is not a book > you use to learn about AVL trees (it does not even mention them by that > name), either in terms of what they are used for, how they work, or how > they may be implemented. You will learn more that is relevant to you > from an animated gif on the Wikipedia page than from reading the entire > chapter in "Fundamental Algorithms". > > If you want to understand how you can prove the algorithmic complexities > of different tree structures and their algorithms, and to better > understand how to prove the tree invariants, and to better understand > how to make your own interesting tree structures - /then/ studying that > part Knuth's book could be useful. It is not a book you read, it is > something you have to study. I very much agree on everything you wrote here. The unfortunate part of the story is that a lot of the literature on that (unless referring to the original authors of the AVL tree) is obviously [still] based on or inspired by Knuth's book. (One detail is the already mentioned representation of the balancing property. Even Wikipedia still shows that form, but at least they explicitly mention that they based their representation on Knuth and that using the height is an equivalent option.) > > And like "The C Programming Language", it suffers from age - you can't > learn modern C from a 50 year old book, and you can't learn modern > coding from a 60 year old book. Far and away the biggest mistake of the > books - IMHO - is the use of a mythical assembly as the language for the > programs instead of a high-level pseudo-code that made the interesting > stuff clear instead of bogging it down in irrelevant detail. Indeed. - It's many years that I had TAOCP in my hands but I seem to recall that he also wrote (in that specific language) everything in iterative form. That is especially bad in case of recursive data structures and algorithms - we recently discussed that here! - that are (IMHO) a lot clearer than any iterative code on tree structures. With the Internet resources we luckily have a rich source of more recent and refined information about that topic. (The problem there is only to sort the wheat from the chaff.) Janis
Re: AVL tree insertion - programming exercise
Author: bart
Date: Fri, 09 Oct 2026 10:43
Date: Fri, 09 Oct 2026 10:43
60 lines
4399 bytes
4399 bytes
On 09/10/2026 09:34, David Brown wrote: > On 09/10/2026 08:53, Tim Rentsch wrote: >> BGB <cr88192@gmail.com> writes: >> >>> On 10/8/2026 7:56 AM, Tim Rentsch wrote: >> >>>> I learned about AVL trees from The Art of Computer Proggramming, >>>> by Knuth. I'm sure there are other explanations available but I >>>> don't have any other pointers to give you. >>> >>> I read about them at some point when I was younger, and used them >>> occasionally. [...] >> >> I encourage you to read the description in Knuth TAOCP. > > I would not recommend that, any more than I would recommend "The C > Programming Language" to someone wanting to learn C. > > Knuth's detailed mathematical treatment of algorithms is second to none, > and his contribution to computer science is legendary. His writing > style is wonderful, and I thoroughly enjoyed the TeXbook (though it left > me with a serious disability - I am now unable to read text without > being annoyed at typographic flaws or inconsistencies!). > > But it is /not/ a book you use to learn a new topic. It is not a book > you use to learn about AVL trees (it does not even mention them by that > name), either in terms of what they are used for, how they work, or how > they may be implemented. You will learn more that is relevant to you > from an animated gif on the Wikipedia page than from reading the entire > chapter in "Fundamental Algorithms". > > If you want to understand how you can prove the algorithmic complexities > of different tree structures and their algorithms, and to better > understand how to prove the tree invariants, and to better understand > how to make your own interesting tree structures - /then/ studying that > part Knuth's book could be useful. It is not a book you read, it is > something you have to study. > > And like "The C Programming Language", it suffers from age - you can't > learn modern C from a 50 year old book, and you can't learn modern > coding from a 60 year old book. When I first looked at it wasn't quite that old! > Far and away the biggest mistake of the > books - IMHO - is the use of a mythical assembly as the language for the > programs instead of a high-level pseudo-code that made the interesting > stuff clear instead of bogging it down in irrelevant detail. This 'MIX' language was something that astonished me even then. Not only was it assembly that would totally obscure whatever algorithm was being expressed, but it was a weird made-up assembly with unusual byte and word sizes. I understand that more recent editions use an updated 'MMIX' language: now the registers are 64 bits, and there's a lot more of them. This is still like publishing a book of algorithms using ARM64 assembly to express them. It's not clear what he had against HLLs; perhaps he thought an actual HLL would soon be superseded? Then pseudo-code should have been used.
Re: AVL tree insertion - programming exercise
Author: David Brown
Date: Fri, 09 Oct 2026 10:59
Date: Fri, 09 Oct 2026 10:59
112 lines
7025 bytes
7025 bytes
On 09/10/2026 12:27, Janis Papanagnou wrote: > On 2026-10-09 10:34, David Brown wrote: >> On 09/10/2026 08:53, Tim Rentsch wrote: >>> BGB <cr88192@gmail.com> writes: >>>> On 10/8/2026 7:56 AM, Tim Rentsch wrote: >>> >>>>> I learned about AVL trees from The Art of Computer Proggramming, >>>>> by Knuth. I'm sure there are other explanations available but I >>>>> don't have any other pointers to give you. >>>> >>>> I read about them at some point when I was younger, and used them >>>> occasionally. [...] >>> >>> I encourage you to read the description in Knuth TAOCP. >> >> I would not recommend that, any more than I would recommend "The C >> Programming Language" to someone wanting to learn C. >> >> Knuth's detailed mathematical treatment of algorithms is second to >> none, and his contribution to computer science is legendary. His >> writing style is wonderful, and I thoroughly enjoyed the TeXbook >> (though it left me with a serious disability - I am now unable to read >> text without being annoyed at typographic flaws or inconsistencies!). > > (Hah! - And I thought I'm a rare case here, being [in my vicinity] > the only one who seems to be suffering from bad typesetting. - I > didn't need his book, though - actually I haven't read his book on > TeX - since these bad feelings appeared as soon as MS word came out, > and Ariel fonts, and similar typographical steps backwards. > It's characteristic; you look at a book and can immediately make an > educated guess whether it had been typeset using TeX, Nroff, MSWord, > or something else.) (MS Word destroyed the quality of the printed word by opening it to people with no training or guidance, and making it very easy to make poor-quality and inconsistent documents. I would not recommend anyone use plain TeX - but I can recommend the TeXbook. (Think of it like learning to understand assembly for a processor - it's interesting and useful to know, even though you usually want to use much higher level languages.) Lamport's "LaTeX: A Document Preparation System" is also highly readable, albeit a bit dated now - for actual document writing, modern descendents like LuaTeX are usually better.) > >> >> But it is /not/ a book you use to learn a new topic. It is not a book >> you use to learn about AVL trees (it does not even mention them by >> that name), either in terms of what they are used for, how they work, >> or how they may be implemented. You will learn more that is relevant >> to you from an animated gif on the Wikipedia page than from reading >> the entire chapter in "Fundamental Algorithms". >> >> If you want to understand how you can prove the algorithmic >> complexities of different tree structures and their algorithms, and to >> better understand how to prove the tree invariants, and to better >> understand how to make your own interesting tree structures - /then/ >> studying that part Knuth's book could be useful. It is not a book you >> read, it is something you have to study. > > I very much agree on everything you wrote here. > > The unfortunate part of the story is that a lot of the literature on > that (unless referring to the original authors of the AVL tree) is > obviously [still] based on or inspired by Knuth's book. (One detail > is the already mentioned representation of the balancing property. > Even Wikipedia still shows that form, but at least they explicitly > mention that they based their representation on Knuth and that using > the height is an equivalent option.) > Wikipedia is usually a good first start for this kind of thing. It has its limitations, of course, but typically you can start there and then move on to more detailed resources according to needs and interests. And Wikipedia is quite good at showing their references. >> >> And like "The C Programming Language", it suffers from age - you can't >> learn modern C from a 50 year old book, and you can't learn modern >> coding from a 60 year old book. Far and away the biggest mistake of >> the books - IMHO - is the use of a mythical assembly as the language >> for the programs instead of a high-level pseudo-code that made the >> interesting stuff clear instead of bogging it down in irrelevant detail. > > Indeed. - It's many years that I had TAOCP in my hands but I seem > to recall that he also wrote (in that specific language) everything > in iterative form. That is especially bad in case of recursive data > structures and algorithms - we recently discussed that here! - that > are (IMHO) a lot clearer than any iterative code on tree structures. > TAOCP (books 1 to 3) are on my bookshelf, along with all sorts of books of different vintages - but it is a very long time since I made any use of it. (My "The C Programming Language" should be there too, but I don't know where it has gone. My "The C++ Programming Language" is there - also very readable, also completely useless for learning modern C++.) > With the Internet resources we luckily have a rich source of more > recent and refined information about that topic. (The problem there > is only to sort the wheat from the chaff.) > Indeed. Websites can offer better graphics and animations than books, and much more scope for trying things out yourself - they can give far more than books for this kind of thing. But editorial and quality control is often sadly lacking.
Re: AVL tree insertion - programming exercise
Author: David Brown
Date: Fri, 09 Oct 2026 11:20
Date: Fri, 09 Oct 2026 11:20
55 lines
4045 bytes
4045 bytes
On 09/10/2026 12:43, bart wrote: > On 09/10/2026 09:34, David Brown wrote: >> And like "The C Programming Language", it suffers from age - you can't >> learn modern C from a 50 year old book, and you can't learn modern >> coding from a 60 year old book. > > When I first looked at it wasn't quite that old! I have the second edition from 1972 - the first edition was published in 1968, but started in 1962. I believe it would have been 1991 when I bought my copy - it was considered old-fashioned by then and not usable as course textbook. But having won a bookshop voucher as an academic prize, it seemed appropriate to spend some on the nearest computer science has to a bible. (I think I also bought a complete Shakespeare, which I still have, and from which I have actually read three or four plays.) > >> Far and away the biggest mistake of the books - IMHO - is the use of a >> mythical assembly as the language for the programs instead of a high- >> level pseudo-code that made the interesting stuff clear instead of >> bogging it down in irrelevant detail. > > This 'MIX' language was something that astonished me even then. Not only > was it assembly that would totally obscure whatever algorithm was being > expressed, but it was a weird made-up assembly with unusual byte and > word sizes. Indeed. When I first saw it, I was familiar with several 8-bit assemblies (6502, Z80, 8085) and some 32-bit ARM assembly, and had read books on the 68000 family and MIPS. MIX was, to me, absurd. To be fair, the computer world was not nearly as consolidated in 1962, but I'm sure it looked fairly odd even by the standards of the time. > I understand that more recent editions use an updated 'MMIX' language: > now the registers are 64 bits, and there's a lot more of them. This is > still like publishing a book of algorithms using ARM64 assembly to > express them. Yes, this strikes me as even more absurd. It's just re-doing the previous mistake in a slightly less bad way. > > It's not clear what he had against HLLs; perhaps he thought an actual > HLL would soon be superseded? Then pseudo-code should have been used. Real HLL's were around at the time - FORTRAN and ALGOL, or possibly LISP, would have been appropriate choices in the days before small letters were invented. Or he could have made a simple HLL pseudo-code for the purpose - things like unlimited sized integers and unlimited memory sizes would have made sense.
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Fri, 09 Oct 2026 12:38
Date: Fri, 09 Oct 2026 12:38
88 lines
5084 bytes
5084 bytes
In article <11aaimc$n0f0$2@dont-email.me>, David Brown <david.brown@hesbynett.no> wrote: >On 09/10/2026 12:43, bart wrote: >> [snip] >> This 'MIX' language was something that astonished me even then. Not only >> was it assembly that would totally obscure whatever algorithm was being >> expressed, but it was a weird made-up assembly with unusual byte and >> word sizes. > >Indeed. When I first saw it, I was familiar with several 8-bit >assemblies (6502, Z80, 8085) and some 32-bit ARM assembly, and had read >books on the 68000 family and MIPS. MIX was, to me, absurd. To be >fair, the computer world was not nearly as consolidated in 1962, but I'm >sure it looked fairly odd even by the standards of the time. If one were coming from the world of early 1960s mainframes, it would perhaps be less astonishing. >> I understand that more recent editions use an updated 'MMIX' language: >> now the registers are 64 bits, and there's a lot more of them. This is >> still like publishing a book of algorithms using ARM64 assembly to >> express them. > >Yes, this strikes me as even more absurd. It's just re-doing the >previous mistake in a slightly less bad way. Unfortunately, he is quite adamant about this. He truly believes that the best way to understand how an algorithm works on a computer is to see it at the instruction level. >> It's not clear what he had against HLLs; perhaps he thought an actual >> HLL would soon be superseded? Then pseudo-code should have been used. > >Real HLL's were around at the time - FORTRAN and ALGOL, or possibly >LISP, would have been appropriate choices in the days before small >letters were invented. Or he could have made a simple HLL pseudo-code >for the purpose - things like unlimited sized integers and unlimited >memory sizes would have made sense. The criticism of TAOCP is valid. It is not a book for workaday programmers; it is an academic reference. The real value is in the mathematical treatment and historical/bibliographic notes, not the presentation of code in assembler. Other books exist that are better references for most people; certainly for most programmers: * "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein is a standard. * Sedgewick's books in C, simply titled, "Algorithms" are pretty good, though his code style is slightly idiosyncratic. Now in its 4th Edition, he has switched to Java and taken on a coauthor (Kevin Wayne). * "Algorithms", by Dasgupta, Papadimitriou, and Vazirani is a wonderful book that, I think, strikes a nice balance between formality and utility. * I am rather partial to Skiena's work, "The Algorithm Design Manual." * "Data Structures and Their Algorithms" by Lewis and Denenberg is a bit dated, but very accessible; examples are presented in a Pascal-like pseudocode. * "Data Structures and Algorithms" by Aho, Hopcroft, and Ullman is a nice book. Be careful not to confuse it with Aho and Ullman's, "The Design and Analysis of Computer Algorithms", however; the latter is far denser and intended for academics studying the fundamental properties of algorithms. * "Algorithms + Data Structures = Programs" and the later revision, "Algorithms & Data Structures", by Wirth, are similarly dated, but still very nice. * "Purely Functional Data Structures", by Okasaki; I include this just because I think that it is fun. Of course, more specialized texts exist for specific problem domains, programming paradigms, etc; books on string algorithms, mutual exclusion protocols for concurrent programs, etc, are all available, though not general. Did Knuth err using MIX and then MMIX for TAOCP? Perhaps; I do think that neither Fortran nor Lisp would have been appropriate choices; Algol perhaps. Other authors around that time or slightly later either adopt a more mathematics-inspired pseudonotation (Dijkstra; Hoare) algolish pseudo-code (Aho et al), or a language of their own design (Wirth). Knuth is a wonderful object of study, but I do not recommend it. - Dan C.
Re: AVL tree insertion - programming exercise
Author: David Brown
Date: Fri, 09 Oct 2026 14:05
Date: Fri, 09 Oct 2026 14:05
124 lines
7070 bytes
7070 bytes
On 09/10/2026 14:38, Dan Cross wrote: > In article <11aaimc$n0f0$2@dont-email.me>, > David Brown <david.brown@hesbynett.no> wrote: >> On 09/10/2026 12:43, bart wrote: >>> [snip] >>> This 'MIX' language was something that astonished me even then. Not only >>> was it assembly that would totally obscure whatever algorithm was being >>> expressed, but it was a weird made-up assembly with unusual byte and >>> word sizes. >> >> Indeed. When I first saw it, I was familiar with several 8-bit >> assemblies (6502, Z80, 8085) and some 32-bit ARM assembly, and had read >> books on the 68000 family and MIPS. MIX was, to me, absurd. To be >> fair, the computer world was not nearly as consolidated in 1962, but I'm >> sure it looked fairly odd even by the standards of the time. > > If one were coming from the world of early 1960s mainframes, it > would perhaps be less astonishing. It would undoubtedly have been less astonishing in 1968 (when the first book was published) than in 1991 (when I first saw it), but I would have thought that even people familiar with 1960's mainframes would see it as strange to use a limited and highly specific artificial assembly that matched poorly with existing systems. I am sure people were already familiar with the pain of converting code written in assembly for one system into assembly for a very different system - the only advantage of the MIX code was that it was equally useless to everyone, and favoured no one! > >>> I understand that more recent editions use an updated 'MMIX' language: >>> now the registers are 64 bits, and there's a lot more of them. This is >>> still like publishing a book of algorithms using ARM64 assembly to >>> express them. >> >> Yes, this strikes me as even more absurd. It's just re-doing the >> previous mistake in a slightly less bad way. > > Unfortunately, he is quite adamant about this. He truly > believes that the best way to understand how an algorithm works > on a computer is to see it at the instruction level. > Then he could easily have invented a machine that is powerful enough for the details not to matter, and not need describing. The number and size of registers should not matter in any way for understanding algorithms. >>> It's not clear what he had against HLLs; perhaps he thought an actual >>> HLL would soon be superseded? Then pseudo-code should have been used. >> >> Real HLL's were around at the time - FORTRAN and ALGOL, or possibly >> LISP, would have been appropriate choices in the days before small >> letters were invented. Or he could have made a simple HLL pseudo-code >> for the purpose - things like unlimited sized integers and unlimited >> memory sizes would have made sense. > > The criticism of TAOCP is valid. It is not a book for workaday > programmers; it is an academic reference. The real value is in > the mathematical treatment and historical/bibliographic notes, > not the presentation of code in assembler. Agreed. > > Other books exist that are better references for most people; > certainly for most programmers: > > * "Introduction to Algorithms" by Cormen, Leiserson, Rivest, > and Stein is a standard. > * Sedgewick's books in C, simply titled, "Algorithms" are pretty > good, though his code style is slightly idiosyncratic. Now in > its 4th Edition, he has switched to Java and taken on a > coauthor (Kevin Wayne). > * "Algorithms", by Dasgupta, Papadimitriou, and Vazirani is a > wonderful book that, I think, strikes a nice balance between > formality and utility. > * I am rather partial to Skiena's work, "The Algorithm Design > Manual." > * "Data Structures and Their Algorithms" by Lewis and Denenberg > is a bit dated, but very accessible; examples are presented in > a Pascal-like pseudocode. > * "Data Structures and Algorithms" by Aho, Hopcroft, and Ullman > is a nice book. Be careful not to confuse it with Aho and > Ullman's, "The Design and Analysis of Computer Algorithms", > however; the latter is far denser and intended for academics > studying the fundamental properties of algorithms. > * "Algorithms + Data Structures = Programs" and the later > revision, "Algorithms & Data Structures", by Wirth, are > similarly dated, but still very nice. > * "Purely Functional Data Structures", by Okasaki; I include > this just because I think that it is fun. > My bookshelves are too scattered for me to easily see what I have. But I am not convinced that books are good references at all these days, for most purposes. They are, however, still good for study if you are doing courses or really learning a subject. > Of course, more specialized texts exist for specific problem > domains, programming paradigms, etc; books on string algorithms, > mutual exclusion protocols for concurrent programs, etc, are all > available, though not general. Yes. > > Did Knuth err using MIX and then MMIX for TAOCP? Perhaps; I do > think that neither Fortran nor Lisp would have been appropriate > choices; Algol perhaps. Other authors around that time or > slightly later either adopt a more mathematics-inspired > pseudonotation (Dijkstra; Hoare) algolish pseudo-code (Aho et > al), or a language of their own design (Wirth). > As Hoare was the head of department when I was at university, and therefore very influential in how we were taught, I am of course partial to his style of notation and think that would have been the best choice! > Knuth is a wonderful object of study, but I do not recommend it. > Agreed.
Re: AVL tree insertion - programming exercise
Author: Michael S
Date: Fri, 09 Oct 2026 14:47
Date: Fri, 09 Oct 2026 14:47
12 lines
1540 bytes
1540 bytes
On Fri, 9 Oct 2026 12:38:04 -0000 (UTC) cross@spitfire.i.gajendra.net (Dan Cross) wrote: > > Knuth is a wonderful object of study, but I do not recommend it. > > - Dan C. > Disagreed.
Re: AVL tree insertion - programming exercise
Author: Janis Papanagnou
Date: Fri, 09 Oct 2026 15:54
Date: Fri, 09 Oct 2026 15:54
57 lines
3981 bytes
3981 bytes
On 2026-10-09 16:05, David Brown wrote: > On 09/10/2026 14:38, Dan Cross wrote: >> In article <11aaimc$n0f0$2@dont-email.me>, >> David Brown <david.brown@hesbynett.no> wrote: >>> On 09/10/2026 12:43, bart wrote: >>>> [snip] >>>> This 'MIX' language was something that astonished me even then. Not >>>> only >>>> was it assembly that would totally obscure whatever algorithm was being >>>> expressed, but it was a weird made-up assembly with unusual byte and >>>> word sizes. >>> >>> Indeed. When I first saw it, I was familiar with several 8-bit >>> assemblies (6502, Z80, 8085) and some 32-bit ARM assembly, and had read >>> books on the 68000 family and MIPS. MIX was, to me, absurd. To be >>> fair, the computer world was not nearly as consolidated in 1962, but I'm >>> sure it looked fairly odd even by the standards of the time. >> >> If one were coming from the world of early 1960s mainframes, it >> would perhaps be less astonishing. > > It would undoubtedly have been less astonishing in 1968 (when the first > book was published) than in 1991 (when I first saw it), but I would have > thought that even people familiar with 1960's mainframes would see it as > strange to use a limited and highly specific artificial assembly that > matched poorly with existing systems. I am sure people were already > familiar with the pain of converting code written in assembly for one > system into assembly for a very different system - the only advantage of > the MIX code was that it was equally useless to everyone, and favoured > no one! I have no recollection on MIX. (Never engaged with it.) Incidentally, our own folks had an own set of abstract machines and respective machine languages (M1, M2, M3). (I'm not sure who actually invented them; I associated G. Seegmüller with those.) But their purpose was to teach system programming, and to teach about the (back these days) typical CPU addressing facilities, and about their utility to support HLLs. I think it makes sense to learn these things, the concepts, not on any specific commercial assembly language but on an abstract machine. YMMV. Using some hypothetical assembly to learn programming complex algorithms might be useful for case studies, but I think it's otherwise not a good idea. Our teachers uphold all due respect but clearly suggested more sensible representations. To represent high-level algorithms a clear notation is sensible, ideally without any ballast; it need not even be an existing HLL. Janis > [...]
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Fri, 09 Oct 2026 16:48
Date: Fri, 09 Oct 2026 16:48
22 lines
1761 bytes
1761 bytes
In article <20261009174739.00006700@yahoo.com>, Michael S <already5chosen@yahoo.com> wrote: >On Fri, 9 Oct 2026 12:38:04 -0000 (UTC) >cross@spitfire.i.gajendra.net (Dan Cross) wrote: >> Knuth is a wonderful object of study, but I do not recommend it. > >Disagreed. Lacking a lot of information here. Which part of what I wrote, most of which you snipped, do you disagree with, specifically? Would You recommend TAOCP as a reference for working programmers? What about it do you like? Is there anything about it that you do not like? Perhaps I should clarify: I recommend _studying_ it. I do not recommend it as an everyday reference for programmers, nor for learning data structures and algorithms; I thought that would have been obvious from the context that you omitted. - Dan C.
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Fri, 09 Oct 2026 19:48
Date: Fri, 09 Oct 2026 19:48
125 lines
7115 bytes
7115 bytes
In article <11aasbg$r60s$1@dont-email.me>, David Brown <david.brown@hesbynett.no> wrote: >On 09/10/2026 14:38, Dan Cross wrote: >> In article <11aaimc$n0f0$2@dont-email.me>, >> David Brown <david.brown@hesbynett.no> wrote: >>> On 09/10/2026 12:43, bart wrote: >>>> [snip] >>>> This 'MIX' language was something that astonished me even then. Not only >>>> was it assembly that would totally obscure whatever algorithm was being >>>> expressed, but it was a weird made-up assembly with unusual byte and >>>> word sizes. >>> >>> Indeed. When I first saw it, I was familiar with several 8-bit >>> assemblies (6502, Z80, 8085) and some 32-bit ARM assembly, and had read >>> books on the 68000 family and MIPS. MIX was, to me, absurd. To be >>> fair, the computer world was not nearly as consolidated in 1962, but I'm >>> sure it looked fairly odd even by the standards of the time. >> >> If one were coming from the world of early 1960s mainframes, it >> would perhaps be less astonishing. > >It would undoubtedly have been less astonishing in 1968 (when the first >book was published) than in 1991 (when I first saw it), but I would have >thought that even people familiar with 1960's mainframes would see it as >strange to use a limited and highly specific artificial assembly that >matched poorly with existing systems. That's the thing: I don't know that it did match poorly against many of those systems. Word oriented machines, with word sizes that were not powers-of-two, abstruse IO instructions; all of that was fairly common back then. True, the IBM 360 had bucked that trend and would set the precedent for what came later, but that hand't happened yet. Moreover, assembly language programming for applications was still common. Using assembler would have had a lot of reach, even if it's a fairly opaque way to present algorithms. >I am sure people were already >familiar with the pain of converting code written in assembly for one >system into assembly for a very different system - the only advantage of >the MIX code was that it was equally useless to everyone, and favoured >no one! Ha; great description. But had he chosen Burroughs, or IBM 360, or PDP-6, or the GE-635, or whatever else, it would have been seen as favoring a particular vendor (and therefore irrelevant to those using other machines) or too specific to be considered general. >> Unfortunately, he is quite adamant about this. He truly >> believes that the best way to understand how an algorithm works >> on a computer is to see it at the instruction level. > >Then he could easily have invented a machine that is powerful enough for >the details not to matter, and not need describing. The number and size >of registers should not matter in any way for understanding algorithms. Perhaps? I suspect that he felt that he _did_ come up with a machine powerful enough to describe what he wanted, yet not so abstract that one couldn't see one's way to implementing on the local CPU. He wasn't writing for the home computer user or hobby programmer, as home computers as we know them were yet to be invented. He was writing for people working in a computer center running programs on slow machines that filled a large room, and likely doing so on punched cards, possibly in assembler. We can look at MIX now (or even in the early 1990s) and say, "huh, that's weird..." but in 1968 it probably would not have raised so many eyebrows among its intended audience. >> Other books exist that are better references for most people; >> certainly for most programmers: >> >> * "Introduction to Algorithms" by Cormen, Leiserson, Rivest, >> and Stein is a standard. >> * Sedgewick's books in C, simply titled, "Algorithms" are pretty >> good, though his code style is slightly idiosyncratic. Now in >> its 4th Edition, he has switched to Java and taken on a >> coauthor (Kevin Wayne). >> * "Algorithms", by Dasgupta, Papadimitriou, and Vazirani is a >> wonderful book that, I think, strikes a nice balance between >> formality and utility. >> * I am rather partial to Skiena's work, "The Algorithm Design >> Manual." >> * "Data Structures and Their Algorithms" by Lewis and Denenberg >> is a bit dated, but very accessible; examples are presented in >> a Pascal-like pseudocode. >> * "Data Structures and Algorithms" by Aho, Hopcroft, and Ullman >> is a nice book. Be careful not to confuse it with Aho and >> Ullman's, "The Design and Analysis of Computer Algorithms", >> however; the latter is far denser and intended for academics >> studying the fundamental properties of algorithms. >> * "Algorithms + Data Structures = Programs" and the later >> revision, "Algorithms & Data Structures", by Wirth, are >> similarly dated, but still very nice. >> * "Purely Functional Data Structures", by Okasaki; I include >> this just because I think that it is fun. > >My bookshelves are too scattered for me to easily see what I have. But >I am not convinced that books are good references at all these days, for >most purposes. They are, however, still good for study if you are doing >courses or really learning a subject. Oh? Nowdays we have far more available resources, so I can see how one wouldn't necessarily reach for a book the way one would have in days past, but what do you see as the alternative? >> Did Knuth err using MIX and then MMIX for TAOCP? Perhaps; I do >> think that neither Fortran nor Lisp would have been appropriate >> choices; Algol perhaps. Other authors around that time or >> slightly later either adopt a more mathematics-inspired >> pseudonotation (Dijkstra; Hoare) algolish pseudo-code (Aho et >> al), or a language of their own design (Wirth). > >As Hoare was the head of department when I was at university, and >therefore very influential in how we were taught, I am of course partial >to his style of notation and think that would have been the best choice! Could be! I've always enjoyed the notation he came up with for CSP, for example. - Dan C.
Re: AVL tree insertion - programming exercise
Author: Scott Lurndal
Date: Fri, 09 Oct 2026 23:18
Date: Fri, 09 Oct 2026 23:18
13 lines
1474 bytes
1474 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes: >In article <11aasbg$r60s$1@dont-email.me>, >David Brown <david.brown@hesbynett.no> wrote: >>As Hoare was the head of department when I was at university, and >>therefore very influential in how we were taught, I am of course partial >>to his style of notation and think that would have been the best choice! > >Could be! I've always enjoyed the notation he came up with for >CSP, for example. Indeed, I still have a copy of that from grad school somewhere around here.
Re: AVL tree insertion - programming exercise
Author: Johann 'Myrkraverk' Oskarsson
Date: Sat, 10 Oct 2026 02:38
Date: Sat, 10 Oct 2026 02:38
52 lines
3977 bytes
3977 bytes
On 10/9/2026 11:54 PM, Janis Papanagnou wrote: > Using some hypothetical assembly to learn programming complex > algorithms might be useful for case studies, but I think it's > otherwise not a good idea. Our teachers uphold all due respect > but clearly suggested more sensible representations. > > To represent high-level algorithms a clear notation is sensible, > ideally without any ballast; it need not even be an existing HLL. That's what several books an algorithms do, including the venerable C.L.R.S., which I admit not to have read yet, but keep a copy on my shelf due to a recommendation. After all, it isn't Sedgewick, and the coverage is different. On Knuth, I forgot why he chose assembly language, but in the times he first started to write, that was the normal programming language of the day. Just, there were a lot more varieties out there, than today. Now- adays, you get by with x86, amd64, ARM, and Aaarch64, SPIM compatible cores in 32 and 64bit varieties, and the RISC-V variants. Really, nothing worth worrying about, and it's reasonable to expect any proficient programmer is fluent in any or all of these. I myself lack knowledge of the ARMatures, but dabble with the rest. At least, so far, I do have plans to dabble with an Aarch64 in the near-ish future. Other programmers have chosen a specific real world programming language to teach algorithms, and not just generally like C.L.R.S. and Sedgewick, but Mailund chose C for his /String Algorithms in C/ book, and /The Joys of Hashing/; then most game programming books have used C++. I personally like to learn this stuff in the concrete, rather than some abstract representation, and a lot of books from ages past were written in C -- only worth mentioning because we're talking about this stuff in comp.lang.c -- though nowadays it's Python, Java, Rust, C#, C++, Common Lisp, -- yes, I believe I mentioned that book before -- and even OCaml if we count /Handbook of Practical Logic and Automated Reasoning/ (2009) by John Harrison as worthy of a shelf space. Then I have books in ML, Pascal, Ada, and Modula-2. Those hardly ever show up as something written this century, so don't need to be studied by a student of the art. Personally, I'm not sure what I'd write in -- meaning the programming language -- if I were to write a book on the craft today, so let's just see if I publish something in the near-ish future. Best wishes, and happy writing books on the craft! -- Johann | email: invalid -> com | http://www.myrkraverk.com/blog/ I'm not from the Internet, I just work there. | via Easynews.com https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:; Federated at https://fed.brid.gy/bsky/myrkraverk.bsky.social
Re: AVL tree insertion - programming exercise
Author: Stefan Ram
Date: Sat, 10 Oct 2026 03:12
Date: Sat, 10 Oct 2026 03:12
14 lines
1919 bytes
1919 bytes
Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> wrote or quoted: >On Knuth, I forgot why he chose assembly language Assembly language is more readable. For example, LDA $3120,X is more readable than BD 20 31 .
Re: AVL tree insertion - programming exercise
Author: Lawrence D’Oliveiro
Date: Sat, 10 Oct 2026 04:44
Date: Sat, 10 Oct 2026 04:44
15 lines
1742 bytes
1742 bytes
On 10 Oct 2026 03:12:37 GMT, Stefan Ram wrote: > Assembly language is more readable. For example, > > LDA $3120,X > > is more readable than > > BD 20 31 > > . All the best CPUs disagree. “Puny humans! We spit on your weak computational capabilities!”
Re: AVL tree insertion - programming exercise
Author: David Brown
Date: Sat, 10 Oct 2026 13:27
Date: Sat, 10 Oct 2026 13:27
190 lines
11203 bytes
11203 bytes
On 09/10/2026 21:48, Dan Cross wrote: > In article <11aasbg$r60s$1@dont-email.me>, > David Brown <david.brown@hesbynett.no> wrote: >> On 09/10/2026 14:38, Dan Cross wrote: >>> In article <11aaimc$n0f0$2@dont-email.me>, >>> David Brown <david.brown@hesbynett.no> wrote: >>>> On 09/10/2026 12:43, bart wrote: >>>>> [snip] >>>>> This 'MIX' language was something that astonished me even then. Not only >>>>> was it assembly that would totally obscure whatever algorithm was being >>>>> expressed, but it was a weird made-up assembly with unusual byte and >>>>> word sizes. >>>> >>>> Indeed. When I first saw it, I was familiar with several 8-bit >>>> assemblies (6502, Z80, 8085) and some 32-bit ARM assembly, and had read >>>> books on the 68000 family and MIPS. MIX was, to me, absurd. To be >>>> fair, the computer world was not nearly as consolidated in 1962, but I'm >>>> sure it looked fairly odd even by the standards of the time. >>> >>> If one were coming from the world of early 1960s mainframes, it >>> would perhaps be less astonishing. >> >> It would undoubtedly have been less astonishing in 1968 (when the first >> book was published) than in 1991 (when I first saw it), but I would have >> thought that even people familiar with 1960's mainframes would see it as >> strange to use a limited and highly specific artificial assembly that >> matched poorly with existing systems. > > That's the thing: I don't know that it did match poorly against > many of those systems. Word oriented machines, with word sizes > that were not powers-of-two, abstruse IO instructions; all of > that was fairly common back then. True, the IBM 360 had bucked > that trend and would set the precedent for what came later, but > that hand't happened yet. > You misunderstand me, I think. Yes, cpus had widely varying architectures, word sizes, data representations, etc., and could have instructions dedicated to particular hardware devices - and if all else failed in your code, a "halt and catch fire" instruction. It's not so much that MIX matched poorly to existing systems - they all matched poorly to each other. Implementing an algorithm on different processors' assemblies meant figuring out how to make it work with that particular word size, or how to make efficient use of the particular indexing registers and addressing modes that the cpu used. Translating from one assembly to another on a radically different architecture means two steps - you first have to figure out what the original code is really trying to do, and abstract away from the messy implementation details. Then you have to fit it into the new architecture, and add the new messy implementation details. So by using MIX, rather than a higher level, more abstract pseudo-code, anyone trying to use the code in the book on a real cpu has to do twice the work. > Moreover, assembly language programming for applications was > still common. Using assembler would have had a lot of reach, > even if it's a fairly opaque way to present algorithms. > Sure, assembly was very commonly used for applications - and for a long time afterwards. It's a major reason why the computing world is dominated by the polished turd that is the modern x86 processor - translating assembly programs into anything else is difficult, expensive and risky, so it is rarely done. >> I am sure people were already >> familiar with the pain of converting code written in assembly for one >> system into assembly for a very different system - the only advantage of >> the MIX code was that it was equally useless to everyone, and favoured >> no one! > > Ha; great description. But had he chosen Burroughs, or IBM 360, > or PDP-6, or the GE-635, or whatever else, it would have been > seen as favoring a particular vendor (and therefore irrelevant > to those using other machines) or too specific to be considered > general. I can fully understand neutrality here. He may even have wanted to avoid favouring one existing high-level language for another. But the guy was pretty smart - he could easily have invented his own hypothetical high-level language to use instead of such a low-level one. > >>> Unfortunately, he is quite adamant about this. He truly >>> believes that the best way to understand how an algorithm works >>> on a computer is to see it at the instruction level. >> >> Then he could easily have invented a machine that is powerful enough for >> the details not to matter, and not need describing. The number and size >> of registers should not matter in any way for understanding algorithms. > > Perhaps? > > I suspect that he felt that he _did_ come up with a machine > powerful enough to describe what he wanted, yet not so abstract > that one couldn't see one's way to implementing on the local > CPU. He wasn't writing for the home computer user or hobby > programmer, as home computers as we know them were yet to be > invented. He was writing for people working in a computer > center running programs on slow machines that filled a large > room, and likely doing so on punched cards, possibly in > assembler. We can look at MIX now (or even in the early 1990s) > and say, "huh, that's weird..." but in 1968 it probably would > not have raised so many eyebrows among its intended audience. > >>> Other books exist that are better references for most people; >>> certainly for most programmers: >>> >>> * "Introduction to Algorithms" by Cormen, Leiserson, Rivest, >>> and Stein is a standard. >>> * Sedgewick's books in C, simply titled, "Algorithms" are pretty >>> good, though his code style is slightly idiosyncratic. Now in >>> its 4th Edition, he has switched to Java and taken on a >>> coauthor (Kevin Wayne). >>> * "Algorithms", by Dasgupta, Papadimitriou, and Vazirani is a >>> wonderful book that, I think, strikes a nice balance between >>> formality and utility. >>> * I am rather partial to Skiena's work, "The Algorithm Design >>> Manual." >>> * "Data Structures and Their Algorithms" by Lewis and Denenberg >>> is a bit dated, but very accessible; examples are presented in >>> a Pascal-like pseudocode. >>> * "Data Structures and Algorithms" by Aho, Hopcroft, and Ullman >>> is a nice book. Be careful not to confuse it with Aho and >>> Ullman's, "The Design and Analysis of Computer Algorithms", >>> however; the latter is far denser and intended for academics >>> studying the fundamental properties of algorithms. >>> * "Algorithms + Data Structures = Programs" and the later >>> revision, "Algorithms & Data Structures", by Wirth, are >>> similarly dated, but still very nice. >>> * "Purely Functional Data Structures", by Okasaki; I include >>> this just because I think that it is fun. >> >> My bookshelves are too scattered for me to easily see what I have. But >> I am not convinced that books are good references at all these days, for >> most purposes. They are, however, still good for study if you are doing >> courses or really learning a subject. > > Oh? Nowdays we have far more available resources, so I can see > how one wouldn't necessarily reach for a book the way one would > have in days past, but what do you see as the alternative? > I don't know that there is one right answer - it depends on the depth you want to go into, how specialist the topic is, and what you are trying to get out of it all in the end. And it depends on how you personally want to learn. For deeper study and for more specialist topics, books are still the top choice for me. And I much prefer to read books than websites over breakfast. But if I want a reference or refresher on AVL trees (to pick a random topic :-) ), my first port of call is Wikipedia. If that is not enough, I might be looking for videos - there's plenty around that can give nice animations to help you understand what is going on, from five minute quick introductions to hour-long lectures. I'd look for sample code in a high level language - typically Python, but it might be interesting to look at implementations in Haskell or other significantly different languages. There are webpages full of explanations of them, and comparisons to other tree structures. And there are online courses for those looking for more academic treatment of data structures and algorithms. The internet is full of resources like this. Of course the quality is varied - you definitely want to look at more than one site to get a balanced view, and to minimise the risk of missing something important. But for technical and non-controversial subjects like this, you are going to be fairly safe with Wikipedia and a google search. >>> Did Knuth err using MIX and then MMIX for TAOCP? Perhaps; I do >>> think that neither Fortran nor Lisp would have been appropriate >>> choices; Algol perhaps. Other authors around that time or >>> slightly later either adopt a more mathematics-inspired >>> pseudonotation (Dijkstra; Hoare) algolish pseudo-code (Aho et >>> al), or a language of their own design (Wirth). >> >> As Hoare was the head of department when I was at university, and >> therefore very influential in how we were taught, I am of course partial >> to his style of notation and think that would have been the best choice! > > Could be! I've always enjoyed the notation he came up with for > CSP, for example. > I wonder how CSP is taught now that people wave bank cards or mobile phones at vending machines instead of inserting coins... (There's a fun family of microcontrollers with CSP at their heart from XMOS.)
Re: AVL tree insertion - programming exercise
Author: Michael S
Date: Sat, 10 Oct 2026 16:08
Date: Sat, 10 Oct 2026 16:08
47 lines
2867 bytes
2867 bytes
On Fri, 9 Oct 2026 16:48:21 -0000 (UTC) cross@spitfire.i.gajendra.net (Dan Cross) wrote: > In article <20261009174739.00006700@yahoo.com>, > Michael S <already5chosen@yahoo.com> wrote: > >On Fri, 9 Oct 2026 12:38:04 -0000 (UTC) > >cross@spitfire.i.gajendra.net (Dan Cross) wrote: > >> Knuth is a wonderful object of study, but I do not recommend it. > > > >Disagreed. > > Lacking a lot of information here. > > Which part of what I wrote, most of which you snipped, do you > disagree with, specifically? Would You recommend TAOCP as a > reference for working programmers? What about it do you like? > Is there anything about it that you do not like? > > Perhaps I should clarify: I recommend _studying_ it. That's better. > I do not > recommend it as an everyday reference for programmers, nor for > learning data structures and algorithms; It is still the best reference for *some* data structures and algorithms. Not necessarily something that is still practically important. In particular, I am thinking about external sortng. > I thought that would > have been obvious from the context that you omitted. > No, it was not. > - Dan C. > What I find especially embarrassing in above conversation between David and yourself is likening of TAoCP to K&R. K&R is just another book of rather average quality that turned important only because, by lucky twist of history, the language it describes became extremely popular. OTOH, TAoCP is a monument.
Re: AVL tree insertion - programming exercise
Author: David Brown
Date: Sat, 10 Oct 2026 16:29
Date: Sat, 10 Oct 2026 16:29
19 lines
2247 bytes
2247 bytes
On 10/10/2026 18:08, Michael S wrote: > What I find especially embarrassing in above conversation between David > and yourself is likening of TAoCP to K&R. > K&R is just another book of rather average quality that turned important > only because, by lucky twist of history, the language it describes > became extremely popular. > OTOH, TAoCP is a monument. > The comparison was that both were well-written textbooks with clear and readable language, and both are now outdated and usually a poor choice for someone today wanting to learn about their topics or use them as references. Obviously there are also very significant differences between them. You don't even need to open the books - you only need to look at the thickness of the books to see their scopes were hugely different.
Re: AVL tree insertion - programming exercise
Author: Bonita Montero
Date: Sat, 10 Oct 2026 17:40
Date: Sat, 10 Oct 2026 17:40
6 lines
1761 bytes
1761 bytes
AVL trees are cool. Nominally, a red-black tree is faster for insertions and deletions—and in C++, `std::map` is almost always based on one—but that is a theoretical advantage. In practice, because the search depth of an AVL tree is up to half that of a red-black tree, you touch fewer memory locations during a search, meaning less cache data needs to be reloaded. The same applies to insertion and deletion operations.
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Sat, 10 Oct 2026 17:43
Date: Sat, 10 Oct 2026 17:43
78 lines
3770 bytes
3770 bytes
In article <20261010190833.00007268@yahoo.com>, Michael S <already5chosen@yahoo.com> wrote: >On Fri, 9 Oct 2026 16:48:21 -0000 (UTC) >cross@spitfire.i.gajendra.net (Dan Cross) wrote: > >> In article <20261009174739.00006700@yahoo.com>, >> Michael S <already5chosen@yahoo.com> wrote: >> >On Fri, 9 Oct 2026 12:38:04 -0000 (UTC) >> >cross@spitfire.i.gajendra.net (Dan Cross) wrote: >> >> Knuth is a wonderful object of study, but I do not recommend it. >> > >> >Disagreed. >> >> Lacking a lot of information here. >> >> Which part of what I wrote, most of which you snipped, do you >> disagree with, specifically? Would You recommend TAOCP as a >> reference for working programmers? What about it do you like? >> Is there anything about it that you do not like? >> >> Perhaps I should clarify: I recommend _studying_ it. > >That's better. Better? Better would be to preserve context and add useful information to the thread. You quoted the part where I sayd, "Knuth is a wonderful object of study" and then replied with the single, information-free, word, "Disagreed", leaving it totally ambiguous whether you disagreed with that or with my saying that "I do not recommend it." >> I do not >> recommend it as an everyday reference for programmers, nor for >> learning data structures and algorithms; > >It is still the best reference for *some* data structures and >algorithms. Not necessarily something that is still practically >important. In particular, I am thinking about external sortng. CLRS covers external sorting, or did in earlier editions. >> I thought that would >> have been obvious from the context that you omitted. > >No, it was not. To you. Let's look at your response and the context your preserved. Given that "Knuth is a wonderful object of study, but I do not recommend it", your response was either factually incorrect, or a statement of your own dislike of studying Knuth. Which was true depends on which part of my statement you disagreed with. If the second part, that I do not recommend Knuth, then "Disagreed" would mean that you think I _am_ recommending it, in direct contradiction of what I had just said; that would be factually incorrect. If "Disagreed" with the _first_ part, then you are saying you disagree that Knuth is a, "wonderful object of study." If that is the case, just say so. >What I find especially embarrassing in above conversation between David >and yourself is likening of TAoCP to K&R. >K&R is just another book of rather average quality that turned important >only because, by lucky twist of history, the language it describes >became extremely popular. >OTOH, TAoCP is a monument. Cool, man. Cool. - Dan C. (If he saw this, Don would roll his eyes.)
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Sat, 10 Oct 2026 17:44
Date: Sat, 10 Oct 2026 17:44
24 lines
1986 bytes
1986 bytes
In article <11adp65$1s1lb$2@dont-email.me>, David Brown <david.brown@hesbynett.no> wrote: >On 10/10/2026 18:08, Michael S wrote: >> What I find especially embarrassing in above conversation between David >> and yourself is likening of TAoCP to K&R. >> K&R is just another book of rather average quality that turned important >> only because, by lucky twist of history, the language it describes >> became extremely popular. >> OTOH, TAoCP is a monument. > >The comparison was that both were well-written textbooks with clear and >readable language, and both are now outdated and usually a poor choice >for someone today wanting to learn about their topics or use them as >references. > >Obviously there are also very significant differences between them. You >don't even need to open the books - you only need to look at the >thickness of the books to see their scopes were hugely different. I find it weird that this guy feels the need to rise to the defense of Knuth's honor or something. - Dan C.
Re: AVL tree insertion - programming exercise
Author: Janis Papanagnou
Date: Sat, 10 Oct 2026 18:35
Date: Sat, 10 Oct 2026 18:35
24 lines
2209 bytes
2209 bytes
On 2026-10-10 18:08, Michael S wrote: >> TAOCP > > It is still the best reference for *some* data structures and > algorithms. Not necessarily something that is still practically > important. In particular, I am thinking about external sortng. Have you anything specific in mind beyond merge-sort? I was under the impression that many books about algorithms and data-structures covered the topics that Knuth collected in his "monument". >> [...] > [...] > K&R is just another book of rather average quality that turned important > only because, by lucky twist of history, the language it describes > became extremely popular. I know people that back in the days valued K&R as excellent book. (I never understood where that came from, though.) Janis
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Sat, 10 Oct 2026 19:36
Date: Sat, 10 Oct 2026 19:36
36 lines
2197 bytes
2197 bytes
In article <11ae0h8$cjlv$3@dont-email.me>, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: >On 2026-10-10 18:08, Michael S wrote: >>> TAOCP >> >> It is still the best reference for *some* data structures and >> algorithms. Not necessarily something that is still practically >> important. In particular, I am thinking about external sortng. > >Have you anything specific in mind beyond merge-sort? Radix sort? >I was under the impression that many books about algorithms >and data-structures covered the topics that Knuth collected >in his "monument". They do. Knuth does go into great detail about the precise mathematical properties of the methods he discusses, but it is not as if treatment is absent from other sources. >>> [...] >> [...] >> K&R is just another book of rather average quality that turned important >> only because, by lucky twist of history, the language it describes >> became extremely popular. > >I know people that back in the days valued K&R as excellent >book. (I never understood where that came from, though.) The way the poster described it, I think it likely a reflection of deference to the cachet of Knuth, less the substance of either reference. - Dan C.
Re: AVL tree insertion - programming exercise
Author: Andrey Tarasevich
Date: Sat, 10 Oct 2026 19:39
Date: Sat, 10 Oct 2026 19:39
24 lines
2359 bytes
2359 bytes
On Tue 10/6/2026 11:08 PM, Tim Rentsch wrote: > I offer this small programming exercise for anyone who is > interested. I did exactly that a couple of months ago, except that my case the AVL tree was intended to serve as a carrying sub-structure for an augmented interval tree. I did not make any effort to decouple the implementations, meaning that my AVL implementation is intermixed with the interval tree implementation. Also, I had to support not only operations that start from the root of the tree (like your `add_key`), but also operations that start from a specific arbitrary node (e.q. a `delete_node(Node *)` operation). So, in my case I opted to also keep and maintain the parent link in each node. Extra memory expenditure is not that drastic, especially considering the fact that my nodes are already heavier - carrying geometric data for the interval tree. Meanwhile, you seem to be striving to squeeze out all "unnecessary" memory usage (re: embedding the balance value into the unused pointer bits). Is this a hard requirement in your exercise? -- Best regards, Andrey
Re: AVL tree insertion - programming exercise
Author: Michael S
Date: Sat, 10 Oct 2026 20:00
Date: Sat, 10 Oct 2026 20:00
32 lines
2814 bytes
2814 bytes
On Sat, 10 Oct 2026 12:39:09 -0700 Andrey Tarasevich <noone@noone.net> wrote: > On Tue 10/6/2026 11:08 PM, Tim Rentsch wrote: > > I offer this small programming exercise for anyone who is > > interested. > I did exactly that a couple of months ago, except that my case the > AVL tree was intended to serve as a carrying sub-structure for an > augmented interval tree. I did not make any effort to decouple the > implementations, meaning that my AVL implementation is intermixed > with the interval tree implementation. > > Also, I had to support not only operations that start from the root > of the tree (like your `add_key`), but also operations that start > from a specific arbitrary node (e.q. a `delete_node(Node *)` > operation). So, in my case I opted to also keep and maintain the > parent link in each node. Extra memory expenditure is not that > drastic, especially considering the fact that my nodes are already > heavier - carrying geometric data for the interval tree. > > Meanwhile, you seem to be striving to squeeze out all "unnecessary" > memory usage (re: embedding the balance value into the unused pointer > bits). Is this a hard requirement in your exercise? > I also did it relatively recently (2 or 3 years ago) and also in specialized context (median filtering) so not sure how applicable my solution would be here. BTW, at the end AVL tree didn't ended up as my data structure of choice for this particular application, but it did ended up as a solid contender with the same BigO characteristics as a winner.
Re: AVL tree insertion - programming exercise
Author: Janis Papanagnou
Date: Sat, 10 Oct 2026 20:08
Date: Sat, 10 Oct 2026 20:08
25 lines
2234 bytes
2234 bytes
On 2026-10-10 21:36, Dan Cross wrote: > In article <11ae0h8$cjlv$3@dont-email.me>, > Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: >> On 2026-10-10 18:08, Michael S wrote: >>>> TAOCP >>> >>> It is still the best reference for *some* data structures and >>> algorithms. Not necessarily something that is still practically >>> important. In particular, I am thinking about external sortng. >> >> Have you anything specific in mind beyond merge-sort? > > Radix sort? I've only few dedicated books about "data structures and algorithms" in my bookshelf, but one the the three has it as topic. I suppose if any of the algorithms appearing in Knuth's book would over the time not have been mentioned as well by other authors then that algorithm might not have been worth to be mentioned? ;-) Janis > [...]
Re: AVL tree insertion - programming exercise
Author: Michael S
Date: Sat, 10 Oct 2026 20:21
Date: Sat, 10 Oct 2026 20:21
21 lines
2022 bytes
2022 bytes
On Sat, 10 Oct 2026 19:36:53 -0000 (UTC) cross@spitfire.i.gajendra.net (Dan Cross) wrote: > In article <11ae0h8$cjlv$3@dont-email.me>, > Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: > >On 2026-10-10 18:08, Michael S wrote: > >>> TAOCP > >> > >> It is still the best reference for *some* data structures and > >> algorithms. Not necessarily something that is still practically > >> important. In particular, I am thinking about external sortng. > > > >Have you anything specific in mind beyond merge-sort? > > Radix sort? > Is there a variant of radix sort usable for external sorting? I don't recollect anything of this sort.
Re: AVL tree insertion - programming exercise
Author: Scott Lurndal
Date: Sat, 10 Oct 2026 20:22
Date: Sat, 10 Oct 2026 20:22
22 lines
1892 bytes
1892 bytes
Michael S <already5chosen@yahoo.com> writes: >On Fri, 9 Oct 2026 16:48:21 -0000 (UTC) >cross@spitfire.i.gajendra.net (Dan Cross) wrote: > >> In article <20261009174739.00006700@yahoo.com>, >> Michael S <already5chosen@yahoo.com> wrote: >> >On Fri, 9 Oct 2026 12:38:04 -0000 (UTC) >> >cross@spitfire.i.gajendra.net (Dan Cross) wrote: >> >> Knuth is a wonderful object of study, but I do not recommend it. > >> I do not >> recommend it as an everyday reference for programmers, nor for >> learning data structures and algorithms; > >It is still the best reference for *some* data structures and >algorithms. Not necessarily something that is still practically >important. In particular, I am thinking about external sortng. Like 6-tape drive merge sorts? Not a common use case in these days...
Re: AVL tree insertion - programming exercise
Author: Janis Papanagnou
Date: Sat, 10 Oct 2026 20:28
Date: Sat, 10 Oct 2026 20:28
33 lines
2720 bytes
2720 bytes
On 2026-10-10 21:39, Andrey Tarasevich wrote: > [ AVL-tree implementation ] > > Also, I had to support not only operations that start from the root of > the tree (like your `add_key`), but also operations that start from a > specific arbitrary node (e.q. a `delete_node(Node *)` operation). We don't need a parent-reference to delete an actual referenced node, do we? In my Algol 68 implementation I'm using a "REF REF NODE" type (let's call that a pointer reference for better understanding) that makes it possible to operate on a higher indirection level. (In C++ that might be implemented with &*, and in "C" emulated with **, I'd suppose, and in Pascal I had done that with a 'var' parameter on a pointer type; I don't recall to have implemented that in "C" myself but I don't see that it wouldn't be feasible.) > So, in > my case I opted to also keep and maintain the parent link in each node. > Extra memory expenditure is not that drastic, especially considering the > fact that my nodes are already heavier - carrying geometric data for the > interval tree. > > Meanwhile, you seem to be striving to squeeze out all "unnecessary" > memory usage (re: embedding the balance value into the unused pointer > bits). Is this a hard requirement in your exercise? I recall I've done such a hack in the 1980's on an Atari where memory was scarce to implement a 'tries' data structure. Janis
Re: AVL tree insertion - programming exercise
Author: Michael S
Date: Sat, 10 Oct 2026 20:32
Date: Sat, 10 Oct 2026 20:32
37 lines
2766 bytes
2766 bytes
On Sat, 10 Oct 2026 20:22:21 GMT scott@slp53.sl.home (Scott Lurndal) wrote: > Michael S <already5chosen@yahoo.com> writes: > >On Fri, 9 Oct 2026 16:48:21 -0000 (UTC) > >cross@spitfire.i.gajendra.net (Dan Cross) wrote: > > > >> In article <20261009174739.00006700@yahoo.com>, > >> Michael S <already5chosen@yahoo.com> wrote: > >> >On Fri, 9 Oct 2026 12:38:04 -0000 (UTC) > >> >cross@spitfire.i.gajendra.net (Dan Cross) wrote: > >> >> Knuth is a wonderful object of study, but I do not recommend > >> >> it. > > > > >> I do not > >> recommend it as an everyday reference for programmers, nor for > >> learning data structures and algorithms; > > > >It is still the best reference for *some* data structures and > >algorithms. Not necessarily something that is still practically > >important. In particular, I am thinking about external sortng. > > Like 6-tape drive merge sorts? Not a common use case in these > days... > Sure. But algorithms are still fun. I like chess problems, mostly of mate in 3 variaty. That sorting thing is certainly no worse than some of them. But I think that he discusses sorting on other forms of mass storage as well. Not sure if any of specific algorithms would be applicable to modern SSDs but the way he analyze them very likely still relevant.
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Sat, 10 Oct 2026 20:55
Date: Sat, 10 Oct 2026 20:55
25 lines
1800 bytes
1800 bytes
In article <20261010232112.000024ee@yahoo.com>, Michael S <already5chosen@yahoo.com> wrote: >On Sat, 10 Oct 2026 19:36:53 -0000 (UTC) >cross@spitfire.i.gajendra.net (Dan Cross) wrote: > >> In article <11ae0h8$cjlv$3@dont-email.me>, >> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: >> >On 2026-10-10 18:08, Michael S wrote: >> >>> TAOCP >> >> >> >> It is still the best reference for *some* data structures and >> >> algorithms. Not necessarily something that is still practically >> >> important. In particular, I am thinking about external sortng. >> > >> >Have you anything specific in mind beyond merge-sort? >> >> Radix sort? > >Is there a variant of radix sort usable for external sorting? >I don't recollect anything of this sort. Section 5.4.7 is titled, "External Radix Sorting." - Dan C.
Re: AVL tree insertion - programming exercise
Author: Chris M. Thomasson
Date: Sat, 10 Oct 2026 21:12
Date: Sat, 10 Oct 2026 21:12
21 lines
2074 bytes
2074 bytes
On 10/9/2026 9:44 PM, Lawrence D’Oliveiro wrote: > On 10 Oct 2026 03:12:37 GMT, Stefan Ram wrote: > >> Assembly language is more readable. For example, >> >> LDA $3120,X >> >> is more readable than >> >> BD 20 31 >> >> . > > All the best CPUs disagree. > > “Puny humans! We spit on your weak computational capabilities!” Fwiw, check this out when you get some free time to burn: (Making an SNES Game the Way Nintendo Intended) https://youtu.be/kYLJLJkVfLk
Re: AVL tree insertion - programming exercise
Author: David Brown
Date: Sat, 10 Oct 2026 21:22
Date: Sat, 10 Oct 2026 21:22
30 lines
2890 bytes
2890 bytes
On 10/10/2026 19:44, Dan Cross wrote: > In article <11adp65$1s1lb$2@dont-email.me>, > David Brown <david.brown@hesbynett.no> wrote: >> On 10/10/2026 18:08, Michael S wrote: >>> What I find especially embarrassing in above conversation between David >>> and yourself is likening of TAoCP to K&R. >>> K&R is just another book of rather average quality that turned important >>> only because, by lucky twist of history, the language it describes >>> became extremely popular. >>> OTOH, TAoCP is a monument. >> >> The comparison was that both were well-written textbooks with clear and >> readable language, and both are now outdated and usually a poor choice >> for someone today wanting to learn about their topics or use them as >> references. >> >> Obviously there are also very significant differences between them. You >> don't even need to open the books - you only need to look at the >> thickness of the books to see their scopes were hugely different. > > I find it weird that this guy feels the need to rise to the > defense of Knuth's honor or something. > If that is the case, then he needn't bother - I have great respect for Knuth and what has contributed to the field of computer science (and of course also computer typography). But that doesn't mean I think he made the right decision in his use of MIX and assembly, or that I think his books are ever-lasting. We can appreciate people's achievements without idolising them or their works.
Re: AVL tree insertion - programming exercise
Author: Tim Rentsch
Date: Sun, 11 Oct 2026 02:02
Date: Sun, 11 Oct 2026 02:02
185 lines
7701 bytes
7701 bytes
Andrey Tarasevich <noone@noone.net> writes:
> On Tue 10/6/2026 11:08 PM, Tim Rentsch wrote:
>
>> I offer this small programming exercise for anyone who is
>> interested.
>
> I did exactly that a couple of months ago, except that my case the AVL
> tree was intended to serve as a carrying sub-structure for an
> augmented interval tree. I did not make any effort to decouple the
> implementations, meaning that my AVL implementation is intermixed with
> the interval tree implementation.
>
> Also, I had to support not only operations that start from the root of
> the tree (like your `add_key`), but also operations that start from a
> specific arbitrary node (e.q. a `delete_node(Node *)` operation). So,
> in my case I opted to also keep and maintain the parent link in each
> node. Extra memory expenditure is not that drastic, especially
> considering the fact that my nodes are already heavier - carrying
> geometric data for the interval tree.
Right. Clearly your requirements were more extensive than those of
my simple programming exercise.
> Meanwhile, you seem to be striving to squeeze out all "unnecessary"
> memory usage (re: embedding the balance value into the unused pointer
> bits). Is this a hard requirement in your exercise?
It might be useful to give some history as background for my
comments, and for the exercise.
More than a few years ago I was interested in using trees, of some
as-yet-to-be-determined variety, as the basis for a (generic) set
data type. One of the kinds of trees I looked into was AVL trees.
Because I had a copy of Sorting and Searching handy, I pulled it off
the shelf and read about AVL trees. To help my exploration, I took
the Knuth description of AVL-tree insertion and transliterated it
into C. Here is the result, more or less a direct expression of the
AVL-insertion algorithm given in Sorting and Searching:
#include <assert.h>
#include <stdlib.h>
typedef struct tree_node_s *Tree;
typedef signed int Key;
typedef signed int Balance;
struct tree_node_s {
Tree left, right;
Key key;
Balance b;
};
void
avl_insert( Tree head, Key k ){
Tree t, s, p, q, r;
Balance a;
A1:
t = head, s = p = head->right;
/* fall through */
A2:
if( k < p->key ) goto A3;
if( k > p->key ) goto A4;
return;
A3:
q = p->left;
if( !q ){
p->left = q = malloc( sizeof *q );
goto A5;
}
if( q->b != 0 ) t = p, s = q;
p = q;
goto A2;
A4:
q = p->right;
if( !q ){
p->right = q = malloc( sizeof *q );
goto A5;
}
if( q->b != 0 ) t = p, s = q;
p = q;
goto A2;
A5:
q->left = q->right = 0, q->key = k, q->b = 0;
/* fall through */
A6:
if( k < s->key ) r = p = s->left;
else r = p = s->right;
while( p != q ){
if( k < p->key ) p->b = -1, p = p->left;
else p->b = +1, p = p->right;
}
/* fall through */
A7:
if( k < s->key ) a = -1;
else a = +1;
if( s->b == 0 ){
s->b = a;
return;
}
if( s->b == -a ){
s->b = 0;
return;
}
if( s->b == a ){
if( r->b == a ) goto A8;
if( r->b == -a ) goto A9;
assert( 0 );
}
assert( 0 );
A8:
p = r;
if( a == 1 ) s->right = r->left, r->left = s;
else s->left = r->right, r->right = s;
s->b = r->b = 0;
goto A10;
A9:
if( a == 1 ){
p = r->right;
r->right = p->left, p->left = r;
s->left = p->right, p->right = s;
} else {
p = r->left;
r->left = p->right, p->right = r;
s->right = p->left, p->left = s;
}
if( p->b == a ) s->b = -a, r->b = 0;
if( p->b == 0 ) s->b = 0, r->b = 0;
if( p->b == -a ) s->b = 0, r->b = -a;
p->b = 0;
/* fall through */
A10:
if( s == t->right ) t->right = p;
else t->left = p;
}
The code compiles but I can't say more than that. To the best of my
recollection it was never actually run.
Because I found the above "explanation" so unhelpful in trying to
understand how AVL trees work, I wrote my own AVL-insertion code in
C, using an interface similar to the one shown in my posting stating
the exercise. (The earlier interface was slightly more elaborate
because it allowed for "threading" of the binary tree, but I took
that aspect out of the exercise problem.)
In my revised version, it made sense to put the extra state "inside"
the pointers, in order to distinguish a "true" link from a "thread"
link in the tree traversal algorithms. In posting the exercise I
took out the second extra bit (the bit saying this link is a thread
link) but otherwise left it alone. In some ways I think having the
"taller" bit be part of the links simplifies the algorithm slightly
but I didn't really think about that when posting the exercise.
There is an advantage (to me) to keep that interface, since I
already have a test rig to drive it. There wasn't any sort of
consideration about the nature of the exercise, just a pragmatic
thought that if there were any testing to be done it would be
easier to use the same interface.
I posted the exercise because I thought some people might enjoy
writing some C code, and because I was (and am) curious about how
other people would implement AVL insertion in C. AVL trees are
rather tricky to implement, and I wonder what other people would do
to deal with that barrier. And since you have some experience (and
also a good level of expertise) I hope you will take the time to
write and post an answer.
Incidentally, in my tree investigation of many years ago, the winner
(judged by an unspecified metric) ended up being 2-3-4 trees. But
that is a topic for another day (and most likely another newsgroup,
since it wasn't written in C).
Re: AVL tree insertion - programming exercise
Author: Scott Lurndal
Date: Sun, 11 Oct 2026 15:24
Date: Sun, 11 Oct 2026 15:24
42 lines
2688 bytes
2688 bytes
Michael S <already5chosen@yahoo.com> writes: >On Sat, 10 Oct 2026 20:22:21 GMT >scott@slp53.sl.home (Scott Lurndal) wrote: > >> Michael S <already5chosen@yahoo.com> writes: >> >On Fri, 9 Oct 2026 16:48:21 -0000 (UTC) >> >cross@spitfire.i.gajendra.net (Dan Cross) wrote: >> > >> >> In article <20261009174739.00006700@yahoo.com>, >> >> Michael S <already5chosen@yahoo.com> wrote: >> >> >On Fri, 9 Oct 2026 12:38:04 -0000 (UTC) >> >> >cross@spitfire.i.gajendra.net (Dan Cross) wrote: >> >> >> Knuth is a wonderful object of study, but I do not recommend >> >> >> it. >> >> > >> >> I do not >> >> recommend it as an everyday reference for programmers, nor for >> >> learning data structures and algorithms; >> > >> >It is still the best reference for *some* data structures and >> >algorithms. Not necessarily something that is still practically >> >important. In particular, I am thinking about external sortng. >> >> Like 6-tape drive merge sorts? Not a common use case in these >> days... >> > >Sure. But algorithms are still fun. It was fun to watch the merge sorts. Our (Burroughs) sort Guru (Bernie) had highly optimized the MCP sort intrinsic, and would test with a string of a dozen 6250 BPI/200IP 9-track drives. Definitely fun to watch. Bernie is the fellow in the green kate wolf tee shirt, who famously stated in 1983 that programmers didn't need their own office terminals (vs. a common terminal room). One of the wags in the room commented to him that his car sits in the parking lot, unused, most of the day. https://www.youtube.com/watch?v=3-hIflT1DAY
Page 1 of 3 • 114 total messages
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads