-
Notifications
You must be signed in to change notification settings - Fork 1
/
02. arbol_avl.py
210 lines (176 loc) · 6.27 KB
/
02. arbol_avl.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
class Node:
def __init__(self, label):
self.label = label
self._parent = None
self._left = None
self._right = None
self.height = 0
@property
def right(self):
return self._right
@right.setter
def right(self, node):
if node is not None:
node._parent = self
self._right = node
@property
def left(self):
return self._left
@left.setter
def left(self, node):
if node is not None:
node._parent = self
self._left = node
@property
def parent(self):
return self._parent
@parent.setter
def parent(self, node):
if node is not None:
self._parent = node
self.height = self.parent.height + 1
else:
self.height = 0
class AVL:
def __init__(self):
self.root = None
self.size = 0
def insert(self, value):
node = Node(value)
if self.root is None:
self.root = node
self.root.height = 0
self.size = 1
else:
dad_node = None
curr_node = self.root
while True:
if curr_node is not None:
dad_node = curr_node
if node.label < curr_node.label:
curr_node = curr_node.left
else:
curr_node = curr_node.right
else:
node.height = dad_node.height
dad_node.height += 1
if node.label < dad_node.label:
dad_node.left = node
else:
dad_node.right = node
self.rebalance(node)
self.size += 1
break
# Operación de rotación
def rebalance(self, node):
n = node
while n is not None:
height_right = n.height
height_left = n.height
if n.right is not None:
height_right = n.right.height
if n.left is not None:
height_left = n.left.height
if abs(height_left - height_right) > 1:
if height_left > height_right:
left_child = n.left
if left_child is not None:
h_right = (left_child.right.height
if (left_child.right is not None) else 0)
h_left = (left_child.left.height
if (left_child.left is not None) else 0)
if (h_left > h_right):
self.rotate_left(n)
break
else:
self.double_rotate_right(n)
break
else:
right_child = n.right
if right_child is not None:
h_right = (right_child.right.height
if (right_child.right is not None) else 0)
h_left = (right_child.left.height
if (right_child.left is not None) else 0)
if (h_left > h_right):
self.double_rotate_left(n)
break
else:
self.rotate_right(n)
break
n = n.parent
def rotate_left(self, node):
aux = node.parent.label
node.parent.label = node.label
node.parent.right = Node(aux)
node.parent.right.height = node.parent.height + 1
node.parent.left = node.right
def rotate_right(self, node):
aux = node.parent.label
node.parent.label = node.label
node.parent.left = Node(aux)
node.parent.left.height = node.parent.height + 1
node.parent.right = node.right
def double_rotate_left(self, node):
self.rotate_right(node.getRight().getRight())
self.rotate_left(node)
def double_rotate_right(self, node):
self.rotate_left(node.getLeft().getLeft())
self.rotate_right(node)
def empty(self):
if self.root is None:
return True
return False
def in_order(self, curr_node):
if curr_node is not None:
self.in_order(curr_node.left)
print(curr_node.label, end=" ")
self.in_order(curr_node.right)
def pre_order(self, curr_node):
if curr_node is not None:
print(curr_node.label, end=" ")
self.in_order(curr_node.left)
self.in_order(curr_node.right)
def pos_order(self, curr_node):
if curr_node is not None:
self.in_order(curr_node.left)
self.in_order(curr_node.right)
print(curr_node.label, end=" ")
def get_root(self):
return self.root
if __name__ == "__main__":
def menu():
opc = 0
arbol_avl = AVL()
try:
while opc != 7:
print("\nÁrbol AVL\n"
+ "\n\t1. Agregar Nodos"
+ "\n\t2. ¿Esta vacío el árbol?"
+ "\n\t3. Recorrer en orden"
+ "\n\t4. Recorrer en post-orden"
+ "\n\t5. Recorrer en pre-orden"
+ "\n\t6. Obtener raiz"
+ "\n\t7. Salir")
opc = int(input("Ingrese su opción "))
if opc == 1:
dato = int(input("Ingrese el dato a anexar al arbol "))
arbol_avl.insert(dato)
elif opc == 2:
print("SI" if arbol_avl.empty() else "NO")
elif opc == 3:
arbol_avl.in_order(arbol_avl.root)
elif opc == 4:
arbol_avl.pos_order(arbol_avl.root)
elif opc == 5:
arbol_avl.pre_order(arbol_avl.root)
elif opc == 6:
print(arbol_avl.get_root().label)
elif opc == 7:
print("Adios")
else:
print("Ingresaste una opción errónea")
except Exception as e:
print("\n Solo se permiten caracteres numéricos")
print(e)
menu()