ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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 "/"
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");
}
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.