Binary tree in Python exercise I'm practicing Python. Being completely new to the language I'd like to hear someone else thoughts on the way I use the language. I'm coming from (mainly) Java and C so I'd expect new ways of doing stuff offered by Python which I am surely not aware of because I'm still learning.

I came up with a very simple implementation for a binary tree. The idea is to practice with classes in Python. The tree supports just inserting elements and traversing pre-order, in-order and post-order. I'm not concerned about the functionality but more about comments.

from __future__ import print_function
import math
import os

class BinaryTree:
    __left = None
    __right = None
    __value = None

    def __init__(self):
        return

    def addValue(self, value):
        if self.__value is None:
            self.__value = value

        elif value &gt; self.__value:
            if self.__right is None:
                self.__right = BinaryTree()
            self.__right.addValue(value)

        elif value &lt; self.__value:
            if self.__left is None:
                self.__left = BinaryTree()
            self.__left.addValue(value)

        return

    def preOrderTraverse(self):
        """
        Performs a pre-order traversal of the tree.
        Returns the list of elements
        """
        elements = []
        if self.__value is not None:
            # Visit the root node first
            elements.append(self.__value)
            # Then visit the left node if present
            if self.__left is not None:
                elements += self.__left.preOrderTraverse()
            # Then visit the right node if present
            if self.__right is not None:
                elements += self.__right.preOrderTraverse()

        return elements

    def inOrderTraverse(self):
        """
        Performs a in-order traversal of the tree.
        Returns the list of elements
        """
        elements = []
        if self.__value is not None:
            # Visit the left node first if present
            if self.__left is not None:
                elements += self.__left.inOrderTraverse()
            # Then visit the root node
            elements.append(self.__value)
            # Then visit the right node if present
            if self.__right is not None:
                elements += self.__right.inOrderTraverse()

        return elements


    def postOrderTraverse(self):
        """
        Performs a post-order traversal of the tree.
        Returns the list of elements
        """
        elements = []
        if self.__value is not None:
            # Visit the left node first if present
            if self.__left is not None:
                elements += self.__left.postOrderTraverse()
            # Then visit the right node if present
            if self.__right is not None:
                elements += self.__right.postOrderTraverse()
            # Then visit the root node
            elements.append(self.__value)

        return elements


if __name__ == "__main__":
    binarytree = BinaryTree()
    binarytree.addValue(23)
    binarytree.addValue(15)
    binarytree.addValue(19)
    binarytree.addValue(8)
    binarytree.addValue(7)
    binarytree.addValue(42)
    binarytree.addValue(0)
    binarytree.addValue(1)
    elements = binarytree.preOrderTraverse()

    print("Pre order traversal: ", end="")
    for val in elements:
        print(val, end=" ")

    print("")

    elements = binarytree.inOrderTraverse()

    print("In order traversal: ", end="")
    for val in elements:
        print(val, end=" ")

    print("")

    elements = binarytree.postOrderTraverse()

    print("Post order traversal: ", end="")
    for val in elements:
        print(val, end=" ")

    print("")
PEP 8 says

You have minor issues with the code layout:


PEP 8 requires two empty lines before the class definition,
Modules os and math are not used,
In addValue the return statement is unnecessary,
PEP 8 proposes the method names in this_format; rename addValue to add_value.


Improving the code

I suggest you try to take use of a visitor pattern: in your traversal methods add new argument taking a function that is called with a current binary tree node. I had this in mind:

from __future__ import print_function


class BinaryTree:
    def __init__(self):
        self.__left = None
        self.__right = None
        self.__value = None

    def add_value(self, value):
        if self.__value is None:
            self.__value = value

        elif value &gt; self.__value:
            if self.__right is None:
                self.__right = BinaryTree()
            self.__right.add_value(value)

        elif value &lt; self.__value:
            if self.__left is None:
                self.__left = BinaryTree()
            self.__left.add_value(value)

    def pre_order_traverse(self, visitor_func=None):
        """
        Performs a pre-order traversal of the tree.
        Returns the list of elements
        """
        if self.__value is not None:
            # Visit the root node first
            if visitor_func:
                visitor_func(self.__value)
            # Then visit the left node if present
            if self.__left is not None:
                self.__left.pre_order_traverse(visitor_func)
            # Then visit the right node if present
            if self.__right is not None:
                self.__right.pre_order_traverse(visitor_func)

    def in_order_traverse(self, visitor_func=None):
        """
        Performs a in-order traversal of the tree.
        Returns the list of elements
        """
        if self.__value is not None:
            # Visit the left node first if present
            if self.__left is not None:
                self.__left.in_order_traverse(visitor_func)
            # Then visit the root node
            if visitor_func:
                visitor_func(self.__value)
            # Then visit the right node if present
            if self.__right is not None:
                self.__right.in_order_traverse(visitor_func)

    def post_order_traverse(self, visitor_func=None):
        """
        Performs a post-order traversal of the tree.
        Returns the list of elements
        """
        if self.__value is not None:
            # Visit the left node first if present
            if self.__left is not None:
                self.__left.post_order_traverse(visitor_func)
            # Then visit the right node if present
            if self.__right is not None:
                self.__right.post_order_traverse(visitor_func)
            # Then visit the root node
            if visitor_func:
                visitor_func(self.__value)


if __name__ == "__main__":
    binarytree = BinaryTree()
    binarytree.add_value(23)
    binarytree.add_value(15)
    binarytree.add_value(19)
    binarytree.add_value(8)
    binarytree.add_value(7)
    binarytree.add_value(42)
    binarytree.add_value(0)
    binarytree.add_value(1)

    def visitor1(value):
        print(value)

    class ElementVisitor:
        def __init__(self):
            self.elements = []

        def add(self, value):
            self.elements.append(value)

    ev = ElementVisitor()
    binarytree.pre_order_traverse(ev.add)
    print(ev.elements)

    print("")

    binarytree.in_order_traverse(visitor1)

    print("")

    binarytree.post_order_traverse(visitor1)


As you can see, above you can both print the nodes and collect them in a list; both by simply passing an appropriate function/object method.

Hope that helps.