-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_shape.py
More file actions
86 lines (66 loc) · 1.8 KB
/
Copy pathbinary_shape.py
File metadata and controls
86 lines (66 loc) · 1.8 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
74
75
76
77
78
79
80
81
82
83
84
85
86
import sys
class BinaryTree: #Make sure you're only adding ints!!!
def __init__(self, data):
self.data = data #ROOT
self.right = None
self.left = None
def add_item(self, item):
if self.data:
if item < self.data:
if self.left is None:
self.left = BinaryTree(item)
else:
self.left.add_item(item)
else:
if self.right is None:
self.right = BinaryTree(item)
else:
self.right.add_item(item)
else:
self.data = item
# Print the tree
def PrintTree(self):
if self.left:
self.left.PrintTree()
print(self.data)
if self.right:
self.right.PrintTree()
def makeTree(line, size):
listLine = line.split()
tree = BinaryTree(int(listLine[0]))
idx = 1
while idx < size:
tree.add_item(int(listLine[idx]))
idx += 1
return tree
def sameShape(tree1, tree2):
if tree1 == None and tree2 == None:
return True;
if tree1 != None and tree2 != None:
return sameShape(tree1.left, tree2.left) and sameShape(tree1.right, tree2.right)
return False
uniques = set()
firstLine = sys.stdin.readline().split()
n = int(firstLine[0])
k = int(firstLine[1])
count = 1
nextLine = sys.stdin.readline()
firstTree = makeTree(nextLine, k)
uniques.add(firstTree)
if count == n:
print(1)
exit()
for line in sys.stdin:
count += 1
currTree = makeTree(line, k)
unique = True
for uniqueTree in uniques:
if sameShape(currTree, uniqueTree):
unique = False
break
if unique:
uniques.add(currTree)
if count >= n:
break
print(len(uniques))
exit()