PDA

View Full Version : i need help in c++ "binary tree"




alkansa
Dec 3, 2010, 04:29 AM
[FONT="System"][SIZE="3"]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


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

thank you



Littleodie914
Dec 3, 2010, 08:34 AM
I need to add this option

cout<<"\n\t5) draw tree";

i nee this functionNo, you need to either consult your textbook, or contact your professor if you are having issues understanding course material. ;)

jared_kipe
Dec 3, 2010, 09:51 AM
struct node * root = '\0';

Awesome.

alkansa
Dec 3, 2010, 10:03 AM
I need to help, not the subject of a relationship seminar materials

chown33
Dec 3, 2010, 11:12 AM
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.

alkansa
Dec 3, 2010, 02:29 PM
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

mrbash
Dec 3, 2010, 03:43 PM
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.

ulbador
Dec 3, 2010, 03:47 PM
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.

alkansa
Dec 3, 2010, 04:08 PM
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

ulbador
Dec 3, 2010, 04:29 PM
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.

jared_kipe
Dec 3, 2010, 04:35 PM
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.

jared_kipe
Dec 3, 2010, 04:38 PM
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....

ulbador
Dec 3, 2010, 04:57 PM
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.

chown33
Dec 3, 2010, 05:06 PM
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.

jared_kipe
Dec 4, 2010, 12:48 AM
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.

alkansa
Dec 4, 2010, 04:42 AM
I am I will work to solve this code and the code, I'm a gift for those who want

holmesf
Dec 4, 2010, 04:35 PM
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!

Mac_Max
Dec 6, 2010, 04:04 AM
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.

holmesf
Dec 6, 2010, 04:05 AM
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?

jared_kipe
Dec 6, 2010, 09:10 PM
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.

jared_kipe
Dec 7, 2010, 02:25 PM
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.

7
5 8
-1 8 9
-2 0 8 9 13
-3 3 9 10 15
-4 -2 2 4 9 11 14 16