From 6903d95b41206d757540f48230e245e7f28b8667 Mon Sep 17 00:00:00 2001 From: Rafael Leyva Ruiz Date: Mon, 2 Oct 2017 16:32:59 +0200 Subject: [PATCH] AVL done --- data_structures/AVL/AVL.py | 173 +++++++++++++------------------------ 1 file changed, 59 insertions(+), 114 deletions(-) diff --git a/data_structures/AVL/AVL.py b/data_structures/AVL/AVL.py index 0c34cb597..9717f1b6e 100644 --- a/data_structures/AVL/AVL.py +++ b/data_structures/AVL/AVL.py @@ -7,34 +7,42 @@ 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 + return self._right @right.setter def right(self, node): - node.parent = self - self.right = node + if node is not None: + node._parent = self + self._right = node @property def left(self): - return self.left + return self._left @left.setter def left(self, node): - node.parent = self - self.left = node + if node is not None: + node._parent = self + self._left = node @property def parent(self): - return self.parent + return self._parent @parent.setter def parent(self, node): - self.parent = node - self.height = self.parent.height + 1 + if node is not None: + self._parent = node + self.height = self.parent.height + 1 + else: + self.height = 0 class AVL: @@ -45,7 +53,7 @@ class AVL: def insert(self, value): node = Node(value) - + if self.root is None: self.root = node self.root.height = 0 @@ -65,65 +73,72 @@ class AVL: 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(dad_node) + self.rebalance(node) self.size += 1 break def rebalance(self, node): - height_right = 0 - height_left = 0 n = node + while n is not None: - height_right = 0 - height_left = 0 + height_right = n.height + height_left = n.height - if node.getRight() is not None: - height_right = node.getRight().getHeight() + if n.right is not None: + height_right = n.right.height - if node.getLeft() is not None: - height_left = node.getLeft().getHeight() + 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 = node.getRight() - if (): + left_child = n.left + 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) break else: self.double_rotate_right(n) break else: - right_child = node.right + 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.getParent() + n = n.parent 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): - aux = node.parent - node.parent = node - node.left = aux - - print(node.parent.label) - print(node.parent.right.label) - print(node.parent.left.label) + 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()) @@ -140,96 +155,26 @@ class AVL: def preShow(self, curr_node): if curr_node is not None: - self.preShow(curr_node.getLeft()) - print(curr_node.getLabel(), end=" ") - self.preShow(curr_node.getRight()) + self.preShow(curr_node.left) + print(curr_node.label, end=" ") + self.preShow(curr_node.right) - def preorden(self, curr_node): + def preorder(self, curr_node): if curr_node is not None: - self.preShow(curr_node.getLeft()) - self.preShow(curr_node.getRight()) - print(curr_node.getLabel(), end=" ") + self.preShow(curr_node.left) + self.preShow(curr_node.right) + print(curr_node.label, end=" ") def getRoot(self): return self.root 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(2) t.insert(3) +# t.preShow(t.root) +# print("\n") # t.insert(4) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") # t.insert(5) -# t.preShow(t.getRoot()) -# print("\n") -# 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()) +# t.preShow(t.root) +# t.preorden(t.root)