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

ThreeBlindQuarks threesomequarks at proton.me
Tue Jan 30 21:59:05 EST 2024



Duh! Sorry Alan, I got misled by empty lines and a comment dedented all the way and stopped looking below to see the rest of the code. 

So, yes, the variable gets emptied rapidly and then refilled.

My major concerns with such designs are that without a decent explanation of intent, it is hard to see if it does what is intended. Often, to report a problem, you both narrow the range so a small example can be run where you show what you expected alongside what you got.

ANd it seems my interpreter gets confused by comments not indented at the level of the code. I had to move them to the right to get it to see the full loop too or it went into that infinite loop on the first part.

Ideally code should not be edited after and we should get an exact copy of what was run, albeit I too have no interest in using python 2.0 and I have to ask why anyone is except for legacy code.

I want to ask a serious question. Are there two programming assignments here with one being smashed together with another?

I mean if you have an assignment to create functions or classes to represent a binary tree with insertions and deletions and traversals, then the payload is not the main point. You then exercise the algorithm with something like root.insert(node) and so on. You do not need random names albeit to really test it, sure, you can make some up. But note that if the algorithm sorts nodes in alphabetical order, then a10 may come ahead of a9. You may need a special order function that takes the two parts apart, or insist on using a01 and a02 and so on so a10 is more than a09.

What the code I am seeing does not really seem to be doing any of what I am expecting. I have not really been shown what is expected albeit in another forum, some of the design is shown.

Having said that, I can imagine ways the above could make a binary tree representation but perhaps one it would be difficult to traverse. Can you do a depth-first search versus a breadth first search on this and apply recursive algorithms?

If nothing else, the OP perhaps should consider explaining what something like this does that I wonder if they just copied from somewhere:

cells_arrays = list(map(lambda x: [x[0], x[-1]],
                            np.array_split(data, data.size // k)))

I can run it and see the results but by itself it is formidable. It creates an anonymous lambda function that simply return a list of the first and last item in what it is applied to. It applies that to what looks like the numbers modulo 100, which in this case may be meaningless.  Then it converts what I think is a list to being a list.

Now here would be an alternative I can imagine where the names grew as you went deeper. If you looked at a name like "a5.b6.c7" and wanted to insert "b9" which would be after b6 but before b7, perhaps you could adjust names as you went along to reflect some kind of hierarchy. I would not likely do that either and go with actual objects as classes which point to other such nodes and leaves and which can easily be traversed.

Without explanations, I am not seeing why I would want to go on. Sorry.

My advice sometimes to people is to do things another way rather than pound on to a design they may not have thought out well enough. Amazingly often, that is not seen as advice and they merrily keep trying to patch ...

Moving on ...
 



Sent with Proton Mail secure email.

On Tuesday, January 30th, 2024 at 5:23 PM, Alan Gauld via Tutor <tutor at python.org> wrote:

> 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
> 
> 
> 
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list