Results 1 to 10 of 10

Thread: Graphing a Binary Tree (Due tomorrow)

  1. #1

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Unhappy Graphing a Binary Tree (Due tomorrow)

    Lets say I have developed a Binary Tree structure in a module creating my own user defined Type named "BTree"...

    My problem is... How can I graph this tree I created?
    I need to be able to graph or paint at least 4 levels. I would appreciate help.

    Thank you in advance


    This would be somewhat what I would need...
    O
    / \
    O O
    / \ / \
    O O O O
    / \ / \ / \ / \
    O O O OO OO O
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    That's what the treeview control is meant for.

  3. #3

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Talking

    I know, but this is a homework and I cannot go to my professor showing I used a control for what I was supposed to program...
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  4. #4
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    What would you like to know?
    Learn how you can draw pictures in VB or need to calculate the coordinates of items in the binary tree?

    The first one has been explained many times in this forum.
    The second one is pretty easy.
    Suppose you have a drawing box. W is the width. H is the height of every generation (level). The location of the first item will be (1/2 * W, H)
    Of the second level the coordinates will be (1/4 * W, 2 * H) and (3/4 * W, 2 * H).
    Third level: (1/8 * W, 3 * H), (3/8 * W, 3 * H), etc, etc.
    Fourth level: (1/16 * W, 4 * H), etc.
    Good luck!

  5. #5
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    You can also do this trick with paper.
    The orientation should be landscape (will be easier to fold)
    Draw four horizontal lines on it, spaced evenly. Fold your paper one time vertically. Unfold the paper. At the crossing line of the topmost line and the fold, draw a circle.
    Now, fold the paper again, using the same fold. Fold the paper another time, so you will have 3 parallel vertical folds. Unfold your paper. Draw circles where the first and third folds cross the second topmost line.
    Refold your paper twice, using the same folds. Fold it yet another time. Unfold it, and you'll see seven folds. Mark the crossings of the first, third, fifth and seventh folds (these are the new folds) and the third topmost line.
    The fourth time should be easy enough now

    You can do this for as many levels as you want, supposedly that you can still fold the paper more
    When you're done, draw a line from the topmost circle, to both circles at the next level. Those two circles should be connected with the four at the third level, two lines per second-level circle. Etc.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by riis
    What would you like to know?
    Learn how you can draw pictures in VB or need to calculate the coordinates of items in the binary tree?

    The first one has been explained many times in this forum.
    The second one is pretty easy.
    Suppose you have a drawing box. W is the width. H is the height of every generation (level). The location of the first item will be (1/2 * W, H)
    Of the second level the coordinates will be (1/4 * W, 2 * H) and (3/4 * W, 2 * H).
    Third level: (1/8 * W, 3 * H), (3/8 * W, 3 * H), etc, etc.
    Fourth level: (1/16 * W, 4 * H), etc.
    Good luck!

    I think I saw a pattern developing here......

    Given L = Level; H = Generation Height; W = Width of drawing area

    x = ( 1/(2^(L+1)) * W
    y = H * L

    But that only helps to get the co-ords of the left most node on each level.....
    maybe some one else can take it from there........
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Talking I don't know, guys...

    Don't think I sat down here waiting on you all to answer my question, I also developed my own trick...

    But there is a little detail on my trick and I wonder if it happens with yours too... What about if the tree is not completely filled? I mean, think there are gaps and some nodes have just one child. This is the code I used:


    VB Code:
    1. Public Sub ShowNodesInForm(MeForm As Form, TrN As Variant, ShpN As Variant, LnN As Variant)
    2. If Tree.Valid = True Then
    3.  Dim i As Long
    4.  
    5.  Set TreeN = TrN 'A label control Array
    6.  Set ShapeN = ShpN 'A shape control Array
    7.  Set LineN = LnN 'A line control Array
    8.  
    9.  DistDefault = TreeN(0).Width
    10.  
    11.  Set OCode = New OrdersCode 'This is to be able to call the code from the InOrder
    12.  CTree = Tree
    13.  
    14.  ReDim LastX(Tree.Height + 1)
    15.  ReDim DistNodes(Tree.Height + 1)
    16.  ReDim PosY(Tree.Height + 1)
    17.  
    18.  For i = 1 To Tree.Height + 1
    19.   DistNodes(i) = CalcDNode(Tree, i)
    20.   LastX(i) = CalcFirstX(DistNodes(i))
    21.   PosY(i) = 2 * DistDefault * (i - 1)
    22.  Next i
    23.  
    24.  InOrden Tree.Node, Tree.Root, OCode, "PrintNode"
    25.  
    26. Else
    27.  MsgBox "Error, the specified Tree is invalid", vbCritical, "Error"
    28. End If
    29. End Sub
    30.  
    31. Public Function CalcFirstX(DistNodes As Single) As Single
    32. If DistNodes = DistDefault Then
    33.  CalcFirstX = 0
    34. Else
    35.  CalcFirstX = (DistNodes - 1) / 2
    36. End If
    37. End Function
    38.  
    39. Public Function CalcDNode(Arbol As BTree, Level As Long) As Single
    40. If Arbol.Height - Level = -1 Then
    41.  CalcDNode = DistDefault
    42. Else
    43.  CalcDNode = 2 * CalcDNode(Arbol, Level + 1) + 1
    44. End If
    45. End Function
    46.  
    47. Public Sub InOrden(n() As BTNode, Num As Long, MyObject As Object, MyFun As String)
    48. If n(Num).ChildLeft >= 0 Then InOrden n(), n(Num).ChildLeft, MyObject, MyFun
    49. CallByName MyObject, MyFun, VbMethod, Num
    50. If n(Num).ChildRight >= 0 Then InOrden n(), n(Num).ChildRight, MyObject, MyFun
    51. End Sub
    52.  
    53. 'This is in the Class Module "OrdersCode"
    54.  
    55. Public Sub PrintNode(Num As Long)
    56. Dim Lvl As Long
    57. Lvl = CTree.Node(Num).Level
    58.  
    59. If Lvl <= 4 Then
    60.  TreeN(CG).Left = LastX(Lvl) '+ DistNodes(Lvl)
    61.  TreeN(CG).Top = PosY(Lvl)
    62.  ShapeN(CG).Left = TreeN(CG).Left
    63.  ShapeN(CG).Top = TreeN(CG).Top
    64.  'LineN(CG).Left = True " As you can see, I am working still on this
    65.  'LineN(CG).Top = True " Still working, don't mind this code
    66.  
    67.  LastX(Lvl) = TreeN(CG).Left + DistNodes(Lvl)
    68.  MsgBox "LastX(" & Lvl & ") = TreeN(" & CG & ") [" & TreeN(CG) & "] + DistNodes(" & Lvl & ") [" & DistNodes(Lvl) & "] = " & LastX(Lvl)
    69.  
    70.  TreeN(CG).Visible = True
    71.  TreeN(CG).Caption = CTree.Node(Num).Info(0)
    72.  ShapeN(CG).Visible = True
    73.  'LineN(CG).Visible = True
    74.  
    75.  CG = CG + 1
    76. End If
    77. End Sub
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  8. #8
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    If a node has zero or one children, then skip the missing children. That shouldn't be a problem, since you don't need extra space.
    Of course you can make it so that when a node has only one child, the X-position of the child is the same as its parent.

  9. #9

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Thumbs up Thank you!

    Because of you two I could develop my own code. But I would like to see what you stated as pseudo or already coded, it sounds interesting.

    Here I will add the code I used... It was simple... I felt bad I did not think about it before. I used the following reasoning:

    1.- Print the Root (Calculate where the root should be using the levels and the following recursive formula: Dist(level) = 2*Dist(level+1) - 1*Unit. The exit condition would be If the level given is the maximum level of the tree, then: Dist(MaxLevel) = 1*Unit. Then simply draw it in y = 0*unit, x = (Dist(level) -1 * unit)/2 where level will be 1, of course.)
    2.- Now you go through the tree using the Pre-Order starting from the root and:
    a) If the node has a Left Child, print the Node in y = 2*(level-1) units and x = node's Parent Left + 1/2* node's Parent Width - (Dist(level) - 1 * unit)/2. Now point a line from the node's Parent to the node.
    b) If the node has a Right Child, print the node in the same y, and x = node's Parent Left + 1/2 * node's Parent Width + (Dist(level) - 1 * unit)/2. Now point a line from the node's Parent to the node.

    Anyway, a doubt comes to me... What can I do if the nodes are graphied out of the form? Isn't there a way to be able to add scrolling bars so you can see all the way right until the right-most node and all the way down until the down-most node?

    And also, how can I create another instance of the Label array I am using? Thanks in advance...

    Code:


    VB Code:
    1. Public Tree As BTree 'The Tree created by Me
    2. Public CTree As BTree 'The Tree created by Me
    3. Public OCode As Object
    4.  
    5. Public CG As Integer
    6. Public TreeN As Variant 'Label Array to contain the Info
    7. Public ShapeN As Variant 'Shape Array: Circle
    8. Public LineN As Variant 'Line Array to point to the "node"
    9.  
    10. Public DistDefault As Single
    11. Public MaxLevel As Long
    12.  
    13. Public LastX() As Single
    14. Public DistNodes() As Single
    15. Public PosY() As Single
    16.  
    17. Public Sub ShowNodesInForm(MeForm As Form, TrN As Variant, ShpN As Variant, LnN As Variant)
    18. If Tree.Valid = True Then
    19.  Dim i As Long
    20.  
    21.  Set TreeN = TrN
    22.  Set ShapeN = ShpN
    23.  Set LineN = LnN
    24.  
    25.  DistDefault = TreeN(0).Width
    26.  
    27.  Set OCode = New OrdersCode
    28.  CTree = Tree
    29.  
    30.  'MaxLevel = Tree.Height + 1 'Normal
    31.  MaxLevel = 4
    32.  
    33.  ReDim LastX(MaxLevel)
    34.  ReDim DistNodes(MaxLevel)
    35.  ReDim PosY(MaxLevel)
    36.  
    37.  For i = 1 To MaxLevel
    38.   DistNodes(i) = CalcDNode(Tree, i)
    39.   LastX(i) = CalcFirstX(DistNodes(i))
    40.   PosY(i) = 2 * DistDefault * (i - 1)
    41.  Next i
    42.  
    43.   CG = 0
    44.  
    45.   Tree.Node(Tree.Root).GraphNode = CG
    46.   TreeN(CG).Left = LastX(1)
    47.   TreeN(CG).Top = (Tree.Node(Tree.Root).Level - 1) * 2 * DistDefault
    48.   ShapeN(CG).Left = TreeN(CG).Left
    49.   ShapeN(CG).Top = TreeN(CG).Top
    50.  
    51.   TreeN(CG).Top = TreeN(CG).Top + 90
    52.  
    53.   TreeN(CG).Visible = True
    54.   TreeN(CG).Caption = Tree.Node(Tree.Root).Info(0)
    55.   ShapeN(CG).Visible = True
    56.   LineN(CG).Visible = False
    57.   CG = CG + 1
    58.  
    59.  PreOrden Tree.Node, Tree.Root, OCode, "PrintNode"
    60. Else
    61.  MsgBox "Error, not a valid Tree", vbCritical, "Error"
    62. End If
    63. End Sub
    64.  
    65. Public Function CalcFirstX(DistNodes As Single) As Single
    66. If DistNodes = DistDefault Then
    67.  CalcFirstX = 0
    68. Else
    69.  CalcFirstX = (DistNodes - 1 * DistDefault) / 2
    70. End If
    71. End Function
    72.  
    73. Public Function CalcDNode(Arbol As BTree, Nivel As Long) As Single
    74. If MaxLevel - Nivel = 0 Then
    75.  CalcDNode = DistDefault
    76. Else
    77.  CalcDNode = (2 * CalcDNode(Arbol, Nivel + 1)) + 1 * DistDefault
    78. End If
    79. End Function
    80.  
    81. Public Sub PreOrden(n() As BTNode, Num As Long, MyObject As Object, MyFun As String)
    82. CallByName MyObject, MyFun, VbMethod, Num 'PreOrder
    83. If n(Num).ChildLeft >= 0 Then PreOrden n(), n(Num).ChildLeft, MyObject, MyFun
    84. If n(Num).ChildRight >= 0 Then PreOrden n(), n(Num).ChildRight, MyObject, MyFun
    85. End Sub
    86.  
    87. '---- This is inside of a Class Module called "OrdersCode" ----
    88.  
    89. Public Sub PrintNode(Num As Long)
    90. Dim Lvl As Long
    91.  
    92. With CTree
    93. Lvl = .Node(Num).Level
    94.  
    95. If Lvl < MaxLevel Then
    96.  Dim Dist As Single
    97.  Dim ChL As Long
    98.  Dim ChR As Long
    99.  
    100.  ChL = .Node(Num).ChildLeft
    101.  ChR = .Node(Num).ChildRight
    102.  
    103.  If ChL <> -1 Then
    104.   Dist = (DistNodes(Lvl + 1) + 1 * DistDefault) / 2
    105.  
    106.   .Node(ChL).GraphNode = CG
    107.   TreeN(CG).Left = TreeN(.Node(Num).GraphNode).Left - Dist
    108.   TreeN(CG).Top = (.Node(ChL).Level - 1) * 2 * DistDefault
    109.   ShapeN(CG).Left = TreeN(CG).Left
    110.   ShapeN(CG).Top = TreeN(CG).Top
    111.   LineN(CG).X1 = TreeN(CG).Left + (DistDefault / 2)
    112.   LineN(CG).X2 = TreeN(.Node(Num).GraphNode).Left + (DistDefault / 2)
    113.   LineN(CG).Y1 = TreeN(CG).Top
    114.   LineN(CG).Y2 = TreeN(.Node(Num).GraphNode).Top + (DistDefault / 2)
    115.  
    116.   TreeN(CG).Top = TreeN(CG).Top + 90
    117.  
    118.   TreeN(CG).Visible = True
    119.   TreeN(CG).Caption = .Node(ChL).Info(0)
    120.   ShapeN(CG).Visible = True
    121.   LineN(CG).Visible = True
    122.   CG = CG + 1
    123.  End If
    124.  
    125.  If ChR <> -1 Then
    126.   .Node(ChR).GraphNode = CG
    127.   TreeN(CG).Left = TreeN(.Node(Num).GraphNode).Left + Dist
    128.   TreeN(CG).Top = (.Node(ChR).Level - 1) * 2 * DistDefault
    129.   ShapeN(CG).Left = TreeN(CG).Left
    130.   ShapeN(CG).Top = TreeN(CG).Top
    131.   LineN(CG).X1 = TreeN(CG).Left + (DistDefault / 2)
    132.   LineN(CG).X2 = TreeN(.Node(Num).GraphNode).Left + (DistDefault / 2)
    133.   LineN(CG).Y1 = TreeN(CG).Top
    134.   LineN(CG).Y2 = TreeN(.Node(Num).GraphNode).Top + (DistDefault / 2)
    135.  
    136.   TreeN(CG).Top = TreeN(CG).Top + 90
    137.  
    138.   TreeN(CG).Visible = True
    139.   TreeN(CG).Caption = .Node(ChR).Info(0)
    140.   ShapeN(CG).Visible = True
    141.   LineN(CG).Visible = True
    142.   CG = CG + 1
    143.  End If
    144.  
    145. End If
    146.  
    147. End With
    148. End Sub
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  10. #10

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Talking Duh...

    Ignore my other doubts, I would just like to see the other way to graph the nodes in pseudo-code or in VB Code...

    My method now is working and I could post "Solved" but before that... I would love to see your method working too. I consider it very interesting
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width