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!

Longest "Branch" in tree...and then some!?!?

Status
Not open for further replies.

CJason

Programmer
Joined
Oct 13, 2004
Messages
223
Location
US
This is actually a 2-part question:

1) I need to create some sort of "tree" structure. I'm not sure the best way to do that in perl (I'd use linked lists in other languages, but I'm not sure how to use perl linked lists...if perl even has that capability). The tree is pretty simple...just a "name" and "value" at each node.

2) Once I get a tree structure, I need to determine the deepest branches...from deepest to shallowest...based on the value on each node. So, for example, I want to find the deepest to shallowest branches where there are consecutive values of "1" on the nodes. (I hope that makes sense).

Here is an example:
(a,1)->(b,1)->(c,2)->(d,1)
(a,1)->(e,1)->(f,1)

I would want the following information, when looking for consecutive 1's:
max_depth = 3 => a->e->f
depth = 2 => a->b
min_depth = 1 => d

Any ideas where to even begin on this one?!?!?!?!

Thanks in advance!!!
 
could be there is already a module as there are a number of "tree" modules on cpan, but it also looks fairly straight forward to parse each line and count the consecutive instances of x,n where x is an alpha and n is a digit.

How much perl do you know?

none
some of the basics


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I know Perl well...but, haven't really had the need for "linked list" type of processing in perl. I've been looking online for some ideas, and I "think" I have a direction. I'm basically attempting to do a hash of a hash to do what I want...but, I was hoping perl has some sort of structure where you could do something like

a->next = b
b->next = c
b->prev = a
etc...

but, I don't think that's possible.....or is it?!?!?!?
 
>>I was hoping perl has some sort of structure where you could do something like

I don't know. But I'm se you can do this with perl pretty easily. I'll check back tonight when I'm off work and see if you stll need help.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I did like a psuedo-linked list by using a hash of a hash. When I get back to work tomorrow, I'll test it some more and see if it's working correctly. If you know of an easier way than "hash of hash", I would love to know how!
 
If you don't get it figured out, post a good sample of what the real data looks like.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top