473,396 Members | 2,154 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,396 software developers and data experts.

begging for a tree implementation

I'm looking for a simple tree implementation: 0-n children, 1 root.
All the nice methods would be appreciated (getLeaves, isLeaf, isRoot,
depthfirst, breadthfirst,...) That's really all I need. I could code
one up, but it would take time to debug, and i'm really short on time
right now.

Thanks!
Micah

Apr 26 '06 #1
11 5351
Micah enlightened us with:
I'm looking for a simple tree implementation: 0-n children, 1 root.
All the nice methods would be appreciated (getLeaves, isLeaf,
isRoot, depthfirst, breadthfirst,...) That's really all I need. I
could code one up, but it would take time to debug, and i'm really
short on time right now.


What kind of tree do you want? B+-tree? Black/Red tree? Binary search
tree?

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Apr 26 '06 #2

Sybren Stuvel wrote:
Micah enlightened us with:
I'm looking for a simple tree implementation: 0-n children, 1 root.
All the nice methods would be appreciated (getLeaves, isLeaf,
isRoot, depthfirst, breadthfirst,...) That's really all I need. I
could code one up, but it would take time to debug, and i'm really
short on time right now.


What kind of tree do you want? B+-tree? Black/Red tree? Binary search
tree?

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa


I'm looking for a simple abstract-data-type tree. I would have thought
there would be a built-in type, but I can't find one. I just need to
be able to start from a root node and attach children from there. I
could jury-rig one using a dict or some tuples, but I'd like a
full-featured tree if someone has one implemented.

Thanks for the reply!
Micah

Apr 27 '06 #3
Micah enlightened us with:
I'm looking for a simple abstract-data-type tree. I would have thought
there would be a built-in type, but I can't find one. I just need to
be able to start from a root node and attach children from there. I
could jury-rig one using a dict or some tuples, but I'd like a
full-featured tree if someone has one implemented.


If you keep things that vague: use a list. See the first element as
the root node. Every node has only one child.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Apr 27 '06 #4
Micah:
I'd like a full-featured tree


What features?

--
René Pijlman
Apr 27 '06 #5
Ant
Did you not read the first post?
All the nice methods would be appreciated (getLeaves, isLeaf, isRoot,
depthfirst, breadthfirst,...)


Apr 27 '06 #6
Ant
I was looking for a tree implementation a while back, but got no real
pointers. Seems that there are no tree data model modules out there -
which surprises me, since it is such a fundamental data structure.

I ended up using a very basic tree data class - I didn't need all of
the methods you mentioned. Here it is in case that helps at all:

class Node (object):
def __init__(self, data, parent=None):
self.data = data
self.parent = parent
self.children = []

def add_child(self, child):
self.children.append(child)
child.parent = self

Some of those methods you mentioned would be trivial to implement:

def is_root(self):
return not self.parent
def is_leaf(self):
return not self.children

It may also be more pythonic to have 'walk' methods for traversing
trees.

I may start a tree data structure project - it's something I've been
thinking about for a while, and now it's clear that it's not just me
who uses trees as data structures!

Apr 27 '06 #7
> I may start a tree data structure project - it's something I've been
thinking about for a while, and now it's clear that it's not just me
who uses trees as data structures!


Oh, people do use them. It's just to easy to cough one up when you need it -
either as nested tuples, lists, dicts or a simple class like yours - which
I needed yesterday and wrote in 2 minutes. And that is tailored to the
needs at hand and not some abstraction that might prevent you from e.g.
using methods like __iter__ or the like with your own semantics.

This is not to discourage you - just don't expect people to greet you as the
next messiah who finally brought one of CS most fundamental data structures
to Python... :)

Diez
Apr 27 '06 #8
Diez B. Roggisch wrote:
This is not to discourage you - just don't expect people to greet you as the
next messiah who finally brought one of CS most fundamental data structures
to Python... :)


xml.etree was added to Python 2.5 before christmas :-)

</F>

Apr 27 '06 #9
Ant
Damn! Missed the boat ;-)

Apr 27 '06 #10
Fredrik Lundh wrote:
Diez B. Roggisch wrote:
This is not to discourage you - just don't expect people to greet you as
the next messiah who finally brought one of CS most fundamental data
structures to Python... :)


xml.etree was added to Python 2.5 before christmas :-)


Can't wait until etree grows some tree optimization algorithms like AVL or
red-black. Those degenerated XML-documents of mine always annoyed me.. :)

Diez
Apr 27 '06 #11
Micah wrote:
I'm looking for a simple tree implementation: 0-n children, 1 root.
All the nice methods would be appreciated (getLeaves, isLeaf, isRoot,
depthfirst, breadthfirst,...) That's really all I need. I could code
one up, but it would take time to debug, and i'm really short on time
right now.

Thanks!
Micah


http://aspn.activestate.com/ASPN/Coo.../Recipe/136529

Apr 27 '06 #12

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

Similar topics

3
by: Steve Johnson | last post by:
Been banging my head on this for two days now. Hope someone can help! My test program below is in the form of a single JSP, with a Node class build in. (All the coded needed to run is below.) ...
12
by: pillepop2003 | last post by:
Hey! Can anyone give me a hint, how this problem is best implemented: I have a table of users (see below), where every user has one "superior user" (= parent node), this should be a fully...
5
by: nandor.sieben | last post by:
I'm working on a project analyzing a game and I am in need of a tree template library. I found a few implementations on the web but all of them were too complicated for me to understand. I wrote...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
5
by: Mike | last post by:
Why does this code insert a node into a binary search tree correctly? If I only inserting going by first digit it works properly but when I try inserting going by the whole ip and the port number...
25
by: prabhat143 | last post by:
Hi, Given a singly linked, null terminated list, how can it be converted to tree? Each node in the list has three attributes: it's ID, it's parent ID and of course, the next node it's pointing...
3
by: asianmuscle | last post by:
Hi I am relearning datastructure... but got kinda stuck in a basic delete node operation. what it does is to delete the first node it finds when the item is equal the input item. the end result is...
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
6
by: Bart Kastermans | last post by:
I am playing with some trees. In one of the procedures I wrote for this I am trying to change self to a different tree. A tree here has four members (val/type/left/right). I found that self = SS...
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
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
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.