LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2002, 05:22 PM   #1
JMC
Member
 
Registered: Apr 2002
Distribution: Mandrake 8.2
Posts: 43

Rep: Reputation: 15
Printing a binary tree in c?


Anyone happen to know how to pring a binary tree in c as to get the format

Code:
            +
          /   \
        *      c
      /   \
     a     b
My data structure is set up as follows:

struct Node {

ptr left;
char value;
ptr right;
};

Thanks for any help.
 
Old 07-02-2002, 08:37 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
You need to add a few more specifics. Do you want it printed as a tree or on a singe line. Prefix, Infix, postfix? How do you load the tree. No matter how you need it printed, a recursive function is the best way to walk the tree.
 
Old 07-02-2002, 09:04 PM   #3
JMC
Member
 
Registered: Apr 2002
Distribution: Mandrake 8.2
Posts: 43

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by crabboy
You need to add a few more specifics. Do you want it printed as a tree or on a singe line. Prefix, Infix, postfix? How do you load the tree. No matter how you need it printed, a recursive function is the best way to walk the tree.
I can print it in a line in any fashion ( pre, in, post, breadth, depth..etc ). I was wanting to print it just like I posted the example above. Just a easier to read fashion. I know there is a way you can manipulate it with spaces and newlines using the depth of the tree but I can't recall exactly how to do it.
 
Old 07-03-2002, 02:13 AM   #4
Rum
Member
 
Registered: Jun 2002
Posts: 34

Rep: Reputation: 15
okay, this just some vague hint:
to be able to position your root in the center first,
you need to know the width of the tree - ie., how many
leaves are there at the lowest level - this is proportional
to the height.
second, for the links you can use "\" and "/"
 
Old 09-26-2003, 08:51 AM   #5
chetan13
LQ Newbie
 
Registered: Jun 2003
Posts: 7

Rep: Reputation: 0
static int readTree(BinaryTreePtr presentNode,int max,int row,int col,int a[44][120]){// reads the tree into the array
// a[44][120]; //a simulates the physical screen
int i=0;
int m=0,n=0; //flags for sending error signal
int r=row; //r changes to the next row
if(presentNode==NULL)// if the tree(not a subtree) doesn't have any elements at all
return -1;

if(max==1&&(presentNode->leftSubtree!=NULL||presentNode->rightSubtree!=NULL))//max denotes the number of
//slashes to the next element. if it is zero and yet not a leaf then dont print the graphical tree
return -1;
a[row][col]=presentNode->nodeValue;//sets the value on the screen
if(presentNode->leftSubtree!=NULL){
while(i++<max)
a[++r][col-i]='/';
m= readTree(presentNode->leftSubtree,max/2,r+1,col-max-1,a);
}
i=0;
r=row;
if(presentNode->rightSubtree!=NULL){
while(i++<max)
a[++r][col+i]='\\';
n= readTree(presentNode->rightSubtree,max/2,r+1,col+max+1,a);
}
if(m==-1||n==-1)
return -1;
else
return 0;
}

static int printTree(BinaryTreePtr presentNode){
int i,j;
int a[44][120];
for(i=0;i<44;i++){
for(j=0;j<120;j++){
a[i][j]=0;
}
}
if(-1!=readTree(presentNode,16,0,60,a)){// -1 means it cannot print graphical form of tree
for(i=0;i<37;i++){
for(j=0;j<100;j++){//not 120 so that spaces dont shoot off onto the next line
if(i==0||i==17||i==26||i==31||i==34||i==36){//only on these lines integers exist(other lines have only slashes)
if(a[i][j]==0)
printf(" "); //print spaces for uninitialized elements
else
printf("%d",a[i][j]);
}
else {
if((a[i][j]==0))
printf(" ");
else
printf("%c",a[i][j]);
}
}
printf("\n");
}
}
else
printf("Cannot print graphical form due to insufficient screen size\n");
}
 
Old 09-26-2003, 11:02 AM   #6
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Hello chetan13

Welcome to LQ

To make your code readable to all :-

Begin your code with
squarebracket CODE squarebracket
Then type....
Whatever your code might be...
then type
square bracket /CODE square bracket . Then the code will be of this format

Code:
Your code
PS :- Edit your previous post to make the necessary changes...

Last edited by SaTaN; 09-26-2003 at 11:05 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
representation of binary tree using array sajith Programming 3 10-06-2005 10:59 PM
Scribus Printing How to put 4 flyers on a page when printing jazzboy Linux - Software 0 05-02-2004 06:13 AM
the bible = the tree of the knowledge of good and evil () Jesus = the tree of life Michael111 General 2 04-14-2004 04:28 PM
Binary search tree insertion in java ksgill Programming 6 02-12-2004 05:11 PM
need a P-Tree (Patricia Tree) library manaskb Programming 1 11-02-2002 06:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration