473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

trees, iterations and adding leaves

Hello

I have trees like this:
>>from nltk_lite.parse.tree import Tree
tree6 = Tree('main', ['sub1', 'sub2'])
tree6
('main': 'sub1' 'sub2')

I use nltk package - but it should not matter here.
I could change it's lafes (add node) like this:
>>tree6[0] = Tree('newsub',[])
tree6
('main': ('newsub': ) 'sub2')

But my tree has many levels and it's imposibble to address it like:
tree6[0][1][0][1][1][1][0]..........

So i wanted to 'travel thru my tree' to last node which should be changed:
>>tree6 = Tree('main', ['sub1', 'sub2'])
subtree = tree6[0]
subtree
'sub1'
>>subtree = Tree('newsub',[])
subtree
('newsub': )
>>tree6
('main': 'sub1' 'sub2')

The problem is that subtree is some kind of a new variable (not pointer)
so changing it i will not alter tree6. How to alter tree6 while
'travelling along it's nodes',
without messy referencing as tree6[0][1][0][1][1][1][0].......... ?

Thanx
Dec 31 '06 #1
1 1155
At Sunday 31/12/2006 14:25, vertigo wrote:
>I use nltk package - but it should not matter here.
Yes, it does. The framework should provide some form of tree traversal.
>So i wanted to 'travel thru my tree' to last node which should be changed:
>tree6 = Tree('main', ['sub1', 'sub2'])
subtree = tree6[0]
subtree
'sub1'
>subtree = Tree('newsub',[])
subtree
('newsub': )
>tree6
('main': 'sub1' 'sub2')
The problem is that subtree is some kind of a new variable (not pointer)
so changing it i will not alter tree6.
This, yes, is a general Python question. When you bind something to
the name "subtree", it doesn't matter what were "subtree" pointing to
before. Read http://effbot.org/zone/python-objects.htm
>How to alter tree6 while
'travelling along it's nodes',
without messy referencing as tree6[0][1][0][1][1][1][0].......... ?
Without any further knowledge of the Tree objects, you could do
something like this:

def traverse_tree(tree, *ids):
result = tree
while ids:
key = ids.pop(0)
tree = tree[key]
return tree

and say: traverse_tree(tree6, 0, 1, 0, 1, 1, 1, 0) or
traverse_tree(tree6, *[0,1,0,1,1,1,0]) or traverse_tree(tree6,
*[0,1,0,1,1])[0] = another_object
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 2 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: jova | last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so can someone please explain. Thank You.
8
by: [diegueus9] Diego Andrés Sanabria | last post by:
Hello!!! I want know if python have binary trees and more?
1
by: barnesc | last post by:
Hi again, Since my linear algebra library appears not to serve any practical need (I found cgkit, and that works better for me), I've gotten bored and went back to one of my other projects:...
3
by: ptrSriram | last post by:
Can someone help me with an algorithm to merge two binary search trees. One method I thought of was to flatten both the trees into sorted lists(inorder traversal),merge those two sorted lists,...
3
by: vertigo | last post by:
Hello What library/functions/classes could i use to create trees ? Thanx
2
by: parasuram | last post by:
Hi friends ............. this is a question regarding the data structures trees Pleas post it if possible with in 2 days I will thankful if some body could help doing this. Operating...
9
by: Wojtek | last post by:
Hi, I want to use tree-like structures in my program. All of them are provided by user in config files. Tree's are looking like this: AND | +--OR | +--something | +--something
7
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A...
4
Uncle Dickie
by: Uncle Dickie | last post by:
Sorry for the slightly obscure Title. I couldn't really think of a way to describe my scenario accurately, but here goes: The following bit of code retrieves the latest bill of materials for a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.