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

MRAB python at mrabarnett.plus.com
Sun Jan 28 14:23:59 EST 2024


On 2024-01-28 18:16, marc nicole via Python-list wrote:
> So I am trying to build a binary tree hierarchy given numerical elements
> serving for its leaves (last level of the tree to build). From the leaves I
> want to randomly create a name for the higher level of the hierarchy and
> assign it to the children elements. For example: if the elements inputted
> are `0,1,2,3` then I would like to create firstly 4 elements (say by random
> giving them a label composed of a letter and a number) then for the second
> level (iteration) I assign each of 0,1 to a random name label (e.g. `b1`)
> and `2,3` to another label (`b2`) then for the third level I assign a
> parent label to each of `b1` and `b2` as `c1`.
> 
> An illustation of the example is the following tree:
> 
> 
> [image: tree_exp.PNG]
> 
This list strips images, and discussion has mostly moved to 
https://discuss.python.org.



More information about the Python-list mailing list