-
Notifications
You must be signed in to change notification settings - Fork 0
/
question_2_c.py
53 lines (42 loc) · 1.63 KB
/
question_2_c.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
45
46
47
48
49
50
51
52
53
# Project 1 Question 2 part c code - Python
# Created by Ryan Doherty
# Using the properties of a BST can make creating a sorted list very easy. Since we
# know that: left < root < right, we can simply do an In-order traversal to print a list of
# increasing value. If we start with an unsorted list, we just feed that data into a new
# binary tree, then perform the In-order traversal.
# (5 points) (You must submit code for this question! ) Implement the algorithm that
# you described above in sort().
# My Node class from earlier questions can implement this:
class Node:
def __init__(self, val):
self.left = None
self.right = None
self.val = val
# Inserts a value into the BST recursively:
def insertRec(self, val):
if self.val:
if val < self.val:
if self.left is None:
self.left = Node(val)
else:
self.left.insertRec(val)
elif val > self.val:
if self.right is None:
self.right = Node(val)
else:
self.right.insertRec(val)
else:
self.val = val
def printTree(self):
if self.left:
self.left.printTree()
print(self.val, end=' ')
if self.right:
self.right.printTree()
if __name__ == "__main__":
root = Node(None)
arr_in = [5, 4, 12, 2, 0, 9, 58, 82, 6, 44, 25]
for n in arr_in:
root.insertRec(n)
print("Array in: ", arr_in, '\n', 'Tree out: ', sep='', end='')
root.printTree()