tree implementation in python code example

Example 1: how to crate a binary tree in python

class Node:

    def __init__(self, data):

        self.left = None
        self.right = None
        self.data = data

    def insert(self, data):
# Compare the new value with the parent node
        if self.data:
            if data < self.data:
                if self.left is None:
                    self.left = Node(data)
                else:
                    self.left.insert(data)
            elif data > self.data:
                if self.right is None:
                    self.right = Node(data)
                else:
                    self.right.insert(data)
        else:
            self.data = data

# Print the tree
    def PrintTree(self):
        if self.left:
            self.left.PrintTree()
        print( self.data),
        if self.right:
            self.right.PrintTree()

# Use the insert method to add nodes
root = Node(12)
root.insert(6)
root.insert(14)
root.insert(3)

root.PrintTree()

Example 2: python tree library

# pypi, the Python Package Index, suggests tinytree, treedict, caxes, 
# treelib, pyavl... these are just the top few after filtering away the 
# many accidental hits (which point to specific tree such as XML ones, 
#                 AST ones, etc, etc;-). If you clarify what you want 
# to do with your trees it may be easier to suggest a specific package.

pip install treelib pyavl tinytree treedict caxes
# or 
pip3 install treelib pyavl tinytree treedict caxes

Example 3: how to make a tree by python

import turtle
arrow=turtle.Turtle()
arrow.color ("green")
window=turtle.Screen()
window.bgcolor("cyan")
arrow.begin_fill()
arrow.forward(100)
arrow.setheading(120)
arrow.forward(100)
arrow.setheading(240)
arrow.forward(100)
arrow.setheading(270)
arrow.end_fill()
arrow.penup()
arrow.forward(60)
arrow.pendown()
arrow.begin_fill()
arrow.setheading(0)
arrow.forward(100)
arrow.setheading(120)
arrow.forward(100)
arrow.setheading(240)
arrow.forward(100)
arrow.setheading(270)
arrow.end_fill()
arrow.penup()
arrow.setheading(0)
arrow.forward(40)
arrow.pendown()
arrow.begin_fill()
arrow.color("brown")
arrow.setheading(270)
arrow.forward(50)
arrow.setheading(0)
arrow.forward(20)
arrow.setheading(90)
arrow.forward(50)
arrow.end_fill()
arrow.penup()
arrow.forward(150)
arrow.setheading(180)
arrow.forward (125)
arrow.pendown()
arrow.color ("green")
arrow.begin_fill()
arrow.setheading(120)
arrow.forward(100)
arrow.setheading(240)
arrow.forward(100)
arrow.setheading(270)
arrow.end_fill()
arrow.penup()
arrow.forward(60)
arrow.pendown()
arrow.begin_fill()
arrow.setheading(0)
arrow.forward(100)
arrow.setheading(120)
arrow.forward(100)
arrow.setheading(240)
arrow.forward(100)
arrow.setheading(270)
arrow.end_fill()
arrow.penup()
arrow.setheading(0)
arrow.forward(40)
arrow.pendown()
arrow.begin_fill()
arrow.color("brown")
arrow.setheading(270)
arrow.forward(50)
arrow.setheading(0)
arrow.forward(20)
arrow.setheading(90)
arrow.forward(50)
arrow.end_fill()
arrow.penup()
arrow.forward(30)
arrow.color("green")
turtle.done

Example 4: binary tree in python

#!/usr/bin/python

class Node:
    def __init__(self, val):
        self.l = None
        self.r = None
        self.v = val

class Tree:
    def __init__(self):
        self.root = None

    def getRoot(self):
        return self.root

    def add(self, val):
        if(self.root == None):
            self.root = Node(val)
        else:
            self._add(val, self.root)

    def _add(self, val, node):
        if(val < node.v):
            if(node.l != None):
                self._add(val, node.l)
            else:
                node.l = Node(val)
        else:
            if(node.r != None):
                self._add(val, node.r)
            else:
                node.r = Node(val)

    def find(self, val):
        if(self.root != None):
            return self._find(val, self.root)
        else:
            return None

    def _find(self, val, node):
        if(val == node.v):
            return node
        elif(val < node.v and node.l != None):
            self._find(val, node.l)
        elif(val > node.v and node.r != None):
            self._find(val, node.r)

    def deleteTree(self):
        # garbage collector will do this for us. 
        self.root = None

    def printTree(self):
        if(self.root != None):
            self._printTree(self.root)

    def _printTree(self, node):
        if(node != None):
            self._printTree(node.l)
            print str(node.v) + ' '
            self._printTree(node.r)

#     3
# 0     4
#   2      8
tree = Tree()
tree.add(3)
tree.add(4)
tree.add(0)
tree.add(8)
tree.add(2)
tree.printTree()
print (tree.find(3)).v
print tree.find(10)
tree.deleteTree()
tree.printTree()