Home
Classrooms
Courses
About Us
Resources
Members
More
Short Questions
Programming: 2002-2003#8
cec
a, b = input().split(', ')
class Node():
def __init__(self, value):
self.value = value
self.left = self.right = None
def insert(self, value):
if value <= self.value:
if self.left == None:
self.left = Node(value)
# print(f'{value} is the left child of {self.value}')
else:
self.left.insert(value)
if value > self.value:
if self.right == None:
self.right = Node(value)
# print(f'{value} is the right child of {self.value}')
self.right.insert(value)
def match(self, other):
if self.left == None and self.right == None:
return True
if other.left == None and self.left != None:
return False
if other.right == None and self.right != None:
return self.right.match(other.right)
return self.left.match(other.left)
return self.left.match(other.left) and self.right.match(other.right)
def traverse(self):
# print(self.value, self.left, self.right)
yield self
if self.left != None:
yield from self.left.traverse()
if self.right != None:
yield from self.right.traverse()
A = Node(a[0])
for c in a[1:]:
A.insert(c)
B = Node(b[0])
for c in b[1:]:
B.insert(c)
for node in B.traverse():
if A.match(node):
print(node.value)
cec
a, b = input().split(', ')
class Node():
def __init__(self, value):
self.value = value
self.left = self.right = None
def insert(self, value):
if value <= self.value:
if self.left == None:
self.left = Node(value)
# print(f'{value} is the left child of {self.value}')
else:
self.left.insert(value)
if value > self.value:
if self.right == None:
self.right = Node(value)
# print(f'{value} is the right child of {self.value}')
else:
self.right.insert(value)
def match(self, other):
if self.left == None and self.right == None:
return True
if other.left == None and self.left != None:
return False
if other.right == None and self.right != None:
return False
if self.left == None:
return self.right.match(other.right)
if self.right == None:
return self.left.match(other.left)
return self.left.match(other.left) and self.right.match(other.right)
def traverse(self):
# print(self.value, self.left, self.right)
yield self
if self.left != None:
yield from self.left.traverse()
if self.right != None:
yield from self.right.traverse()
A = Node(a[0])
for c in a[1:]:
A.insert(c)
B = Node(b[0])
for c in b[1:]:
B.insert(c)
for node in B.traverse():
if A.match(node):
print(node.value)