tree 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