473,539 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to construct a binary-tree using python?

Hi everyone, I'm wondering whether it's possible to construct a
binary-tree using python.
Since python don't have pointer, it can't dynamically allocate memory
like C language.
But some important data structures like linked list, binary-tree and
hash table are closely linked with dynamic memory technology.

Any help that can be provided would be greatly appreciated.

Thanks in advance

May 6 '06 #1
4 24123
hankssong wrote:
Hi everyone, I'm wondering whether it's possible to construct a
binary-tree using python.
Since python don't have pointer, it can't dynamically allocate memory
like C language.
But some important data structures like linked list, binary-tree and
hash table are closely linked with dynamic memory technology.

Its actually very possible to construct a binary tree in python. If you
do some googles searches you'll, in fact find, some implementations.

Its important to remember that python can dynamically make objects on
the fly. Objects are created as references and so you can make tree
nodes whenever you want to

Here are some snippets, I'm not the most advanced coder but its just to
show you that it can be done.

I wouldn't use this code verbatim, its taylored to a project I was doing
at the time, but I hope this helps you get an idea.
class TreeNode:
def __init__(self, nodeData):
self.left = None
self.right = None
[snip - stuff for my project]
class Tree:

def __init__(self):
self.root = None

#assigns the data to a new TreeNode
def addNode(self, inputData):
return TreeNode(inputData) #insert is recursive, so when it reaches
its ending point this is called

#this function traverses the tree and finds the spot to add the node
def insertNode(self, inputData, root):
def insertNode(self, inputData, root):
if inputData.buildTag <= root.bTag:
if root.left == None:
root.left = self.addNode(inputData) #left is empty? add
else:
self.insertNode(inputData, root.left) #visit the left subtree
else:
if root.right == None:
root.right = self.addNode(inputData)
else:
self.insertNode(inputData, root.right)
return #root

def findNode(self, nodeToFind, root): #nodeToFind is just a buildTag
string
if root == None:
return None
else:
#btag is something i needed for what I was doing, ignore it
#print "Comparing " + nodeToFind + " and " + root.bTag
if nodeToFind == root.bTag:
return root
elif nodeToFind < root.bTag:
return( self.findNode(nodeToFind, root.left) )
else:
return( self.findNode(nodeToFind, root.right) )


--

Carl J. Van Arsdall
cv*********@mvista.com
Build and Release
MontaVista Software

May 6 '06 #2
Depending on what concrete use you have for binary trees, you may want
to consider tuples. What's cool about them is that you get pattern
matching on your tree for free.
x = ((2,4),(5,6))
y, _ = x
y (2, 4) (_,y), _ = x
y 4


Or you could code your own binary tree class subclassing tuple.
.... just a thought.
v.

May 6 '06 #3
I use google and get some detailed info in the page:
http://aspn.activestate.com/ASPN/Coo.../Recipe/286239
Carl J. Van Arsdall ,vdrab, thank for your help!

May 6 '06 #4
Op 2006-05-06, hankssong schreef <so**********@gmail.com>:
Hi everyone, I'm wondering whether it's possible to construct a
binary-tree using python.
Since python don't have pointer, it can't dynamically allocate memory
like C language.
But some important data structures like linked list, binary-tree and
hash table are closely linked with dynamic memory technology.

Any help that can be provided would be greatly appreciated.


You may have a look here:

http://www.pardon-sleeuwaegen.be/antoon/avltree.html

--
Antoon Pardon
May 10 '06 #5

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

Similar topics

1
1381
by: Steven Arnold | last post by:
Is there a more elegant way to construct \ in a string than something like: s = '\\n' result = eval( "'%s'" ) % s Another ugly method would be to build a dict with all the different special letters I want as keys, and their corresponding values as values. Or I could have a huge if/elif structure. I can't make ord work, because while...
9
9698
by: Maurizio Penna | last post by:
I, guys. I've embeded an image into a xml file, something like that: <display type="picture" mime="image/png" name = "mosaico6.png"> <!]> </display> Now, I want to display it with a XSL Transformation, but my code don't work: <display>
11
3119
by: Charles T. | last post by:
Hi, I currently writing a serialize/unserialize architecture. The read/write function will read/write from a binary file. My question is is there some sort on defined standart to use when representing data type (int , int32, int64, double, string, etc....) ? Thanks,
18
22575
by: Bern | last post by:
how to specifiy a binary number in c++? hex numbers are specified by 0x prefix
19
4111
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
5
2323
by: Luke Vogel | last post by:
Hi all, Probably a really basic question, but I cant find an answer ... I have an xml file of books something like: <product> <isbn>0-735-61374-5</isbn> <title>Microsoft Visual Basic Step By Step</title> <author>Michael Halvorsen</author> <subject>Programming</subject>
17
3467
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using , operator there one operand will be there for ()? And Unary operators like !, +, -, ~, ... are said to be having associativity right to left which means...
5
2607
by: Peter Olcott | last post by:
I created an object that requires access to another objects data, yet have found no good way to pass this data as a parameter because the member function that requires this data must be a binary predicate for std::sort. The only way that I got it to work so far is to make the other object global. Are there any better ways than this? ...
2
6983
by: Steven T. Hatton | last post by:
What is the best way to read data from a file into a fixed size array of unsigned char? This is a file holding only a SHA1 digest. What I would really like to do is initialize the ifstream with the buffer allocated to hold the data, seekg(ios_base::end) and magically have the data appear in the buffer. I believe something close to this can...
2
44664
by: sypi | last post by:
Hi, I'd need to construnct an EXEC statement in my script I am pretty much stuck with data conversion. I need to compare a binary variable with a SID value to an actual SID found in ..sysusers. Whe I construct the EXEC statement the whol line has to be string and I need to find a way to use the binary data as string. So far, I tryed to...
0
7368
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7537
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7704
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7295
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5228
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3359
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
934
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.