277 lines
4.9 KiB
Python
Executable File
277 lines
4.9 KiB
Python
Executable File
#!/usr/bin/env python
|
|
"""
|
|
Arbol Incluye Valor
|
|
|
|
Escribe una función, ArbolIncluye, que reciba la raiz de un
|
|
árbol binario, y un valor a buscar.
|
|
La función debe retornar un booleano que indique si existe el
|
|
elemento en el árbol.
|
|
"""
|
|
from arbol_binario import Nodo, ArbolIncluye, ArbolIncluye2
|
|
import unittest
|
|
import TestRunner
|
|
|
|
|
|
def test_00():
|
|
""" a
|
|
/ \
|
|
b c
|
|
/ \ \
|
|
d e f
|
|
tree_includes(a, "e") # -> True
|
|
"""
|
|
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 ArbolIncluye(a, "e")
|
|
|
|
|
|
def test_01():
|
|
""" a
|
|
/ \
|
|
b c
|
|
/ \ \
|
|
d e f
|
|
tree_includes(a, "a") # -> True
|
|
"""
|
|
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 ArbolIncluye(a, "a")
|
|
|
|
|
|
def test_02():
|
|
""" a
|
|
/ \
|
|
b c
|
|
/ \ \
|
|
d e f
|
|
tree_includes(a, "n") # -> False
|
|
"""
|
|
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 ArbolIncluye(a, 'n')
|
|
|
|
|
|
def test_03():
|
|
""" a
|
|
/ \
|
|
b c
|
|
/ \ \
|
|
d e f
|
|
/ \
|
|
g h
|
|
tree_includes(a, "f") # -> True
|
|
"""
|
|
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 ArbolIncluye(a, 'f')
|
|
|
|
|
|
def test_04():
|
|
""" a
|
|
/ \
|
|
b c
|
|
/ \ \
|
|
d e f
|
|
/ \
|
|
g h
|
|
tree_includes(a, "p") # -> False
|
|
"""
|
|
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 ArbolIncluye(a, 'p')
|
|
|
|
|
|
def test_05():
|
|
"""
|
|
tree_includes(None, "b") # -> False
|
|
"""
|
|
return ArbolIncluye(None, 'b')
|
|
|
|
|
|
class ArbolIncluyeTestCase(unittest.TestCase):
|
|
|
|
def test_valor_existe_00(self):
|
|
self.assertTrue(test_00())
|
|
|
|
def test_valor_existe_01(self):
|
|
self.assertTrue(test_01())
|
|
|
|
def test_valor_no_existe_02(self):
|
|
self.assertFalse(test_02())
|
|
|
|
def test_valor_existe_03(self):
|
|
self.assertTrue(test_03())
|
|
|
|
def test_valor_no_existe_04(self):
|
|
self.assertFalse(test_04())
|
|
|
|
def test_valor_no_existe_05(self):
|
|
self.assertFalse(test_05())
|
|
|
|
|
|
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 ArbolIncluye2(a, "e")
|
|
|
|
|
|
def test_11():
|
|
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 ArbolIncluye2(a, "a")
|
|
|
|
|
|
def test_12():
|
|
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 ArbolIncluye2(a, 'n')
|
|
|
|
|
|
def test_13():
|
|
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 ArbolIncluye2(a, 'f')
|
|
|
|
|
|
def test_14():
|
|
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 ArbolIncluye2(a, 'p')
|
|
|
|
|
|
def test_15():
|
|
return ArbolIncluye2(None, 'b')
|
|
|
|
|
|
class ArbolIncluyeTestCase2(unittest.TestCase):
|
|
|
|
def test_valor_existe_10(self):
|
|
self.assertTrue(test_10())
|
|
|
|
def test_valor_existe_11(self):
|
|
self.assertTrue(test_11())
|
|
|
|
def test_valor_no_existe_12(self):
|
|
self.assertFalse(test_12())
|
|
|
|
def test_valor_existe_13(self):
|
|
self.assertTrue(test_13())
|
|
|
|
def test_valor_no_existe_14(self):
|
|
self.assertFalse(test_14())
|
|
|
|
def test_valor_no_existe_15(self):
|
|
self.assertFalse(test_15())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
TestRunner.main()
|
|
#unittest.main()
|
|
|