This commit is contained in:
Rafael Leyva Ruiz 2017-10-02 16:32:59 +02:00
parent 8e41aca1b9
commit 6903d95b41

View File

@ -7,34 +7,42 @@ class Node:
def __init__(self, label): def __init__(self, label):
self.label = label self.label = label
self._parent = None
self._left = None
self._right = None
self.height = 0 self.height = 0
@property @property
def right(self): def right(self):
return self.right return self._right
@right.setter @right.setter
def right(self, node): def right(self, node):
node.parent = self if node is not None:
self.right = node node._parent = self
self._right = node
@property @property
def left(self): def left(self):
return self.left return self._left
@left.setter @left.setter
def left(self, node): def left(self, node):
node.parent = self if node is not None:
self.left = node node._parent = self
self._left = node
@property @property
def parent(self): def parent(self):
return self.parent return self._parent
@parent.setter @parent.setter
def parent(self, node): def parent(self, node):
self.parent = node if node is not None:
self._parent = node
self.height = self.parent.height + 1 self.height = self.parent.height + 1
else:
self.height = 0
class AVL: class AVL:
@ -65,65 +73,72 @@ class AVL:
else: else:
curr_node = curr_node.right curr_node = curr_node.right
else: else:
node.height = dad_node.height
dad_node.height += 1
if node.label < dad_node.label: if node.label < dad_node.label:
dad_node.left = node dad_node.left = node
else: else:
dad_node.right = node dad_node.right = node
self.rebalance(node)
self.rebalance(dad_node)
self.size += 1 self.size += 1
break break
def rebalance(self, node): def rebalance(self, node):
height_right = 0
height_left = 0
n = node n = node
while n is not None: while n is not None:
height_right = 0 height_right = n.height
height_left = 0 height_left = n.height
if node.getRight() is not None: if n.right is not None:
height_right = node.getRight().getHeight() height_right = n.right.height
if node.getLeft() is not None: if n.left is not None:
height_left = node.getLeft().getHeight() height_left = n.left.height
if abs(height_left - height_right) > 1: if abs(height_left - height_right) > 1:
if height_left > height_right: if height_left > height_right:
# left_child = node.getRight() left_child = n.left
if (): if left_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.rotate_left(n) self.rotate_left(n)
break break
else: else:
self.double_rotate_right(n) self.double_rotate_right(n)
break break
else: else:
right_child = node.right right_child = n.right
if right_child is not None: if right_child is not None:
h_right = (right_child.right.height h_right = (right_child.right.height
if (right_child.right is not None) else 0) if (right_child.right is not None) else 0)
h_left = (right_child.left.height h_left = (right_child.left.height
if (right_child.left is not None) else 0) if (right_child.left is not None) else 0)
if (h_left > h_right): if (h_left > h_right):
self.double_rotate_left(n) self.double_rotate_left(n)
break break
else: else:
self.rotate_right(n) self.rotate_right(n)
break break
n = n.getParent() n = n.parent
def rotate_left(self, node): def rotate_left(self, node):
pass 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): def rotate_right(self, node):
aux = node.parent aux = node.parent.label
node.parent = node node.parent.label = node.label
node.left = aux node.parent.left = Node(aux)
node.parent.left.height = node.parent.height + 1
print(node.parent.label) node.parent.right = node.right
print(node.parent.right.label)
print(node.parent.left.label)
def double_rotate_left(self, node): def double_rotate_left(self, node):
self.rotate_right(node.getRight().getRight()) self.rotate_right(node.getRight().getRight())
@ -140,96 +155,26 @@ class AVL:
def preShow(self, curr_node): def preShow(self, curr_node):
if curr_node is not None: if curr_node is not None:
self.preShow(curr_node.getLeft()) self.preShow(curr_node.left)
print(curr_node.getLabel(), end=" ") print(curr_node.label, end=" ")
self.preShow(curr_node.getRight()) self.preShow(curr_node.right)
def preorden(self, curr_node): def preorder(self, curr_node):
if curr_node is not None: if curr_node is not None:
self.preShow(curr_node.getLeft()) self.preShow(curr_node.left)
self.preShow(curr_node.getRight()) self.preShow(curr_node.right)
print(curr_node.getLabel(), end=" ") print(curr_node.label, end=" ")
def getRoot(self): def getRoot(self):
return self.root return self.root
t = AVL() t = AVL()
# t.insert(1)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())
# print("\n")
# t.insert(2)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())
# print("\n")
# t.insert(3)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())
# print("\n")
# print(t.getRoot().getHeight())
# print(t.getRoot().getRight().getHeight())
t.insert(1) t.insert(1)
t.insert(2) t.insert(2)
t.insert(3) t.insert(3)
# t.preShow(t.root)
# print("\n")
# t.insert(4) # t.insert(4)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())
# print("\n")
# t.insert(5) # t.insert(5)
# t.preShow(t.getRoot()) # t.preShow(t.root)
# print("\n") # t.preorden(t.root)
# t.preorden(t.getRoot())
# print("\n")
# t.insert(6)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())
# print("\n")
# t.insert(7)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())
# print("\n")
# t.insert(8)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())
# print("\n")
# t.insert(9)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())
# print("\n")
# t.insert(10)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())
# print("\n")
# t.insert(11)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())
# print("\n")
# t.insert(12)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())
# print("\n")
# t.insert(13)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())
# print("\n")
# t.insert(14)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())
# print("\n")
# t.insert(15)
# t.preShow(t.getRoot())
# print("\n")
# t.preorden(t.getRoot())