Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Insert children of a specified node in a binary tree and retrieve all leaves of a binary tree.

I am trying to use a binary tree to store data. When different cases happens at some step, I will need to create two children from the current node.

I also need to collect all information on the leaves (nodes with no children). I use the following definition of binary trees:

from sage.misc.binary_tree import BinaryTree
T = BinaryTree();

I can insert node the following way:

t.insert(0,[1,2,4]);
t.insert(1,[4,5]);
t.insert(2,[5,0]);

But I couldn't find a way to visualize the tree. Also I don't know how to add two children of one certain node. For example, if 0 is the root (it seems so), 1 is its left child and 2 is its right child (which I am not sure), how do I make sure I add two children for the node 2?

I also looked at this page: (http://doc.sagemath.org/html/en/reference/combinat/sage/combinat/binary_tree.html). I couldn't see how to insert a node with values in it, or how to retrieve information of leaves.

Do I have to define my own tree structure?

Thank you for your help!