Wrox Programmer Forums
|
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 14th, 2007, 06:26 AM
Registered User
 
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Grandpa tree

imagen old and nice grandpa he got 5 sons and 4 daughters
(4+5=9)
every one of the sons got 5 sons
(5x5=25)
and every one of the daughters got 2 sons and 2 daughters
(2+2=4)
(4x4=16)
that means the grandpa got 16+25=41 grandchildren...

=>on the 1(first) level he got 9people
=>on the 2(second) level he got 41people

and may be some of the children got sons or daughters too...in that case he will got a 3(third) level too...


in my asp files, there are no replay of names=every body got his original name wich no one esle have.
i enter to the database :
"first name" "fathers name"

i need to build some circulation function that counts how much levels granpa have and how much people he got on every level.

someone wrote/saw something like this before?
any help will help right now:)

 
Old October 15th, 2007, 11:31 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There are numerous ways to go about doing this, and assuming that things are as you say and that a name will never be repeated it shouldn't be difficult.

One way to do this is with a Tree structure. You could build the tree from the database records in several ways, and it is a bit too complicated to cover completely here. Each node in the tree is an object that represents one person, and contains a dictionary of objects that are the children of that person. For example, you would create a "grandpa" object, retrieve all records that are "children" of the grandpa and make an object for each one, adding it to the dictionary of children for grandpa and recursively repeating the process for that childs children... and so on for each child until there are no results in a query for that childs children. Make sense?

There are other ways to approach this, of course.







Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems
 
Old October 16th, 2007, 12:35 AM
Registered User
 
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i can understand what you say and the logic of it but can you show me
links or some main part of such code in asp.

i tried to stor in ech name the number of level...for example if the grandpa level=1 than his son will be level 1+1=2 and the son's son will be 2+1=3 level...and so on...

i wrote a "for" from 1 to the maximum char and dysplays it,
the problem is that i say every name on ech level but not under the relevant father...

how can i view it like a real tree?

 
Old October 16th, 2007, 05:03 AM
Registered User
 
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i wrote this code but it shows all the rows in one column
how can i make this structure?

Code:
     :)
     ||
   :D [8)]
    /|
[:0]         [:p]

and so on...
Code:
dim level
sub GetTree(code)


dim mySQL, conntemp1, rst1
mySQL1="SELECT * FROM mag WHERE father='" & code & "'"
call getFromDatabase(mySQL, rst1, "ciss.asp") 


    if ((not rst1.EOF) and (not rst1.BOF)) then

    while  not rst1.EOF

       for cc=1 to level
        Response.Write "&nbsp"
        Response.Write "&nbsp"
       next 

        Response.Write  rst1("Name")
        Response.Write "<br>"
     level = level + 1   
     GetTree(rst1("code"))
     level = level - 1

    rst1.movenext
    wend 
    end  if 

    Set rst1 = Nothing
end sub





level = 1


dim vab, mySQL, conntemp, rst
vab="grand"
mySQL="SELECT * FROM mag WHERE code='" & vab & "'"
call getFromDatabase(mySQL, rst, "citttss.asp")



rst.movefirst

level=0
while  not rst.EOF
  
  for cc=1 to level
   Response.Write "&nbsp"
   Response.Write "&nbsp"
  next 
 
   Response.Write  rst("Name")
   Response.Write "<br>"
 
 level=level+1
 GetTree(rst("code"))
 level=level-1
 
 rst.movenext
 
wend
 
Old October 17th, 2007, 10:44 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One way to display a tree would be so that it looks like this:
grandpa
- Bob
- - Sally
- - Mary
- Wilbur
- - Tim
- - Albert
- - - Andy
- - - Terry
- - Ryan
- - - Greg
- Ruth
- - Jeff
- - Sally

Similar to a "tree-view" control. Conceptually, this is how a lot of menus are displayed.
This can be done with the simple dashes as I show, or with columns and rows in a table
Displaying a real tree-like structure as you describe but that is a lot more work. It certainly could be done, but will require some sophisticated manipulation of html tables. You would probably want to develop some images that allow you to construct tables that display the names and interconnecting lines. An easy way to look at the source html at sites that display this sort of hierarchical info: ancestry, dog breeding, sports championships or something like that.

As far as the code goes, you have pretty much coded the basic idea from my earlier post. Using a recursive pattern is typically how this sort of thing is done, just as you have done.

Also, there are other approaches. You could build a tree structure in code (using objects and dictionaries - this is known as a composite pattern). Once you have that, you just "traverse the tree" and build the html output as you go.

There are many approaches to building the tree structure. The psuedo code for one way is as follows:
Retrieve all rows in a single db query
Loop through the records, and for each record
- create a "person node" composite object which is a class you would have to define that contains a field for the name, a field for the parent name, and a dictionary of person objects which are the children of this person, and methods for adding children and displaying itself.
- add it to an "all persons" dictionary keyed by the name

Then loop through the dictionary and for each person object:
- If the parent field contains a value (all records but "grandpa" will)
- - Get the perons object from the dictionary for that parent
- - Add a reference to the person object to the parent objects child dictionary by calling the addChild method you wrote on the person class
- - Optionally, you can also add a reference to the parent in the child. This sort of circular reference can be very useful, but you want to make sure you have a method to break all references when you destroy the structure.

Now you are ready to display.
To do the simple display I showed above with the dashes you merely traverse the tree, adding the appropriate dashes as you go. This is also a recursive method of doing this work. The advantage to this sort of scheme is that you can build in the functionality you need into a generic composite pattern and use it for displaying the tree in any manner you like.

Your simple solution is probably good enough for a simple display, but when things start getting complicated you will want to be able to know the size of the tree (how many generations deep and how many persons wide) from the start so you can adjust the display accordingly to make sure you have room for everything. There are lots of different ways people build tree displays of this time with various tricks for positioning things.


Like I have been saying, there are numerous ways to do things like this. Another approach is to use a linked list or a doubly linked list.

Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems





Similar Threads
Thread Thread Starter Forum Replies Last Post
Tree View dotnetDeveloper ASP.NET 3.5 Basics 1 November 26th, 2008 05:05 PM
tree menu anjalisharma Hibernate 4 September 24th, 2007 05:04 AM
Non-Tree DAGs 12th_Man SQL Language 0 June 22nd, 2004 11:42 AM
Tree In PHP ruhin Pro PHP 4 June 19th, 2004 06:14 PM
Tree View Natalie Pro VB 6 5 September 23rd, 2003 07:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.