Ask Your Question
1

Use a Binary Tree

asked 2015-02-28 20:27:34 +0100

Joe gravatar image

updated 2015-05-22 20:47:24 +0100

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
1

answered 2015-03-01 00:19:01 +0100

vdelecroix gravatar image

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)
  -2
 /  \
3    4
edit flag offensive delete link more

Comments

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

Joe gravatar imageJoe ( 2015-03-02 23:38:56 +0100 )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

Stats

Asked: 2015-02-28 20:27:34 +0100

Seen: 631 times

Last updated: Mar 01 '15