This repository was archived by the owner on Feb 3, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +79
-0
lines changed
Expand file tree Collapse file tree 2 files changed +79
-0
lines changed Original file line number Diff line number Diff line change 1+ from math import acos
2+ from turtle import Turtle , Screen
3+
4+ DOT_DIAMETER = 20
5+ GENERATION_DISTANCE = 75
6+
7+ def tree (turtle , d , origin ):
8+ # d is the depth
9+
10+ turtle .penup ()
11+ turtle .setposition (origin )
12+ turtle .dot (DOT_DIAMETER )
13+
14+ if d == 0 : # base case
15+ return
16+
17+ distance = (GENERATION_DISTANCE ** 2 + (2 ** d * DOT_DIAMETER / 2 )** 2 )** 0.5
18+ angle = acos (GENERATION_DISTANCE / distance )
19+
20+ turtle .pendown ()
21+ turtle .left (angle )
22+ turtle .forward (distance )
23+ upper = turtle .position ()
24+ turtle .right (angle )
25+
26+ turtle .penup ()
27+ turtle .setposition (origin )
28+ turtle .pendown ()
29+ turtle .right (angle )
30+ turtle .forward (distance )
31+ lower = turtle .position ()
32+ turtle .left (angle )
33+
34+ tree (turtle , d - 1 , upper ) # recurse upper branch
35+ tree (turtle , d - 1 , lower ) # recurse lower branch
36+
37+ screen = Screen ()
38+
39+ yertle = Turtle ()
40+ yertle .radians () # to accommodate acos()
41+
42+ tree (yertle , 3 , (- 150 , 0 ))
43+
44+ screen .mainloop ()
Original file line number Diff line number Diff line change 1+ class Node :
2+ def __init__ (self , data ) :
3+ self .left = None
4+ self .right = None
5+ self .data = data
6+
7+ def insert (self , data ) :
8+ if data < self .data :
9+ if self .left :
10+ self .left .insert (data )
11+ else :
12+ self .left = Node (data )
13+ return
14+
15+ else :
16+ if self .right :
17+ self .right .insert (data )
18+ else :
19+ self .right = Node (data )
20+ return
21+
22+ def PrintTree (self ) :
23+ if self .left :
24+ self .left .PrintTree ()
25+ print (self .data )
26+ if self .right :
27+ self .right .PrintTree ()
28+
29+ l = Node (2 )
30+ l .insert (5 )
31+ l .insert (1 )
32+ l .insert (7 )
33+ l .insert (9 )
34+ l .insert (10 )
35+ l .PrintTree ()
You can’t perform that action at this time.
0 commit comments