Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tree data structure?

Status
Not open for further replies.

jges

Technical User
Oct 24, 2003
537
US
I have a hierarchy of files (files from a CAD program that represent the structure of a physical assembly) that I need to process. For example:
Code:
C:\path\A.prt
  C:\path\B.prt
    C:\path\C.prt
    C:\path\D.prt
  C:\path\E.prt
  C:\path\F.prt
I need to process files C and D before I can process B, then I can process B, E, and F; and finally file A. This is a small example, in actual use it can range from no children to hundreds of children many levels deep.

I tried using an array, but had a difficult time keeping track of parents and if I had hit the last level or not.

Seems like a good time to use a tree structure, but I don't have any experience with them. Any advice on how to set one up and use it? Or an alternative suggestion?
 
I tried using an array
What about a Scripting.Dictionary object ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
[0] To adequately treat tree-structure like this, you can use a quick , but, I don't think dirty, xml approach. It always offers an advantage of separating structure from processing logic.

[1] First make out a text (xml) file (c:\path\xyz.xml, say) adequately reflect the tree of parts and their files to process. My sketch herein suppose the root being a wrapping element without operational implication.
[tt]
<root>
<A file="c:\path\a.prt">
<E file="c:\path\e.prt" />
<F file="c:\path\f.prt" />
<B file="c:\path\b.prt">
<C file="c:\path\c.prt" />
<D file="c:\path\d.prt" />
</B>
</A>
</root>
[/tt]
[2] The processing logic would be this: process the file located in the file attribute from the deepest level to the level 1 (after the root, here element A, say). The script will show you how those files be obtained in the proper order. You'll find part-B will only be processed with part-C and part-D processed, automatically.
[tt]
[green]xfile="c:\path\xyz.xml"[/green]

set oparser=createobject("msxml2.domdocument.6.0")
with oparser
.async=false
.validateonparse=false
.resolveexternals=true
end with

bret=oparser.load(xfile)
if not bret then
wscript.echo oparser.parseerror.errorcode & vbcrlf & oparser.parseerror.reason
set oparser=nothing
wscript.quite 9
end if

with oparser
.setProperty "SelectionLanguage", "XPath"

set cnodes=.selectNodes("//*[count(descendant::*)=0]")

maxlevel=0
for each onode in cnodes
n=onode.selectNodes("ancestor::*").length
if n>maxlevel then maxlevel=n
next
set cnodes=nothing

for i=maxlevel to 1 step -1 'root node is not operational, it is just a pure wrapper
set cnodes=.selectNodes("//*[count(ancestor::*)=" & i & "]")
for each onode in cnodes
wscript.echo "processing level: " & i & vbcrlf & onode.tagname & vbcrlf & onode.getAttribute("file")
'you actual file processing script here, and you've got all the data needed.
next
set cnodes=nothing
next
end with
set oparser=nothing
[/tt]
 
Thanks for the quick replies.

The xml route looks promising, I'm going to experiment with that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top