School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,394 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,595 people online right now. Registration is fast and FREE... Join Now!




Data Structures Under Python

 

Data Structures Under Python

toshiro

1 Jul, 2009 - 02:29 PM
Post #1

New D.I.C Head
Group Icon

Joined: 27 Jun, 2009
Posts: 11



Thanked: 1 times
Dream Kudos: 25
My Contributions
Is this the most efficient implementation? And no this isn't homework - I'm just messing around with Python implementation of my Java data structures I built last semester

CODE

class BinarySearchTree():
    "Implements a Python varient of a Binary Search Tree"
    def __init__(self):
        self.root = None

    def __len__(self):
        "Overrides len() function to count al of the nodes in the BST"
        return self.length(self.root)

    def length(self, node):
        "Recursive helper method to count nodes"
        if node == None:
            return 0
        else:
            return self.length(node.getLeft()) + self.length(node.getRight()) + 1
        
    def add(self, datatype):
        "Adds a TreeNode to its correct place in the tree, with smaller items to the right and greater ones to the left"
        self.root = self.__add__(self.root, datatype)
        
    def __add__(self, node, dat):
        if node == None:
            return TreeNode(dat)
        else:
            if dat < node.getData():
                node.left = self.__add__(node.left, dat)
            elif dat > node.getData():
                node.right = self.__add__(node.right, dat)
        return node

    def inorder(self):
        "returns a list of each node's data in ascending order"
        self.items = []
        self.__inorder__(self.root)
        return self.items
        
    def __inorder__(self, n):
        if(n == None):
            return ''
        else:
            self.__inorder__(n.getLeft())
            self.items.append(n.getData())
            self.__inorder__(n.getRight())

    def preorder(self):
        "returns a list of each node's data in preorder"
        self.items = []
        self.__preorder__(self.root)
        return self.items
        
    def __preorder__(self, n):
        if(n == None):
            return ''
        else:
            self.items.append(n.getData())
            self.__preorder__(n.getLeft())
            self.__preorder__(n.getRight())

    def postorder(self):
        "returns a list of each node's data in postorder"
        self.items = []
        self.__postorder__(self.root)
        return self.items
        
    def __postorder__(self, n):
        if(n == None):
            return ''
        else:
            self.__postorder__(n.getLeft())
            self.__postorder__(n.getRight())
            self.items.append(n.getData())
                
class TreeNode():
    """Node that contains  pointers to left and right nodes"""
    data = ""
    left = None
    right = None

    def __init__(self, string = ''):
        self.data = string
        self.left = None
        self.right = None
        
    def __str__(self):
        return (self.data)
        
    def getLeft(self):
        if self.left != None:
            return self.left

    def getRight(self):
        if self.right != None:
            return self.right

    def getData(self):
        return self.data


User is offlineProfile CardPM
+Quote Post


Nallo

RE: Data Structures Under Python

20 Jul, 2009 - 07:11 AM
Post #2

New D.I.C Head
*

Joined: 19 Jul, 2009
Posts: 24



Thanked: 3 times
My Contributions
I have a few comments on your code, though those might not increase performance by much:

1.
CODE

    def getLeft(self):
        if self.left != None:
            return self.left

is actually the same as
CODE

    def getLeft(self):
        return self.left

When the interpreter doesnt run into a return statement in a function, it will return None. So your if statement doesnt do anything.

2.
You dont want to use getters and setters in Python that only get or set an attribute. Python is not Java(where getters and setters are (almost always) mandatory) wink2.gif . Ryan Tomayko has written a good article on this topic.
If this article isnt available anymore look up the "property" statement and google for "python getters setters" and "python getters are evil".

Because of 1 and 2 you dont need the getters of your TreeNode class.

3.
CODE

def __add__(self, node, dat):
  ...

Did you really want to overload the "+" Operator or did you want to write a private method?
For a private method the naming convention is to start with 2 underscores, but no underscores at the end.
Methods starting and ending with 2 underscores are meant for creators, destructors, operator overloading ...
So maybe you meant:
CODE

def __add(self, node, dat):
  ...


This post has been edited by Nallo: 20 Jul, 2009 - 07:14 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 09:57PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month