[Python] representing a tree as a list in python

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm learning python and I'm curious about how people choose to store (binary) trees in python.

    Is there something wrong in storing the nodes of the tree as a list in python? something like:

    [0,1,2,3,4,5,6,7,8]


    where the 0'th position is 0 by default, 1 is the root, and for each position (i), the 2i and 2i+1 positions are the children. When no child is present, we just have a 'None' in that position.

    I've read a couple of books/notes where they represent a tree using a list of lists, or something more complicated than just a simple list like this, and I was wondering if there's something inherently wrong in how i'm looking at it?

    Continue reading...

Compartilhe esta Página