#!/usr/bin/env python
# -*- coding: utf-8 -*-


class Binary(object):
    def __init__(self, root):
        self.key = root
        self.left_child = None
        self.right_child = None

    def insert_left(self, new_node):
        if self.left_child is None:
            self.left_child = Binary(new_node)
        else:
            t = Binary(new_node)
            t.left_child = self.left_child
            self.left_child = t

    def insert_right(self, new_node):
        if self.right_child is None:
            self.right_child = Binary(new_node)
        else:
            t = Binary(new_node)
            t.right_child = self.right_child
            self.right_child = t

    def get_value(self):
        return self.key

if __name__ == "__main__":
    b = Binary("test")
    print b.left_child, b.right_child
    b.insert_left("1left")
    print b.left_child, b.right_child
    b.insert_left("1right")
    print b.left_child.left_child, b.right_child


#!/usr/bin/env python
# -*- coding: utf-8 -*-


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


def pre_create(root):
    value = raw_input('enter a node:')
    if value == "#":
        root = None
    else:
        root = Node(value)
        root.left = pre_create(root.left)
        root.right = pre_create(root.right)
    return root


def pre_order(root):
    if root is None:
        return
    else:
        print root.root,
        pre_order(root.left)
        pre_order(root.right)


def in_order(root):
    if root is None:
        return
    else:
        in_order(root.left)
        print root.root,
        in_order(root.right)


def post_order(root):
    if root is None:
        return
    else:
        post_order(root.left)
        post_order(root.right)
        print root.root,


def get_leaf_num(root):
    if root is None:
        return 0
    if root.right is None and root.left is None:
        return 1
    return get_leaf_num(root.right) + get_leaf_num(root.left)


def get_depth(root):
    if root is None:
        return 0
    left_depth = get_depth(root.left)
    right_depth = get_depth(root.right)
    return left_depth + 1 if left_depth > right_depth else right_depth + 1

if __name__ == "__main__":
    root = None
    root = pre_create(root)
    pre_order(root)
    print
    in_order(root)
    print
    post_order(root)
    print
    print get_leaf_num(root)
    print get_depth(root)


update binary by Python