Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 2f5c4c8

Browse files
committed
XD
1 parent 9482518 commit 2f5c4c8

File tree

3 files changed

+52
-44
lines changed

3 files changed

+52
-44
lines changed

Graphic.py

Lines changed: 0 additions & 44 deletions
This file was deleted.
924 Bytes
Binary file not shown.

graphical.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class TreeNode:
2+
def __init__(self, val, left=None, right=None):
3+
self.val = val
4+
self.left = left
5+
self.right = right
6+
def __repr__(self):
7+
return 'TreeNode({})'.format(self.val)
8+
9+
def deserialize(string):
10+
if string == '{}':
11+
return None
12+
nodes = [None if val == 'null' else TreeNode(int(val))
13+
for val in string.strip('[]{}').split(',')]
14+
kids = nodes[::-1]
15+
root = kids.pop()
16+
for node in nodes:
17+
if node:
18+
if kids: node.left = kids.pop()
19+
if kids: node.right = kids.pop()
20+
return root
21+
22+
def drawtree(root):
23+
def height(root):
24+
return 1 + max(height(root.left), height(root.right)) if root else -1
25+
def jumpto(x, y):
26+
t.penup()
27+
t.goto(x, y)
28+
t.pendown()
29+
def draw(node, x, y, dx):
30+
if node:
31+
t.goto(x, y)
32+
jumpto(x, y-20)
33+
t.write(node.val, align='center', font=('Arial', 12, 'normal'))
34+
draw(node.left, x-dx, y-60, dx/2)
35+
jumpto(x, y-20)
36+
draw(node.right, x+dx, y-60, dx/2)
37+
import turtle
38+
t = turtle.Turtle()
39+
t.speed(0); turtle.delay(0)
40+
h = height(root)
41+
jumpto(0, 30*h)
42+
draw(root, 0, 30*h, 40*h)
43+
t.hideturtle()
44+
turtle.mainloop()
45+
46+
if __name__ == '__main__':
47+
drawtree(deserialize('[2,3,4,5,6]'))
48+
49+
50+
51+
# BINARY TREE EXPLANTION
52+

0 commit comments

Comments
 (0)