[Tutor] How to create a binary tree hierarchy given a list of elements as its leaves

Alan Gauld alan.gauld at yahoo.co.uk
Tue Jan 30 17:23:06 EST 2024


On 29/01/2024 23:17, ThreeBlindQuarks via Tutor wrote:

> Your code takes time and effort to copy and try 

I gave up when I saw it was Python v2 code and I
don't have v2 interpreter installed.

> I had hoped others would chime in ;-)

I had planned to, but it all became a bit to
convoluted to comprehend without putting in
more time than I have available. Especially
since I still don't understand the objective.
The label thing makes no sense to me.

> def generate_node_label():
>     return random.choice(string.ascii_lowercase) \
>         + str(random.randint(0, 10))
> 
> It takes no argument and generates two-part strings 

The "random" labels for the nodes. Although it does
allow duplicates which are then removed by later
code(perhaps) so not truly random. But not clear
what their purpose or value is.


> Moving on, this two-line combo floors me.
> 
>     while len(node_hierarchy_name) > 1:
>         k = k * 2
> 
> This is either an infinite loop or does nothing. 

I initially thought that too but the indented code after
the exdented comment is actually inside the loop and
does change node_heirarchy_name

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos





More information about the Tutor mailing list