Merge pull request #138 from rafaelleru/master

working on #93
This commit is contained in:
Harshil 2017-10-19 09:35:45 +05:30 committed by GitHub
commit 7fe8fdc792
2 changed files with 124 additions and 75 deletions

View File

@ -7,40 +7,42 @@ class Node:
def __init__(self, label): def __init__(self, label):
self.label = label self.label = label
self.left = None self._parent = None
self.rigt = None self._left = None
self.parent = None self._right = None
self.height = 0 self.height = 0
def getLabel(self): @property
return self.label def right(self):
return self._right
def setLabel(self, label): @right.setter
self.label = label def right(self, node):
if node is not None:
node._parent = self
self._right = node
def getLeft(self): @property
return self.left def left(self):
return self._left
def setLeft(self, left): @left.setter
self.left = left def left(self, node):
if node is not None:
node._parent = self
self._left = node
def getRight(self): @property
return self.rigt def parent(self):
return self._parent
def setRight(self, right): @parent.setter
self.rigt = right def parent(self, node):
if node is not None:
def getParent(self): self._parent = node
return self.parent self.height = self.parent.height + 1
else:
def setParent(self, parent): self.height = 0
self.parent = parent
def setHeight(self, height):
self.height = height
def getHeight(self, height):
return self.height
class AVL: class AVL:
@ -51,8 +53,10 @@ class AVL:
def insert(self, value): def insert(self, value):
node = Node(value) node = Node(value)
if self.root is None: if self.root is None:
self.root = node self.root = node
self.root.height = 0
self.size = 1 self.size = 1
else: else:
# Same as Binary Tree # Same as Binary Tree
@ -64,63 +68,77 @@ class AVL:
dad_node = curr_node dad_node = curr_node
if node.getLabel() < curr_node.getLabel(): if node.label < curr_node.label:
curr_node = curr_node.getLeft() curr_node = curr_node.left
else: else:
curr_node = curr_node.getRight() curr_node = curr_node.right
else: else:
if node.getLabel() < dad_node.getLabel(): node.height = dad_node.height
dad_node.setLeft(node) dad_node.height += 1
dad_node.setHeight(dad_node.getHeight() + 1) if node.label < dad_node.label:
dad_node.left = node
if (dad_node.getRight().getHeight() -
dad_node.getLeft.getHeight() > 1):
self.rebalance(dad_node)
else: else:
dad_node.setRight(node) dad_node.right = node
dad_node.setHeight(dad_node.getHeight() + 1) self.rebalance(node)
self.size += 1
if (dad_node.getRight().getHeight() -
dad_node.getLeft.getHeight() > 1):
self.rebalance(dad_node)
break break
def rebalance(self, node): def rebalance(self, node):
if (node.getRight().getHeight() - n = node
node.getLeft.getHeight() > 1):
if (node.getRight().getHeight() > while n is not None:
node.getLeft.getHeight()): height_right = n.height
pass height_left = n.height
else:
pass if n.right is not None:
pass height_right = n.right.height
elif (node.getRight().getHeight() -
node.getLeft.getHeight() > 2): if n.left is not None:
if (node.getRight().getHeight() > height_left = n.left.height
node.getLeft.getHeight()):
pass if abs(height_left - height_right) > 1:
else: if height_left > height_right:
pass left_child = n.left
pass if left_child is not None:
pass 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 = 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): def rotate_left(self, node):
# TODO: is this pythonic enought? aux = node.parent.label
aux = node.getLabel() node.parent.label = node.label
node = aux.getRight() node.parent.right = Node(aux)
node.setHeight(node.getHeight() - 1) node.parent.right.height = node.parent.height + 1
node.setLeft(Node(aux)) node.parent.left = node.right
node.getLeft().setHeight(node.getHeight() + 1)
node.getRight().setHeight(node.getRight().getHeight() - 1)
def rotate_right(self, node): def rotate_right(self, node):
aux = node.getLabel() aux = node.parent.label
node = aux.getLeft() node.parent.label = node.label
node.setHeight(node.getHeight() - 1) node.parent.left = Node(aux)
node.setRight(Node(aux)) node.parent.left.height = node.parent.height + 1
node.getLeft().setHeight(node.getHeight() + 1) node.parent.right = node.right
node.getLeft().setHeight(node.getLeft().getHeight() - 1)
def double_rotate_left(self, node): def double_rotate_left(self, node):
self.rotate_right(node.getRight().getRight()) self.rotate_right(node.getRight().getRight())
@ -129,3 +147,34 @@ class AVL:
def double_rotate_right(self, node): def double_rotate_right(self, node):
self.rotate_left(node.getLeft().getLeft()) self.rotate_left(node.getLeft().getLeft())
self.rotate_right(node) self.rotate_right(node)
def empty(self):
if self.root is None:
return True
return False
def preShow(self, curr_node):
if curr_node is not None:
self.preShow(curr_node.left)
print(curr_node.label, end=" ")
self.preShow(curr_node.right)
def preorder(self, curr_node):
if curr_node is not None:
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.insert(2)
t.insert(3)
# t.preShow(t.root)
# print("\n")
# t.insert(4)
# t.insert(5)
# t.preShow(t.root)
# t.preorden(t.root)

View File

@ -68,7 +68,7 @@ class BinarySearchTree:
return False return False
def preShow(self, curr_node): def preShow(self, curr_node):
if curr_node is None: if curr_node is not None:
print(curr_node.getLabel(), end=" ") print(curr_node.getLabel(), end=" ")
self.preShow(curr_node.getLeft()) self.preShow(curr_node.getLeft())