-
Notifications
You must be signed in to change notification settings - Fork 0
/
crack_4_8.py
45 lines (41 loc) · 1.05 KB
/
crack_4_8.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
class BinaryTree(object):
"""docstring for BinaryTree"""
def __init__(self, data):
self.data = data
self.parent = self.left = self.right = None
def addTree(tnode, data):
if tnode is None:
tnode = BinaryTree(data)
elif tnode.data > data:
tnode.right = addTree(tnode.right, data)
tnode.right.parent = tnode
elif tnode.data < data:
tnode.left = addTree(tnode.left, data)
tnode.left.parent = tnode
return tnode
def printTree(tnode):
if tnode is not None:
print tnode.data
printTree(tnode.left)
printTree(tnode.right)
def printAdd(tnode, add):
if tnode.data >add:
printAdd(tnode.left, add)
printAdd(tnode.right, add)
elif tnode.data = add:
print tnode.data
else:
if printSon(tnode.left, add-tnode.data) or printSon(tnode.right, add-tnode.data):
print tnode.data
def printSon(tnode, add):
if tnode.data > add:
return False
elif tnode.data == add:
print tnode.data
return True
else:
if printSon(tnode.left, add-tnode.data) or printSon(tnode.right, add-tnode.data):
print tnode.data
return True
else:
return False