-
Notifications
You must be signed in to change notification settings - Fork 0
/
createRandomTree.py
37 lines (28 loc) · 989 Bytes
/
createRandomTree.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
import random
class Node:
'''Node contains value, left child, and right child'''
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def insert(self, value):
if value < self.value:
if self.left is not None:
self.left.insert(value)
else:
self.left = Node(value)
print str(value) + ' is left child of ' + str(self.value)
else:
if self.right is not None:
self.right.insert(value)
else:
self.right = Node(value)
print str(value) + ' is right child of ' + str(self.value)
def createTree(root, numElts):
for i in range(1, numElts):
root.insert(random.randrange(0, 21))
def getBinaryTree(numElts, randSeed):
random.seed(randSeed)
root = Node(random.randrange(0, 21))
createTree(root, numElts)
return root