i need help in c++ "binary tree"

Discussion in 'Mac Programming' started by alkansa, Dec 3, 2010.

  1. alkansa, Dec 3, 2010
    Last edited: Dec 3, 2010

    macrumors newbie

    Joined:
    Dec 3, 2010
    #1
    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
     
  2. macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #2
    No, you need to either consult your textbook, or contact your professor if you are having issues understanding course material. ;)
     
  3. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
  4. thread starter macrumors newbie

    Joined:
    Dec 3, 2010
    #4
    I need to help, not the subject of a relationship seminar materials
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    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.
     
  6. thread starter macrumors newbie

    Joined:
    Dec 3, 2010
    #6
    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
     
  7. macrumors 6502

    Joined:
    Aug 10, 2008
    #7
    Is this a copy/paste from Google Translate? I'm having a hard time understanding what you are saying. Please use smaller sentences.
     
  8. macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #8
    That is a pretty wild sentence. Even for a non-native English speaker.
     
  9. thread starter macrumors newbie

    Joined:
    Dec 3, 2010
    #9
    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
     
  10. macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #10
    For something like this, you want to use a doubly-linked list and a recursive function to print it out.
     
  11. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #11
    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.
     
  12. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #12
    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....
     
  13. macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #13
    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.
     
  14. macrumors 603

    Joined:
    Aug 9, 2009
    #14
    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.
     
  15. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #15

    Sometimes it's not about efficient googling, it's about doing something for yourself for the challenge of doing it.
     
  16. thread starter macrumors newbie

    Joined:
    Dec 3, 2010
    #16
    I am I will work to solve this code and the code, I'm a gift for those who want
     
  17. holmesf, Dec 4, 2010
    Last edited: Dec 4, 2010

    macrumors 6502a

    Joined:
    Sep 30, 2001
    #17
    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!
     
  18. macrumors 6502

    Joined:
    Mar 8, 2004
    #18
    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.
     
  19. macrumors 6502a

    Joined:
    Sep 30, 2001
    #19
    Because he's using in C++ and wants to draw the tree in a terminal?
     
  20. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #20
    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.
     
  21. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #21
    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
    
    
     

Share This Page