-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.py
More file actions
73 lines (62 loc) · 1.85 KB
/
Copy pathBinarySearchTree.py
File metadata and controls
73 lines (62 loc) · 1.85 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Author: Jingze Dai
# Email Address: daij24@mcmaster.ca or david1147062956@163.com
# Github: https://github.com/daijingz
# Linkedin: https://www.linkedin.com/in/jingze-dai/
# Description: Binary Search Tree (BST)
class BinarySearchTree:
def __init__(self, data=None):
if data is not None:
if type(data) not in [int, str]:
raise Exception()
self.__data = data
self.__type = type(data)
else:
self.__data = None
self.__type = None
self.__left = None
self.__right = None
def get_data(self):
try:
return self.__data
except:
raise Exception()
def get_type(self):
try:
return self.__type
except:
raise Exception()
def get_left(self):
try:
return self.__left
except:
raise Exception()
def get_right(self):
try:
return self.__right
except:
raise Exception()
def set_data(self, data):
try:
if type(data) != self.__type:
raise Exception
self.__data = data
except:
raise Exception()
def set_type(self, new_type):
try:
if new_type not in [int, str]:
raise Exception()
self.__type = new_type
self.__data = None
self.__left = None
self.__right = None
except:
raise Exception()
def set_left(self, left):
if not isinstance(left, BinarySearchTree):
raise Exception()
self.__left = left
def set_right(self, right):
if not isinstance(right, BinarySearchTree):
raise Exception()
self.__left = right