Ask Your Question

Use a Binary Tree

asked 2015-02-28 13:27:34 -0500

Joe gravatar image

updated 2015-05-22 13:47:24 -0500

FrédéricC gravatar image

Hi folks,

newbie alert.

I want to have a binary tree with a single integer value at each node. I'm looking at BinaryTree() but this seems to have the structure of a tree but without values!!!

Please point me in the right direction. I don't mind creating a class from scratch if I can just make a start.

Thanks Joe

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2015-02-28 17:19:01 -0500

You should have a look at LabelledBinaryTree

sage: t1 = LabelledBinaryTree([None, None], label=3)
sage: t2 = LabelledBinaryTree([None, None], label=4)
sage: t = LabelledBinaryTree([t1,t2], label=-2)
sage: t
-2[3[., .], 4[., .]]

And easier to read

sage: ascii_art(t)
 /  \
3    4
edit flag offensive delete link more


Perfect - just what I was looking for. Thank you for your help and example.

Joe gravatar imageJoe ( 2015-03-02 16:38:56 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-02-28 13:27:34 -0500

Seen: 119 times

Last updated: Feb 28 '15