473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How do you guys print out a binary tree?

There are many ways to represent a binary tree on an
ascii screen.

1
/ \
2 3
/ \ / \
4 5 6 7

or

4---2-----------1
| |
5 6----- 3
|
7

Suppose I have a function that takes a matrix like
this one:

1 2 3 4 5
0 7 8 9 10
0 0 13 14 15
0 0 0 19 20
0 0 0 0 25

Look at the triangle represented by the non-zero
integers. This triangle is a binary tree if we take 5
as the root and walk down on both sides.

How do I print out this triangle on an ascii console
and visually present it as a binary tree.

Any suggestions?

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Apr 18 '06 #1
4 4035
Hi,
1 2 3 4 5
0 7 8 9 10
0 0 13 14 15
0 0 0 19 20
0 0 0 0 25
Look at the triangle represented by the non-zero
integers. This triangle is a binary tree if we take 5
as the root and walk down on both sides.


Are you sure? Is 9 a child of 4 or 10? A binary tree can have up to
2^n - 1 nodes. A matrix can have up to n^2 values, in your case of a
half-empty matrix about (n-1)^2.

Apr 18 '06 #2
A half-empty matrix will of course have (n+1)* n * 1/2 elements.

Apr 18 '06 #3
The problem is that you cannot represent a matrix as a tree, due to the
fact that there are more than one tree for a matrix.

First you have to decide, how you will turn the matrix into a tree.

Apr 19 '06 #4
On Tue, 18 Apr 2006 08:17:22 -0700 (PDT) in comp.lang.python, Anthony
Liu <an***********@yahoo.com> wrote:


--- bayerj <ba****@in.tum.de> wrote:
Hi,
> 1 2 3 4 5
> 0 7 8 9 10
> 0 0 13 14 15
> 0 0 0 19 20
> 0 0 0 0 25
> Look at the triangle represented by the non-zero
> integers. This triangle is a binary tree if we

take 5
> as the root and walk down on both sides.


Are you sure? Is 9 a child of 4 or 10? A binary
tree can have up to
2^n - 1 nodes. A matrix can have up to n^2 values,
in your case of a
half-empty matrix about (n-1)^2.


Thanks. I am not concerned about the shape of binary
tree. So, let's forget about binary tree.

Given a triangle like that, it does not matter which
is whose children. How do we nicely present it as
tree in an ascii console?


Something like the following might work. Quick 2-minute script.
Probably needs tweaking to be generally useful

import sys
def print_tri(t):
n = len(t)
cell = 0
for line in t:
tw = max(map(lambda x:len(str(x)), line))
if tw > cell:
cell = tw
for p in range(n,0,-1):
sys.stdout.write("%*s"%(((cell+1)/2)*(2*p),""))
x = 0
y = p-1
while y<n:
s = str(t[x][y])
b = (cell-len(s))/2
sys.stdout.write("%*s%*s"%(b,s,cell-b,""))
x += 1
y += 1
sys.stdout.write("\n")

Regards,
-=Dave

--
Change is inevitable, progress is not.
Apr 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
20
by: pc_newbie | last post by:
is there any algorithm to create a breadth-first order binary tree?
5
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
15
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
2
by: pyguy | last post by:
Hi all, I am running into a conceptual glitch in implementing a simple binary tree class. My insertion and printing (sorting) seems to be ok, but when I search the tree, my find method isn't doing...
4
by: hankssong | last post by:
Hi everyone, I'm wondering whether it's possible to construct a binary-tree using python. Since python don't have pointer, it can't dynamically allocate memory like C language. But some important...
4
by: whitehatmiracle | last post by:
Hello I have written a program for a binary tree. On compiling one has to first chose option 1 and then delete or search. Im having some trouble with the balancing function. It seems to be going...
6
by: APEJMAN | last post by:
would you please help me? I wrote 3 separate line of code for printing my binary tree, and now I am trying to print the level-order traversal of the tree, where the nodes at each level of the tree...
2
by: cioccolatina | last post by:
Hey guys, is there anyone who could help me..? I have file ExpressionBinaryTree.java : /** class ExpressionBinaryTree * uses a binary tree to represent binary expressions * does not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.