-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_iterator.py
38 lines (33 loc) · 907 Bytes
/
test_iterator.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
from unittest import *
class Node:
def __init__(self, value, left=None, right=None):
self.right = right
self.left = left
self.value = value
self.parent = None
if left:
self.left.parent = self
if right:
self.right.parent = self
def traverse_preorder(self):
def traverse(current):
yield current
if current.left:
for left in traverse(current.left):
yield left
if current.right:
for right in traverse(current.right):
yield right
for node in traverse(self):
yield node.value
class Evaluate(TestCase):
def test_exercise(self):
node = Node('a',
Node('b',
Node('c'),
Node('d')),
Node('e'))
self.assertEqual(
'abcde',
''.join([x for x in node.traverse_preorder()])
)