By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

Binary search tree

P: n/a
Hi,

I have to get list of URLs one by one and to find the URLs that I have
more than one time(can't be more than twice).

I thought to put them into binary search tree, this way they'll be
sorted and I'll be able to check if the URL already exist.

Couldn't find any python library that implements trees.
Is there some library of this kind in python? Or can I find it
somewhere else?

Nov 9 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ma*********@gmail.com wrote:
Hi,

I have to get list of URLs one by one and to find the URLs that I have
more than one time(can't be more than twice).

I thought to put them into binary search tree, this way they'll be
sorted and I'll be able to check if the URL already exist.

Couldn't find any python library that implements trees.
Is there some library of this kind in python? Or can I find it
somewhere else?
Put them into a set. You can check for existence (very fast) and at the end it
is easy to sort.

-Larry
Nov 9 '07 #2

P: n/a
On 2007-11-09, Larry Bates <la*********@websafe.comwrote:
ma*********@gmail.com wrote:
>I have to get list of URLs one by one and to find the URLs
that I have more than one time(can't be more than twice).

I thought to put them into binary search tree, this way
they'll be sorted and I'll be able to check if the URL already
exist.

Couldn't find any python library that implements trees. Is
there some library of this kind in python? Or can I find it
somewhere else?

Put them into a set. You can check for existence (very fast)
and at the end it is easy to sort.
The set is likely the way to go.

Python's library support for binary search trees consists of the
bisect module.

--
Neil Cerutti
Ask about our plans for owning your home --sign at mortgage company
Nov 9 '07 #3

P: n/a
On Nov 9, 4:06 pm, maxim.no...@gmail.com wrote:
Hi,

I have to get list of URLs one by one and to find the URLs that I have
more than one time(can't be more than twice).

I thought to put them into binary search tree, this way they'll be
sorted and I'll be able to check if the URL already exist.

Couldn't find any python library that implements trees.
Is there some library of this kind in python? Or can I find it
somewhere else?
Can you use set() or set.difference()?

Nov 9 '07 #4

P: n/a
ma*********@gmail.com a écrit :
Hi,

I have to get list of URLs one by one and to find the URLs that I have
more than one time(can't be more than twice).

I thought to put them into binary search tree, this way they'll be
sorted and I'll be able to check if the URL already exist.
What about a set ?

s = set()
for url in urls:
if url in s:
print "already have ", url
else:
set.add(url)

Nov 9 '07 #5

P: n/a
On Nov 9, 11:45 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
maxim.no...@gmail.com a écrit :
Hi,
I have to get list of URLs one by one and to find the URLs that I have
more than one time(can't be more than twice).
I thought to put them into binary search tree, this way they'll be
sorted and I'll be able to check if the URL already exist.

What about a set ?

s = set()
for url in urls:
if url in s:
print "already have ", url
else:
set.add(url)
Interesting. For this case I usually used dicts. As in:

d = {}
for url in urls:
if url in d:
print "already have ", url
else:
d[url] = 1

Now, I can see that this method has some superfluous data (the `1`
that is assigned to the dict). So I suppose this is less memory
efficient. But is this slower then? As both implementations use hashes
of the URL to access the data. Just asking out of curiosity ;)

Nov 12 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.