implement 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: 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