LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Printing a binary tree in c? (https://www.linuxquestions.org/questions/programming-9/printing-a-binary-tree-in-c-24888/)

JMC 07-02-2002 05:22 PM

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.

crabboy 07-02-2002 08:37 PM

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.

JMC 07-02-2002 09:04 PM

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. :o

Rum 07-03-2002 02:13 AM

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 "/"

chetan13 09-26-2003 08:51 AM

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");
}

SaTaN 09-26-2003 11:02 AM

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...


All times are GMT -5. The time now is 08:53 AM.