DFS in a binary tree Problem statement: Perform a depth first traversal on a BST and print the elements traversed.

class Node(object):
    def __init__(self, value, left=None, right=None):
        self.value = value
        self.left = left
        self.right = right

class BST(object):
    def __init__(self, value):
        self.root = Node(value)

    def insert(self, value):
        current = self.root
        while current:
            if value &gt; current.value:
                if current.right is None:
                    current.right  = Node(value)
                    break
                else:
                    current = current.right
            else:
                if current.left is None :
                    current.left  = Node(value)
                    break
                else:
                    current = current.left

    def preorder(self,root):
## root,left,right

        if root:
            print root.value
            if root.left:
                self.preorder(root.left)
            if root.right:
                self.preorder(root.right)


    def postorder(self,root):
## left, right, root

        if root:
            if root.left:
                self.postorder(root.left)
            if root.right:
                self.postorder(root.right)
            print root.value


    def inorder(self,root):
## left, root, right

        if root.left:
            self.inorder(root.left)
        print root.value
        if root.right:
            self.inorder(root.right)



t = BST(100)
t.insert(12)
t.insert(92)
t.insert(112)
t.insert(123)
t.insert(2)
t.insert(11)
t.insert(52)
t.insert(3)
t.insert(66)
t.insert(10)
print "preorder traversal is this "
t.preorder(t.root)
print "postorder traversal is this "
t.postorder(t.root)
print "inorder traversal is this "
t.inorder(t.root)


How can this code be more efficient?
This code is pretty much the most efficient it can be, so I will add some pointers on the coding style and convention.


Instead of calling your Binary Search Tree class a BST, define it as

class BinarySearchTree(object):
    # remainder of your code


Gives more meaning to the class name.
The preorder traversal is called like t.preorder(t.root). But wait, t already has the root node in it, so it would be much more readable if we could do t.preorder(). So modify the signature to:

def preorder(self, root=None):
    if root is None:
        root = self.root
    # remainder of your code



Same should be done for t.inorder() and t.postorder() as well.


Spaces: You need a space between your arguments, as recommended by PEP-0008. So do def inorder(self, root): instead of def inorder(self,root):. This goes for the inorder and postorder as well
Using root as the argument name is misleading - its better to call it as node, which has the default value of self.root. This is cleaner, since in the DFS, you would traverse all the nodes of the tree.

def preorder(self, node=None):
    # replace root with node in your code


Same should be done for inorder and postorder as well.
Comments and indentation: Your comments are currently not indented right. Instead of having

    def inorder(self,root):
## left, root, right


Try defing them in the form of docstrings

def inorder(self,root):
    """traversal order: left child node, current node, right child node"""
    # remainder of your code