186 lines
3.1 KiB
Python
Executable File
186 lines
3.1 KiB
Python
Executable File
#!/usr/bin/env python
|
|
"""
|
|
Suma de los Valores del Arbol
|
|
|
|
Escribe una funcion, SumaValores, que reciba la raíz de un
|
|
árbol binario.
|
|
Debe retornar la suma total de los valores de los nodos
|
|
"""
|
|
from arbol_binario import Nodo, SumaValores, SumaValores2, SumaValores3
|
|
import unittest
|
|
import TestRunner
|
|
|
|
|
|
def test_00():
|
|
""" test_00
|
|
3
|
|
/ \
|
|
11 4
|
|
/ \ \
|
|
4 -2 1
|
|
tree_sum(a) # -> 21
|
|
"""
|
|
a = Nodo(3)
|
|
b = Nodo(11)
|
|
c = Nodo(4)
|
|
d = Nodo(4)
|
|
e = Nodo(-2)
|
|
f = Nodo(1)
|
|
a.left = b
|
|
a.right = c
|
|
b.left = d
|
|
b.right = e
|
|
c.right = f
|
|
return SumaValores(a)
|
|
|
|
|
|
def test_01():
|
|
"""
|
|
1
|
|
/ \
|
|
6 0
|
|
/ \ \
|
|
3 -6 2
|
|
/ \
|
|
2 2
|
|
tree_sum(a) # -> 10
|
|
"""
|
|
a = Nodo(1)
|
|
b = Nodo(6)
|
|
c = Nodo(0)
|
|
d = Nodo(3)
|
|
e = Nodo(-6)
|
|
f = Nodo(2)
|
|
g = Nodo(2)
|
|
h = Nodo(2)
|
|
a.left = b
|
|
a.right = c
|
|
b.left = d
|
|
b.right = e
|
|
c.right = f
|
|
e.left = g
|
|
f.right = h
|
|
return SumaValores(a)
|
|
|
|
def test_02():
|
|
"""
|
|
tree_sum(None) # -> 0
|
|
"""
|
|
return SumaValores(None)
|
|
|
|
|
|
class SumaValoresTestCase(unittest.TestCase):
|
|
|
|
def test_suma_valores_00(self):
|
|
self.assertEqual(21, test_00())
|
|
|
|
def test_suma_valores_01(self):
|
|
self.assertEqual(10, test_01())
|
|
|
|
def test_suma_valores_02(self):
|
|
self.assertEqual(0, test_02())
|
|
|
|
|
|
def test_20():
|
|
a = Nodo(3)
|
|
b = Nodo(11)
|
|
c = Nodo(4)
|
|
d = Nodo(4)
|
|
e = Nodo(-2)
|
|
f = Nodo(1)
|
|
a.left = b
|
|
a.right = c
|
|
b.left = d
|
|
b.right = e
|
|
c.right = f
|
|
return SumaValores2(a)
|
|
|
|
|
|
def test_21():
|
|
a = Nodo(1)
|
|
b = Nodo(6)
|
|
c = Nodo(0)
|
|
d = Nodo(3)
|
|
e = Nodo(-6)
|
|
f = Nodo(2)
|
|
g = Nodo(2)
|
|
h = Nodo(2)
|
|
a.left = b
|
|
a.right = c
|
|
b.left = d
|
|
b.right = e
|
|
c.right = f
|
|
e.left = g
|
|
f.right = h
|
|
return SumaValores2(a)
|
|
|
|
def test_22():
|
|
return SumaValores2(None)
|
|
|
|
|
|
class SumaValoresTestCase2(unittest.TestCase):
|
|
|
|
def test_suma_valores_20(self):
|
|
self.assertEqual(21, test_20())
|
|
|
|
def test_suma_valores_21(self):
|
|
self.assertEqual(10, test_21())
|
|
|
|
def test_suma_valores_22(self):
|
|
self.assertEqual(0, test_22())
|
|
|
|
|
|
def test_30():
|
|
a = Nodo(3)
|
|
b = Nodo(11)
|
|
c = Nodo(4)
|
|
d = Nodo(4)
|
|
e = Nodo(-2)
|
|
f = Nodo(1)
|
|
a.left = b
|
|
a.right = c
|
|
b.left = d
|
|
b.right = e
|
|
c.right = f
|
|
return SumaValores2(a)
|
|
|
|
|
|
def test_31():
|
|
a = Nodo(1)
|
|
b = Nodo(6)
|
|
c = Nodo(0)
|
|
d = Nodo(3)
|
|
e = Nodo(-6)
|
|
f = Nodo(2)
|
|
g = Nodo(2)
|
|
h = Nodo(2)
|
|
a.left = b
|
|
a.right = c
|
|
b.left = d
|
|
b.right = e
|
|
c.right = f
|
|
e.left = g
|
|
f.right = h
|
|
return SumaValores2(a)
|
|
|
|
def test_32():
|
|
return SumaValores2(None)
|
|
|
|
|
|
class SumaValoresTestCase3(unittest.TestCase):
|
|
|
|
def test_suma_valores_20(self):
|
|
self.assertEqual(21, test_30())
|
|
|
|
def test_suma_valores_21(self):
|
|
self.assertEqual(10, test_31())
|
|
|
|
def test_suma_valores_22(self):
|
|
self.assertEqual(0, test_32())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
TestRunner.main()
|
|
#unittest.main()
|
|
|