Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Multilevel must use recursive?
Message
De
04/08/2001 10:35:39
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
03/08/2001 22:27:19
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00539221
Message ID:
00539771
Vues:
17
>Thank you for reply. Do you know any site have this sample code? COuld you pls show me the simple sample code for these? (if u free) Or, the procedure to do so.
>
>Thank you

John,
I don't know about sites that might have samples but probably ones that deal most with Artificial Intelligence (recursion and list processing is used often).

Below code uses a nonrecursive approach, nonSQL and non object too. While you might have objections for not being OOPed or nonSQL here I would say it's a matter of performance. Despite Rushmore exists it's sometimes dog-slow compared to traditional xbase approach (xbase approach lead to longer but faster code as is here). Object creation even with the lightweight Relation object is slower than xbase approach too. If you try this with a recursive method and set childcount to 1 (to allow MAXDEPTH go) you'd get infamous nesting level exceeded error. Here it's only limited for 1-5 childs for each parent could mean more or less 5^200 records < bg >

As you can see Inserting nodes and getting branch for a node are both nonrecursive.
Clear all
Rand(-1)
#Define MAXDEPTH 200
Create cursor Multilevel (parentid c(10),nodeid c(10), nodedepth i)
Local ix
For ix=1 to 5
  Insert into Multilevel ;
    (parentid,nodeid,nodedepth) ;
    values ;
    ('',sys(2015),1)
Endfor
For lnDepth=1 to MAXDEPTH
  InsertNodes(lnDepth)
  If reccount()>200000 && Don't expect an exit at around 200000
    Exit
  Endif
Endfor
Select Multilevel
Index on parentid Tag parentid
Index on nodeid Tag nodeid
Locate
Brow title 'Press escape on a record - Branch for NodeId will be returned next'
lcNodeId = Multilevel.nodeid
Start=seconds()
GetBranch(lcNodeId)
?seconds()-start
locate
Brow
* You could cut down time if instead of SQL crsNodes had all info
*Select * from Multilevel where nodeid in (select nodeid from crsNodes)
Return

Function InsertNodes
Lparameters tnDepth
Local ix
Select nodeid from Multilevel ;
  where nodedepth = tnDepth ;
  into cursor crsTemp
Scan
  For ix=1 to int(rand()*5)+1 && Insert few childs for parentnode
    Insert into Multilevel ;
      (parentid,nodeid,nodedepth) ;
      values ;
      (crsTemp.nodeid,sys(2015),tnDepth+1)
  Endfor
Endscan

Function GetBranch
Lparameters tcNodeId
Select Multilevel
=seek(tcNodeId,'Multilevel','NodeId') && For first rec. get itself
scatter memvar
Set order to tag parentid
Local lnRec, lcNodeId
afields(arrStruc)
Create cursor crsNodes from array arrStruc
Insert into crsNodes from memvar
Scan
  lnRec=recno('crsNodes')
  lcNodeId = crsNodes.nodeid
  Select Multilevel
  Seek crsNodes.nodeid
  Scan while parentid == lcNodeId
	scatter memvar
	Insert into crsNodes from memvar
  Endscan
  Select crsNodes
  Go lnRec
Endscan
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform