473,378 Members | 1,409 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,378 software developers and data experts.

Storing and searching nodes of a tree

Hi, I have a tree data structure and I name each node with the
following convention:

a
|---aa
| |--- aaa
| |--- aab
|
|---ab
|
|---ac
I use these names as keys in a dictionary, and store node's data.
Now given a name like "abc", I want to find the key with the following
rule:
If the key exists return the value
If the key does not exist, return the value of the leaf node whose
name is in the given name. For, "abc", it is "ab" . For, "ad", it is
"a".

I suppose there must be a simpler solution to this problem. I
implemented it like this:
d = {'a':0, 'aa':12, 'ab':43, 'aaa':22, 'aab':343, 'ac':33}
name = 'abc'
key = max( [ x for x in d.iterkeys() if x in name] )
value = d[key]

I can change the data structure from dictinory to tuple of key,value
pairs or any thing, and afford to keep them in a particular order. Is
there any way I can speed up this as I have to do this for around 4000
times with tree size being ~5000.

-
Suresh

May 15 '07 #1
4 1876
In <11**********************@h2g2000hsg.googlegroups. com>,
jm*******@no.spam.gmail.com wrote:
I use these names as keys in a dictionary, and store node's data.
Now given a name like "abc", I want to find the key with the following
rule:
If the key exists return the value
If the key does not exist, return the value of the leaf node whose
name is in the given name. For, "abc", it is "ab" . For, "ad", it is
"a".

I suppose there must be a simpler solution to this problem. I
implemented it like this:
d = {'a':0, 'aa':12, 'ab':43, 'aaa':22, 'aab':343, 'ac':33}
name = 'abc'
key = max( [ x for x in d.iterkeys() if x in name] )
value = d[key]

I can change the data structure from dictinory to tuple of key,value
pairs or any thing, and afford to keep them in a particular order. Is
there any way I can speed up this as I have to do this for around 4000
times with tree size being ~5000.
What about this:

def search(tree, path):
while path:
result = tree.get(path)
if result is not None:
return result
path = path[:-1]
raise KeyError('path not on tree')

Ciao,
Marc 'BlackJack' Rintsch
May 15 '07 #2
On May 15, 6:33 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
In <1179233407.473173.259...@h2g2000hsg.googlegroups. com>,

jm.sur...@no.spam.gmail.com wrote:
I use these names as keys in a dictionary, and store node's data.
Now given a name like "abc", I want to find the key with the following
rule:
If the key exists return the value
If the key does not exist, return the value of the leaf node whose
name is in the given name. For, "abc", it is "ab" . For, "ad", it is
"a".
I suppose there must be a simpler solution to this problem. I
implemented it like this:
d = {'a':0, 'aa':12, 'ab':43, 'aaa':22, 'aab':343, 'ac':33}
name = 'abc'
key = max( [ x for x in d.iterkeys() if x in name] )
value = d[key]
I can change the data structure from dictinory to tuple of key,value
pairs or any thing, and afford to keep them in a particular order. Is
there any way I can speed up this as I have to do this for around 4000
times with tree size being ~5000.

What about this:

def search(tree, path):
while path:
result = tree.get(path)
if result is not None:
return result
path = path[:-1]
raise KeyError('path not on tree')

Ciao,
Marc 'BlackJack' Rintsch
If I have the tree in the dictionary, the code would like this,
def search(tree, path):
while path and not(tree.haskey(path)):
path = path[:-1]
if path:
return tree[path]
else:
raise KeyError('path not on tree')

Another qn -- dict.haskey() takes logn time, am I right?

-
Suresh

May 15 '07 #3
In <11********************@e51g2000hsg.googlegroups.c om>,
jm*******@no.spam.gmail.com wrote:
If I have the tree in the dictionary, the code would like this,
def search(tree, path):
while path and not(tree.haskey(path)):
path = path[:-1]
if path:
return tree[path]
else:
raise KeyError('path not on tree')

Another qn -- dict.haskey() takes logn time, am I right?
No it's O(1). Dictionaries are implemented as hash tables. You may write
the condition as:

while path and path not in tree:

That's a little easier to read and a bit faster too as a method lookup is
spared.

Ciao,
Marc 'BlackJack' Rintsch
May 15 '07 #4
On May 15, 9:25 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
In <1179243655.596897.6...@e51g2000hsg.googlegroups.c om>,

jm.sur...@no.spam.gmail.com wrote:
If I have the tree in the dictionary, the code would like this,
def search(tree, path):
while path and not(tree.haskey(path)):
path = path[:-1]
if path:
return tree[path]
else:
raise KeyError('path not on tree')
Another qn -- dict.haskey() takes logn time, am I right?

No it's O(1). Dictionaries are implemented as hash tables. You may write
the condition as:

while path and path not in tree:

That's a little easier to read and a bit faster too as a method lookup is
spared.

Ciao,
Marc 'BlackJack' Rintsch
Wow :) , thanks.
-
Suresh

May 15 '07 #5

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

Similar topics

14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
4
by: pmcguire | last post by:
I have a treeview with a lot of nodes. I want to load only the nodes that are initially visible when the form loads, and then continue to populate it in background and/or when the nodes are required...
12
by: Alex D. | last post by:
is it possible? pros and cons?
2
by: Brian Tkatch | last post by:
I have a form that has six possible setups for a TreeView, though after the Tree is shown, they all act the same way. The Nodes have three or four levels. Repopulating the tree for another setup...
0
LacrosseB0ss
by: LacrosseB0ss | last post by:
Hey all! I have just started using the TreeView object in asp. There are some other applications I have seen use it and I have copied some of the code from them at work. What happens is on a form,...
15
by: Gigs_ | last post by:
Hi all! I have text file (english-croatian dictionary) with words in it in alphabetical order. This file contains 179999 words in this format: english word: croatian word I want to make...
10
by: John Rogers | last post by:
This code only counts the parent nodes or rootnodes in a treeview, how do you count all the nodes in a treeview? // one way int NodeCounter = 0; foreach (TreeNode currentNode in...
6
Sl1ver
by: Sl1ver | last post by:
I've got a problem, i got the nodes to move but 1. they copy nodes(if you drag it to 3 different places it will actually have 3 of the same nodes) 2. i want to make the nodes, if moved update the...
0
by: kajuchirag | last post by:
How do i store the date and tree root node and child nodes in database. How many tables to be created. It should be fast in counting the nodes even if there are more than 25000 child nodes in left...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.