-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.py
74 lines (61 loc) · 2.1 KB
/
Tree.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from Direction import Direction
from Node import Node
from Draw import Draw
class Tree:
def __init__(self) -> None:
self.__root: Node = None
self.__nodes: list[Node] = []
def show(self) -> None:
if not self.is_empty():
print("#############")
print("Arbol")
self.__print(self.__root, 0)
print("#############")
else:
print("El aŕbol está sin nodos!!!")
def is_empty(self) -> bool:
return self.__root is None
def __print(self, node: Node, level: int) -> None:
if node:
for i in range(level):
if i < (level - 1):
print(" | ", end="")
else:
print(" +- ", end="")
print(node)
level += 1
self.__print(node.get(Direction.LEFT), level)
self.__print(node.get(Direction.RIGHT), level)
def add(self, node: Node) -> None:
if not self.is_empty():
self.__root.add(node)
else:
self.__root = node
def delete(self, node: Node) -> None:
if node.have_childrens() or node.have_parent():
if self.contains(node):
self.__root.delete(node)
else:
print("No existe!!!", node)
else:
self.__root = None
def update(self, node_search: Node, value: str) -> None:
if self.contains(node_search):
node_search.set_name(value)
else:
print("No existe!!!", node_search)
def contains(self, node: Node) -> bool:
if not self.is_empty():
return self.__root.find(node)
return False
def get_nodes(self) -> list[Node]:
self.__nodes = []
self.__set_nodes(self.__root)
return self.__nodes
def get_node(self, position: int) -> Node:
return self.get_nodes()[position]
def __set_nodes(self, node: Node) -> None:
if node:
self.__nodes.append(node)
self.__set_nodes(node.get(Direction.RIGHT))
self.__set_nodes(node.get(Direction.LEFT))