Ask Your Question
1

Use a Binary Tree

asked 10 years ago

Joe gravatar image

updated 9 years ago

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

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 10 years ago

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
Preview: (hide)
link

Comments

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

Joe gravatar imageJoe ( 10 years ago )

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: 10 years ago

Seen: 666 times

Last updated: Mar 01 '15