-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Tree
More file actions
56 lines (42 loc) · 1.09 KB
/
Binary Tree
File metadata and controls
56 lines (42 loc) · 1.09 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
import timeit
class Binarynode:
def __init__(self, value = None):
self.value = value
self.left = None
self.right = None
def add(self, val):
if val <= self.value:
if self.left:
self.left.add(val)
else:
self.left = Binarynode(val)
else:
if self.right:
self.right.add(val)
else:
self.right = Binarynode(val)
class binaryT:
def __init__(self):
self.root = None
def add(self, value):
if self.root == None:
self.root = Binarynode(value)
else:
self.root.add(value)
def find(self, target):
node = self.root
while node:
if target == node.value:
return True
else:
if target < node.value:
node = node.left
else:
node = node.right
return False
bt = binaryT()
bt.add(5)
bt.add(3)
bt.add(1)
print("find 5", bt.find(5))
print("find 3", bt.find(3))