TheAlgorithms-Python/data_structures/AVL/AVL.py

243 lines
5.9 KiB
Python
Raw Normal View History

2017-07-05 00:48:27 +08:00
'''
A AVL tree
'''
class Node:
def __init__(self, label):
self.label = label
self.left = None
self.rigt = None
self.parent = None
self.height = 0
def getLabel(self):
return self.label
def setLabel(self, label):
self.label = label
def getLeft(self):
return self.left
def setLeft(self, left):
self.left = left
2017-07-08 07:26:49 +08:00
self.left.setParent(self)
self.left.setHeight(self.height + 1)
2017-07-05 00:48:27 +08:00
def getRight(self):
return self.rigt
def setRight(self, right):
self.rigt = right
2017-07-08 07:26:49 +08:00
self.rigt.setParent(self)
self.rigt.setHeight(self.height + 1)
2017-07-05 00:48:27 +08:00
def getParent(self):
return self.parent
def setParent(self, parent):
self.parent = parent
2017-07-08 07:26:49 +08:00
self.height = (self.parent.getHeight() + 1 if
(self.parent is not None) else 0)
2017-07-05 00:48:27 +08:00
def setHeight(self, height):
self.height = height
2017-07-08 02:06:33 +08:00
def getHeight(self):
2017-07-05 00:48:27 +08:00
return self.height
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
2017-07-08 07:26:49 +08:00
self.root.setHeight(0)
2017-07-05 00:48:27 +08: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
if node.getLabel() < curr_node.getLabel():
curr_node = curr_node.getLeft()
else:
curr_node = curr_node.getRight()
else:
if node.getLabel() < dad_node.getLabel():
dad_node.setLeft(node)
else:
dad_node.setRight(node)
2017-07-08 07:26:49 +08:00
self.rebalance(dad_node)
2017-07-05 00:48:27 +08:00
break
def rebalance(self, node):
2017-07-08 02:06:33 +08:00
height_right = 0
height_left = 0
2017-07-08 07:26:49 +08:00
n = node
while n is not None:
height_right = 0
height_left = 0
if node.getRight() is not None:
height_right = node.getRight().getHeight()
if node.getLeft() is not None:
height_left = node.getLeft().getHeight()
if abs(height_left - height_right) > 1:
if height_left > height_right:
left_child = node.getRight()
if ():
self.rotate_left(n)
break
else:
self.double_rotate_right(n)
break
2017-07-08 02:06:33 +08:00
else:
2017-07-08 07:26:49 +08:00
right_child = node.getRight()
if right_child is not None:
h_right = (right_child.getRight().getHeight()
if (right_child.getRight() is not None) else 0)
h_left = (right_child.getLeft().getHeight()
if (right_child.getLeft() is not None) else 0)
if (h_left > h_right):
self.double_rotate_left(n)
break
else:
self.rotate_right(n)
print(n.getLabel())
break
n = n.getParent()
2017-07-05 00:48:27 +08:00
def rotate_left(self, node):
2017-07-08 07:26:49 +08:00
pass
2017-07-05 00:48:27 +08:00
def rotate_right(self, node):
2017-07-08 07:26:49 +08:00
n = Node(node.getLabel())
n.setRight(node.getRight())
n.setLeft(Node(node.getParent().getLabel()))
node = n
2017-07-05 00:48:27 +08:00
def double_rotate_left(self, node):
2017-07-07 03:13:56 +08:00
self.rotate_right(node.getRight().getRight())
self.rotate_left(node)
2017-07-05 00:48:27 +08:00
2017-07-07 03:13:56 +08:00
def double_rotate_right(self, node):
self.rotate_left(node.getLeft().getLeft())
self.rotate_right(node)
2017-07-08 02:06:33 +08:00
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.getLeft())
print(curr_node.getLabel(), end=" ")
self.preShow(curr_node.getRight())
2017-07-08 07:26:49 +08:00
def preorden(self, curr_node):
if curr_node is not None:
self.preShow(curr_node.getLeft())
self.preShow(curr_node.getRight())
print(curr_node.getLabel(), end=" ")
2017-07-08 02:06:33 +08:00
def getRoot(self):
return self.root
t = AVL()
2017-07-08 07:26:49 +08:00
# 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())
2017-07-08 02:06:33 +08:00
t.insert(1)
t.insert(2)
2017-07-08 07:26:49 +08:00
t.insert(3)
# 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())