Apuntes_Python/03_arbol_binario/02_valores_nivel_primero.py

217 lines
4.2 KiB
Python
Raw Normal View History

2022-12-24 22:41:20 -03:00
#!/usr/bin/env python
"""
Valores de Nivel Primero
Escribe una función, ValoresNivelPrioridad, que reciba la raíz
de un árbol binario.
La función debe retornar una lista que contenga todos los valores del arbol
priorizando el nivel del nodo a la profundidad de este.
"""
from arbol_binario import Nodo, ValoresPrioridadNivel, ValoresPrioridadNivel2
import unittest
import TestRunner
def test_00():
"""
a
/ \
b c
/ \ \
d e f
breadth_first_values(a)
-> ['a', 'b', 'c', 'd', 'e', 'f']
"""
a = Nodo('a')
b = Nodo('b')
c = Nodo('c')
d = Nodo('d')
e = Nodo('e')
f = Nodo('f')
a.left = b
a.right = c
b.left = d
b.right = e
c.right = f
return ValoresPrioridadNivel(a)
def test_01():
"""
a
/ \
b c
/ \ \
d e f
/ \
g h
breadth_first_values(a)
-> ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
"""
a = Nodo('a')
b = Nodo('b')
c = Nodo('c')
d = Nodo('d')
e = Nodo('e')
f = Nodo('f')
g = Nodo('g')
h = Nodo('h')
a.left = b
a.right = c
b.left = d
b.right = e
c.right = f
e.left = g
f.right = h
return ValoresPrioridadNivel(a)
def test_02():
"""
a
breadth_first_values(a)
-> ['a']
"""
a = Nodo('a')
return ValoresPrioridadNivel(a)
def test_03():
"""
a
\
b
/
c
/ \
x d
\
e
breadth_first_values(a)
-> ['a', 'b', 'c', 'x', 'd', 'e']
"""
a = Nodo('a')
b = Nodo('b')
c = Nodo('c')
d = Nodo('d')
e = Nodo('e')
x = Nodo('x')
a.right = b
b.left = c
c.left = x
c.right = d
d.right = e
return ValoresPrioridadNivel(a)
def test_04():
"""
howHigh(None)
-> []
"""
return ValoresPrioridadNivel(None)
class ValoresPrioridadNivelTestCase(unittest.TestCase):
def test_valores_por_nivel_primero00(self):
self.assertEqual(['a', 'b', 'c', 'd', 'e', 'f'], test_00())
def test_valores_por_nivel_primero01(self):
self.assertEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], test_01())
def test_valores_por_nivel_primero02(self):
self.assertEqual(['a'], test_02())
def test_valores_por_nivel_primero03(self):
self.assertEqual(['a', 'b', 'c', 'x', 'd', 'e'], test_03())
def test_valores_por_nivel_primero04(self):
self.assertEqual([], test_04())
def test_10():
a = Nodo('a')
b = Nodo('b')
c = Nodo('c')
d = Nodo('d')
e = Nodo('e')
f = Nodo('f')
a.left = b
a.right = c
b.left = d
b.right = e
c.right = f
return ValoresPrioridadNivel2(a)
def test_11():
a = Nodo('a')
b = Nodo('b')
c = Nodo('c')
d = Nodo('d')
e = Nodo('e')
f = Nodo('f')
g = Nodo('g')
h = Nodo('h')
a.left = b
a.right = c
b.left = d
b.right = e
c.right = f
e.left = g
f.right = h
return ValoresPrioridadNivel2(a)
def test_12():
a = Nodo('a')
return ValoresPrioridadNivel2(a)
def test_13():
a = Nodo('a')
b = Nodo('b')
c = Nodo('c')
d = Nodo('d')
e = Nodo('e')
x = Nodo('x')
a.right = b
b.left = c
c.left = x
c.right = d
d.right = e
return ValoresPrioridadNivel2(a)
def test_14():
return ValoresPrioridadNivel2(None)
class ValoresPrioridadNivelTestCase2(unittest.TestCase):
def test_valores_por_nivel_primero10(self):
self.assertEqual(['a', 'b', 'c', 'd', 'e', 'f'], test_10())
def test_valores_por_nivel_primero11(self):
self.assertEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], test_11())
def test_valores_por_nivel_primero12(self):
self.assertEqual(['a'], test_12())
def test_valores_por_nivel_primero13(self):
self.assertEqual(['a', 'b', 'c', 'x', 'd', 'e'], test_13())
def test_valores_por_nivel_primero14(self):
self.assertEqual([], test_14())
if __name__ == '__main__':
TestRunner.main()
#unittest.main()