mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 23:11:09 +00:00
using python properties
This commit is contained in:
parent
67f23f59b1
commit
d8cd33add2
|
@ -12,41 +12,32 @@ class Node:
|
||||||
self.parent = None
|
self.parent = 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):
|
||||||
|
node.parent = self
|
||||||
|
self.right = node
|
||||||
|
|
||||||
def getLeft(self):
|
@property
|
||||||
return self.left
|
def left(self):
|
||||||
|
|
||||||
def setLeft(self, left):
|
|
||||||
self.left = left
|
|
||||||
self.left.setParent(self)
|
|
||||||
self.left.setHeight(self.height + 1)
|
|
||||||
|
|
||||||
def getRight(self):
|
|
||||||
return self.rigt
|
|
||||||
|
|
||||||
def setRight(self, right):
|
|
||||||
self.rigt = right
|
|
||||||
self.rigt.setParent(self)
|
|
||||||
self.rigt.setHeight(self.height + 1)
|
|
||||||
|
|
||||||
def getParent(self):
|
|
||||||
return self.parent
|
return self.parent
|
||||||
|
|
||||||
def setParent(self, parent):
|
@left.setter
|
||||||
self.parent = parent
|
def left(self, node):
|
||||||
self.height = (self.parent.getHeight() + 1 if
|
node.parent = self
|
||||||
(self.parent is not None) else 0)
|
self.left = node
|
||||||
|
|
||||||
def setHeight(self, height):
|
@property
|
||||||
self.height = height
|
def parent(self):
|
||||||
|
return self.parent
|
||||||
|
|
||||||
def getHeight(self):
|
@parent.setter
|
||||||
return self.height
|
def parent(self, node):
|
||||||
|
self.parent = node
|
||||||
|
self.height = self.parent.height + 1
|
||||||
|
|
||||||
|
|
||||||
class AVL:
|
class AVL:
|
||||||
|
@ -59,7 +50,7 @@ class AVL:
|
||||||
node = Node(value)
|
node = Node(value)
|
||||||
if self.root is None:
|
if self.root is None:
|
||||||
self.root = node
|
self.root = node
|
||||||
self.root.setHeight(0)
|
self.root.height = 0
|
||||||
self.size = 1
|
self.size = 1
|
||||||
else:
|
else:
|
||||||
# Same as Binary Tree
|
# Same as Binary Tree
|
||||||
|
@ -71,16 +62,18 @@ 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.rigt
|
||||||
else:
|
else:
|
||||||
if node.getLabel() < dad_node.getLabel():
|
if node.label < dad_node.label:
|
||||||
dad_node.setLeft(node)
|
dad_node.left = node
|
||||||
else:
|
else:
|
||||||
dad_node.setRight(node)
|
dad_node.right = node
|
||||||
|
|
||||||
self.rebalance(dad_node)
|
self.rebalance(dad_node)
|
||||||
|
self.size += 1
|
||||||
break
|
break
|
||||||
|
|
||||||
def rebalance(self, node):
|
def rebalance(self, node):
|
||||||
|
@ -119,7 +112,6 @@ class AVL:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.rotate_right(n)
|
self.rotate_right(n)
|
||||||
print(n.getLabel())
|
|
||||||
break
|
break
|
||||||
n = n.getParent()
|
n = n.getParent()
|
||||||
|
|
||||||
|
@ -130,7 +122,6 @@ class AVL:
|
||||||
n = Node(node.getLabel())
|
n = Node(node.getLabel())
|
||||||
n.setRight(node.getRight())
|
n.setRight(node.getRight())
|
||||||
n.setLeft(Node(node.getParent().getLabel()))
|
n.setLeft(Node(node.getParent().getLabel()))
|
||||||
node = n
|
|
||||||
|
|
||||||
def double_rotate_left(self, node):
|
def double_rotate_left(self, node):
|
||||||
self.rotate_right(node.getRight().getRight())
|
self.rotate_right(node.getRight().getRight())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user