Python/data_structures/AVL/AVL.py

182 lines
4.9 KiB
Python
Raw Normal View History

"""
An AVL tree
"""
from __future__ import print_function
2017-07-04 16:48:27 +00:00
class Node:
def __init__(self, label):
self.label = label
2017-10-02 14:32:59 +00:00
self._parent = None
self._left = None
self._right = None
2017-07-04 16:48:27 +00:00
self.height = 0
2017-07-08 18:50:47 +00:00
@property
def right(self):
2017-10-02 14:32:59 +00:00
return self._right
2017-07-04 16:48:27 +00:00
2017-07-08 18:50:47 +00:00
@right.setter
def right(self, node):
2017-10-02 14:32:59 +00:00
if node is not None:
node._parent = self
self._right = node
2017-07-04 16:48:27 +00:00
2017-07-08 18:50:47 +00:00
@property
def left(self):
2017-10-02 14:32:59 +00:00
return self._left
2017-07-04 16:48:27 +00:00
2017-07-08 18:50:47 +00:00
@left.setter
def left(self, node):
2017-10-02 14:32:59 +00:00
if node is not None:
node._parent = self
self._left = node
2017-07-04 16:48:27 +00:00
2017-07-08 18:50:47 +00:00
@property
def parent(self):
2017-10-02 14:32:59 +00:00
return self._parent
2017-07-04 16:48:27 +00:00
2017-07-08 18:50:47 +00:00
@parent.setter
def parent(self, node):
2017-10-02 14:32:59 +00:00
if node is not None:
self._parent = node
self.height = self.parent.height + 1
else:
self.height = 0
2017-07-04 16:48:27 +00:00
class AVL:
def __init__(self):
self.root = None
self.size = 0
def insert(self, value):
node = Node(value)
2017-10-02 14:32:59 +00:00
2017-07-04 16:48:27 +00:00
if self.root is None:
self.root = node
2017-07-08 18:50:47 +00:00
self.root.height = 0
2017-07-04 16:48:27 +00:00
self.size = 1
else:
# Same as Binary Tree
dad_node = None
curr_node = self.root
while True:
if curr_node is not None:
dad_node = curr_node
2017-07-08 18:50:47 +00:00
if node.label < curr_node.label:
curr_node = curr_node.left
2017-07-04 16:48:27 +00:00
else:
2017-07-10 16:29:45 +00:00
curr_node = curr_node.right
2017-07-04 16:48:27 +00:00
else:
2017-10-02 14:32:59 +00:00
node.height = dad_node.height
dad_node.height += 1
2017-07-08 18:50:47 +00:00
if node.label < dad_node.label:
dad_node.left = node
2017-07-04 16:48:27 +00:00
else:
2017-07-08 18:50:47 +00:00
dad_node.right = node
2017-10-02 14:32:59 +00:00
self.rebalance(node)
2017-07-08 18:50:47 +00:00
self.size += 1
2017-07-04 16:48:27 +00:00
break
def rebalance(self, node):
2017-07-07 23:26:49 +00:00
n = node
2017-10-02 14:32:59 +00:00
2017-07-07 23:26:49 +00:00
while n is not None:
2017-10-02 14:32:59 +00:00
height_right = n.height
height_left = n.height
2017-07-07 23:26:49 +00:00
2017-10-02 14:32:59 +00:00
if n.right is not None:
height_right = n.right.height
2017-07-07 23:26:49 +00:00
2017-10-02 14:32:59 +00:00
if n.left is not None:
height_left = n.left.height
2017-07-07 23:26:49 +00:00
if abs(height_left - height_right) > 1:
if height_left > height_right:
2017-10-02 14:32:59 +00:00
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)
2017-10-02 14:32:59 +00:00
if (h_left > h_right):
2017-07-07 23:26:49 +00:00
self.rotate_left(n)
break
else:
self.double_rotate_right(n)
break
2017-07-07 18:06:33 +00:00
else:
2017-10-02 14:32:59 +00:00
right_child = n.right
2017-07-07 23:26:49 +00:00
if right_child is not None:
2017-07-10 16:29:45 +00:00
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)
2017-07-07 23:26:49 +00:00
if (h_left > h_right):
self.double_rotate_left(n)
break
else:
self.rotate_right(n)
break
2017-10-02 14:32:59 +00:00
n = n.parent
2017-07-04 16:48:27 +00:00
def rotate_left(self, node):
2017-10-02 14:32:59 +00:00
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
2017-07-04 16:48:27 +00:00
2017-07-10 16:29:45 +00:00
2017-10-02 14:32:59 +00:00
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
2017-07-04 16:48:27 +00:00
def double_rotate_left(self, node):
2017-07-06 19:13:56 +00:00
self.rotate_right(node.getRight().getRight())
self.rotate_left(node)
2017-07-04 16:48:27 +00:00
2017-07-06 19:13:56 +00:00
def double_rotate_right(self, node):
self.rotate_left(node.getLeft().getLeft())
self.rotate_right(node)
2017-07-07 18:06:33 +00:00
def empty(self):
if self.root is None:
return True
return False
def preShow(self, curr_node):
if curr_node is not None:
2017-10-02 14:32:59 +00:00
self.preShow(curr_node.left)
print(curr_node.label, end=" ")
self.preShow(curr_node.right)
2017-07-07 18:06:33 +00:00
2017-10-02 14:32:59 +00:00
def preorder(self, curr_node):
2017-07-07 23:26:49 +00:00
if curr_node is not None:
2017-10-02 14:32:59 +00:00
self.preShow(curr_node.left)
self.preShow(curr_node.right)
print(curr_node.label, end=" ")
2017-07-07 23:26:49 +00:00
2017-07-07 18:06:33 +00:00
def getRoot(self):
return self.root
t = AVL()
t.insert(1)
t.insert(2)
2017-07-07 23:26:49 +00:00
t.insert(3)
2017-10-02 14:32:59 +00:00
# t.preShow(t.root)
2017-07-07 23:26:49 +00:00
# print("\n")
2017-10-02 14:32:59 +00:00
# t.insert(4)
2017-07-07 23:26:49 +00:00
# t.insert(5)
2017-10-02 14:32:59 +00:00
# t.preShow(t.root)
# t.preorden(t.root)