-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarytree.py
70 lines (58 loc) · 1.59 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
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
class Node(object):
def __init__(self, data=None):
self.data = data
self.left_child = None
self.right_child = None
def insert(self, data):
if self.data == data:
return False
if data < self.data:
if self.left_child:
return self.left_child.insert(data)
else:
self.left_child = Node(data)
return True
else:
if self.right_child:
return self.right_child.insert(data)
else:
self.right_child = Node(data)
return True
def find(self, data):
if data == self.data:
return True
if data < self.data:
if data.left_child:
return data.left_child.find(data)
else:
return False
if data > self.data:
if self.right_child:
return self.right_child.find(data)
else:
return False
class Tree(object):
def __init__(self):
self.root = None
def insert(self, data):
if self.root:
return self.root.insert(data)
else:
self.root = Node(data)
return True
def find(self, data):
if self.root:
return self.root.find(data)
else:
return False
if __name__ == '__main__':
tree = Tree()
tree.insert(5)
tree.insert(2)
tree.insert(3)
tree.insert(1)
tree.insert(8)
tree.insert(9)
tree.insert(7)
assert tree.find(9)
assert not tree.find(12)