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

Binary Trees in Python

Hello!!!

I want know if python have binary trees and more?
Aug 20 '05 #1
8 3114
In article <ma***************************************@python. org>,
[diegueus9] Diego Andrés Sanabria <ze****@gmail.com> wrote:
Hello!!!

I want know if python have binary trees and more?


Python does not come with a tree data structure. The basic data structures
in Python are lists, tuples, and dicts (hash tables).

People who are used to C++'s STL often feel short-changed because there's
not 47 other flavors of container, but it turns out that the three Python
gives you are pretty useful. Many people never find a need to look beyond
them.

If you do need to go beyond them, it's easy enough to build your own.
Here's one example of a binary ordered tree that you might find useful:

http://aspn.activestate.com/ASPN/Coo.../Recipe/286239
Aug 20 '05 #2
[diegueus9] Diego Andrés Sanabria wrote:
Hello!!!

I want know if python have binary trees and more?

Yea, binary trees are more data structures as opposed to libraries:

Here is one approach ( remove the 'java.lang' stuff)

http://www.newspiritcompany.com/BinaryTreePyNew.html

--
Ramza from Atlanta
http://www.newspiritcompany.com
Aug 20 '05 #3

[diegueus9] Diego Andrés Sanabria <zes...@gmail.com> wrote:
Hello!!!

I want know if python have binary trees and more?


You might be interested that ZODB comes with some B-tree
implementations. They can be used alone or you can persist them in the
ZODB quite easily.

http://www.zope.org/Wikis/ZODB/FrontPage

Aug 20 '05 #4
On Sat, 20 Aug 2005 15:19:55 -0400, Roy Smith <ro*@panix.com> wrote:
In article <ma***************************************@python. org>,
[diegueus9] Diego Andrés Sanabria <ze****@gmail.com> wrote:
Hello!!!

I want know if python have binary trees and more?


Python does not come with a tree data structure. The basic data structures
in Python are lists, tuples, and dicts (hash tables).

People who are used to C++'s STL often feel short-changed because there's
not 47 other flavors of container, but it turns out that the three Python
gives you are pretty useful. Many people never find a need to look beyond
them.


Uh, the STL has seven flavors:
- vector
- deque
- list
- set
- map
- multimap
- multiset
so that's not too bad for a static language. Each of them
is vital for some purpose, but vector and map are by far the
most commonly used.

Neither C++ nor Python has tree structures in their standard libraries. I
assume that's because there is no single interface that is proven to suit
everybody's needs.

/Jorgen

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Aug 21 '05 #5
[Jorgen Grahn]
Neither C++ nor Python has tree structures in their standard
libraries. I assume that's because there is no single interface that
is proven to suit everybody's needs.


It is already easy writing "tree constants" using recursive tuples or
lists. To process simple trees in Python, I usually subclass some
Node type from list, and write the traversal methods that suit the
application. The sub-classing already allow for indexing sub-nodes by
"self[index]", and iterating over all by "for subnode in self:", etc.
In my experience, it all goes pretty easily, while staying simple.

However, things related to balancing, finding paths between nodes, or
searching for patterns, etc. may require more work. There are surely
a flurry of tree algorithms out there. What are the actual needs you
have, and would want to see covered by a library?

--
François Pinard http://pinard.progiciels-bpi.ca
Aug 21 '05 #6
Jorgen Grahn wrote:
On Sat, 20 Aug 2005 15:19:55 -0400, Roy Smith <ro*@panix.com> wrote:
In article <ma***************************************@python. org>,
[diegueus9] Diego Andrés Sanabria <ze****@gmail.com> wrote:

Hello!!!

I want know if python have binary trees and more?
Python does not come with a tree data structure. The basic data structures
in Python are lists, tuples, and dicts (hash tables).

People who are used to C++'s STL often feel short-changed because there's
not 47 other flavors of container, but it turns out that the three Python
gives you are pretty useful. Many people never find a need to look beyond
them.

Uh, the STL has seven flavors:
- vector
- deque
- list
- set
- map
- multimap
- multiset


There are others, e.g. std::valarray. There are also adapters that use
the above templates to implement other structures, adding or limiting
functionality as appropriate; e.g., std::heap and std::stack.
so that's not too bad for a static language. Each of them
is vital for some purpose, but vector and map are by far the
most commonly used.

Neither C++ nor Python has tree structures in their standard libraries. I
assume that's because there is no single interface that is proven to suit
everybody's needs.


Hmmm... I guess I never noticed the lack. C++ has structures or
language features that represent most of the common things trees are
typically used to implement. Of course, a "tree" can be represented in
so many ways, it's more of a design pattern than a data structure. :)
Aug 21 '05 #7
>>>>> "[diegueus9]" == [diegueus9] Diego Andrés Sanabria <diegueus9> writes:

[diegueus9]> Hello!!! I want know if python have binary trees and
[diegueus9]> more?

The latest boost, 1.33, says it has a python wrapper for the boost
graph library.
Boost explicitely states that it's experimental, but my emerge was
successful.
See http://boost.org
-Chris
Aug 24 '05 #8
On Sun, 21 Aug 2005 08:52:10 -0400, François Pinard <pi****@iro.umontreal.ca> wrote:
[Jorgen Grahn]
Neither C++ nor Python has tree structures in their standard
libraries. I assume that's because there is no single interface that
is proven to suit everybody's needs.


It is already easy writing "tree constants" using recursive tuples or
lists. To process simple trees in Python, I usually subclass some
Node type from list, and write the traversal methods that suit the
application. The sub-classing already allow for indexing sub-nodes by
"self[index]", and iterating over all by "for subnode in self:", etc.
In my experience, it all goes pretty easily, while staying simple.

However, things related to balancing, finding paths between nodes, or
searching for patterns, etc. may require more work. There are surely
a flurry of tree algorithms out there. What are the actual needs you
have, and would want to see covered by a library?


I have no needs, actually ... but yes, the things you mention (balancing,
traversal ...) were the ones I was thinking about.

/Jorgen

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Sep 4 '05 #9

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

Similar topics

4
by: abhrajit | last post by:
I'm looking for a C/C++/Java library to create a balanced binary tree data structure given a set of leaf nodes as input. A leaf node should never become an interior node. So if I wish to create...
4
by: Rasmus | last post by:
Hi. As partly novice in python I would like a piece of advise of how to implement (binary) trees the best way? Thanks in advance, Rasmus PS: Due to heavy spam reception (20.000+/week), I...
1
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am > cs students and are now facing difficult problems in understanding > what a binary tree is, how it works, and the algorithm...
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,...
8
by: sudharsan | last post by:
please gimme the logic to merge two binary search trees?I mean which node has to be the root node of the new binary tree?? Thanks in advance
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
2
by: pyguy | last post by:
Hi all, I am running into a conceptual glitch in implementing a simple binary tree class. My insertion and printing (sorting) seems to be ok, but when I search the tree, my find method isn't doing...
5
by: maxim.novak | last post by:
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...
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...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.