NTS Forums

Please login or register.

Login with username, password and session length
 

News:

Welcome to the Newtek Technology Services Forum!


Author Topic: Displaying a Binary Tree backwards in a table  (Read 946 times)

Offline paulin

  • Sr. Member
  • ****
  • Posts: 778
  • Karma: +0/-0
    • http://www.SPextreme.com
Displaying a Binary Tree backwards in a table
« on: July 23, 2003, 04:14:52 AM »
This is a stumper.  At least for me anyway.

OK.  I have a tree that holds a name, seed, scores, left node, and right node.

So the tree may look like:

       1
   1---|
   |   4
1---
   |   2
   2---|
       3


And so on down to a round of 16 or 32.


So now what I want to do is display this tree with leaf nodes on the left and the root on the right in an HTML table like so:

1---|
    1---
4---|  |
       |
       ---1
       |
2---|  |
    2---
3---|


So my first thought was create a 2 dimensional array (possibly jagged since the array is holding Strings. See previous post for that issue) for the total size of the table, populate the appropriate cells with the data then just loop on the array and output.  That would be all fine and good but I ran I ran into the problem of placing the data within the array in the correct locations.  I kept overstepping the bounds since I am having a problem changing locations(x,y) on different levels of the tree correctly.

So now I'm here asking if anyone has any good ides(better ones) on how I can do this.  I thought about a DataTable(thanks to Graham) but I am not sure if or how I can make it work.  So please any input would be appriciated.

Thanks in advance.
steve

_________________________________________________________
Stephen Paulin
paulin@spextreme.com
http://www.SPextreme.com

Developers resources, Beta Testing, News and much more.

Edited by - paulin on 07/23/2003  04:17:00
_________________________________________________________
Stephen Paulin
paulin@spextreme.com
http://www.SPextreme.com

Developers resources, Beta Testing, and much more.

Offline paulin

  • Sr. Member
  • ****
  • Posts: 778
  • Karma: +0/-0
    • http://www.SPextreme.com
Displaying a Binary Tree backwards in a table
« Reply #1 on: July 23, 2003, 10:03:09 AM »
OK well it seems I stumped everyone on that since I got no response.  Oh well. I went off and did it a different way.  So to anyone that is interested heres what I did.

I started by creating an array of ArrayLists.  From there for each depth of the tree I added it to the arraylist in the array location[] of the highest depth.  What I mean is I populated the array list in revers to the tree.  So if the depth was 5 levels then the root node was added to the ArrayList[4]. Next level was placed in ArrayList[3]

This got the data in a close format to the way I wanted it:
  • - 1, 16, 8, 9, 4, 13, 5, 12, etc...
  • [1] - 1, 8, 4, 5, etc..
    [2] - 1, 4, etc...
    [3] - 1, 2
    [4] - 1


    Then I created a few functions and loops to create the table based on this array of arraylists to display the data.

    Now all thats left is convert the tree data into data from a set of tables.  I will most likely be using a DataTable/Set for this.


    Steve


    _________________________________________________________
    Stephen Paulin
paulin@spextreme.com
http://www.SPextreme.com

Developers resources, Beta Testing, News and much more.
_________________________________________________________
Stephen Paulin
paulin@spextreme.com
http://www.SPextreme.com

Developers resources, Beta Testing, and much more.

Offline Ranch Warment

  • Hosting Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
Re: Displaying a Binary Tree backwards in a table
« Reply #2 on: June 02, 2016, 05:05:49 AM »
That second way of doing it makes more sense,  Just space out the nodes as you go if you want, because youve aloted the most brutal end of the tree a character apart instead of the first method, where the most brutal part is done last and will intersect itself.