-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarytree.py
44 lines (38 loc) · 1.31 KB
/
binarytree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import random
class Node:
"""A node for a binary tree"""
def __init__(self, val):
"""Constructor for a node with a value"""
self.val = val
self.left = None
self.right = None
def __str__(self):
"""Returns the node's value as a string"""
return str(self.val)
def printTree(self, prefix="", symbol="-"):
"""Prints a text-based graphical respresentation of the binary tree"""
if len(prefix) == 0:
print(self, "(ROOT)")
else:
print(f"{prefix}--{symbol}--> {self}")
if self.right is not None:
self.right.printTree(prefix+" "+("|"if self.left is not None else " "), "R")
if self.left is not None:
self.left.printTree(prefix+" ", "L")
def insert(self, val):
if val < self.val:
# insert on left
if self.left is None:
self.left = Node(val)
else:
self.left.insert(val)
else:
#insert of right
if self.right is None:
self.right = Node(val)
else:
self.right.insert(val)
root = Node(25)
for i in range(20):
root.insert(random.randrange(50))
root.printTree()