Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

alkansa

macrumors newbie
Original poster
Dec 3, 2010
5
0
Dear Coder

I want you to assist in this code ... Benary see this code ... I want to help me to add a function to draw a tree in this way

Code:
      7
     / \
    /   \
   5     8
  /\     /\
 /  \      \
-1   0    9
 /\    \     \
-2       3   13
            \
           4

thank you
 
Last edited:
I need to help, not the subject of a relationship seminar materials
 
Post your code.

If you don't have code, post your design.

If you don't have a design, post whatever you've created that isn't just a statement of your homework.

If you don't have anything but what you've already posted, google print binary tree ascii

This forum is not a producer of homework answers.
 
I am what I am asking is not the duty of teachers and it is a love for learning and after you try the code in the Hills for a period of weeks, decided to outsource those who are more experience than me
 
I am what I am asking is not the duty of teachers and it is a love for learning and after you try the code in the Hills for a period of weeks, decided to outsource those who are more experience than me

Is this a copy/paste from Google Translate? I'm having a hard time understanding what you are saying. Please use smaller sentences.
 
Is this a copy/paste from Google Translate? I'm having a hard time understanding what you are saying. Please use smaller sentences.

That is a pretty wild sentence. Even for a non-native English speaker.
 
I say I do not want to code
I asked and you wait for answers ... But strange that I find all this criticism of you

Thank you very much consider me and did not ask for help
 
I say I do not want to code
I asked and you wait for answers ... But strange that I find all this criticism of you

Thank you very much consider me and did not ask for help

For something like this, you want to use a doubly-linked list and a recursive function to print it out.
 
Keep in mind, I've never printed a tree like this.

Ok, first things first, I believe you will need to know how deep the tree is to accomplish that.

Your code is now gone, but I don't remember you having a method that returns how tall the tree is.
 
For something like this, you want to use a doubly-linked list and a recursive function to print it out.

Interesting, in my mind I'd think it would be possible with only the height of the tree, and a function prototyped something like printList(void *list, int level, int height)

.... maybe I'll start playing around with some C code....
 
Interesting, in my mind I'd think it would be possible with only the height of the tree, and a function prototyped something like printList(void *list, int level, int height)

.... maybe I'll start playing around with some C code....

I actually had to write EXACTLY this (albiet in PHP and Java which made it infinitely easier, though pointers would have been infinitely more efficient) for a work project about 5 years back.

I spent about 3 months trying to conceptualize this and how to add/remove/collapse nodes on the tree and the double linked list and recursion was about the most effective method I came up with. Then, once the tree was built, I stuck a MASSIVE index on it so you could tell what level what node was on from any other node. Basically my tree had to have to ability to call any specific node "level 1" and start building a new tree from there.

It got horrible unmanageable by about the 30th level from the top. So yeah, the recursion and double linked list only worked so deep.
 
Interesting, in my mind I'd think it would be possible with only the height of the tree, and a function prototyped something like printList(void *list, int level, int height)

.... maybe I'll start playing around with some C code....

Or google print binary tree ascii

When I do, this is the top hit:
http://stackoverflow.com/questions/801740/c-how-to-draw-a-binary-tree-to-the-console

It has some good discussion and analysis, and some possibly good-looking links, but oops, a promising one is dead. But maybe not completely dead yet, there's an update:
http://datastructuresblog.wordpress.com/2007/12/21/printing-binary-trees-in-ascii/

So with minimal googling, and a little reading, it looks like there's an example with code.
 
Or google print binary tree ascii

When I do, this is the top hit:
http://stackoverflow.com/questions/801740/c-how-to-draw-a-binary-tree-to-the-console

It has some good discussion and analysis, and some possibly good-looking links, but oops, a promising one is dead. But maybe not completely dead yet, there's an update:
http://datastructuresblog.wordpress.com/2007/12/21/printing-binary-trees-in-ascii/

So with minimal googling, and a little reading, it looks like there's an example with code.


Sometimes it's not about efficient googling, it's about doing something for yourself for the challenge of doing it.
 
I am I will work to solve this code and the code, I'm a gift for those who want
 
I actually had to write EXACTLY this (albiet in PHP and Java which made it infinitely easier, though pointers would have been infinitely more efficient) for a work project about 5 years back.

I spent about 3 months trying to conceptualize this and how to add/remove/collapse nodes on the tree and the double linked list and recursion was about the most effective method I came up with. Then, once the tree was built, I stuck a MASSIVE index on it so you could tell what level what node was on from any other node. Basically my tree had to have to ability to call any specific node "level 1" and start building a new tree from there.

It got horrible unmanageable by about the 30th level from the top. So yeah, the recursion and double linked list only worked so deep.

That is most certainly not the most efficient way to do this task. Just do a depth first search. For the queue involved in the DFS, rather than inserting nodes directly, wrap them in a secondary structure with information about their relation to the parent (left or right child) and current drawing position. That way when it comes time to visit the child nodes and draw them the correct actions can be taken. I really hope you didn't spend 3 months on that task! Sometimes it's better just to ask somebody!
 
Last edited:
Why not just write out XML descriptor tags to a file as you traverse the tree. That seems pretty logical to me. It's one way MS allows the generation of Tree like views in ASP.net.
 
Why not just write out XML descriptor tags to a file as you traverse the tree. That seems pretty logical to me. It's one way MS allows the generation of Tree like views in ASP.net.

Because he's using in C++ and wants to draw the tree in a terminal?
 
I was able to print a tree like that (without dashes downwards) using straight C, and only calculating the height of the tree once + walking the tree using the current level and shifts with bit comparators. I didn't need any doubling linked list or other storage structure.

My only real problem is that "spaces" are not the most accurate way of counting across a screen. Meaning that the larger the tree, the greater the probability that some nodes will not line up right because of compounding shifting errors.
 
Oh I had a good idea on how to lay out the spaces so that I always calculate the closest space, and it works really really well.


Here is my output of a rather big tree. The right side is MUCH MUCH better than my previous method as that compounding rounding errors would screw of the positioning of the right side much more than the left.
Code:
                                                               7
                                          5                                         8
                        -1                                                  8                        9
            -2             0                                          8                            9            13
      -3                     3                                                                   9             10     15
  -4  -2                  2   4                                                                 9                 11  14  16
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.