mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-19 00:37:02 +00:00
node now eses pythonproperties
This commit is contained in:
parent
d8cd33add2
commit
8e41aca1b9
|
@ -7,9 +7,6 @@ class Node:
|
||||||
|
|
||||||
def __init__(self, label):
|
def __init__(self, label):
|
||||||
self.label = label
|
self.label = label
|
||||||
self.left = None
|
|
||||||
self.rigt = None
|
|
||||||
self.parent = None
|
|
||||||
self.height = 0
|
self.height = 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -23,7 +20,7 @@ class Node:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def left(self):
|
def left(self):
|
||||||
return self.parent
|
return self.left
|
||||||
|
|
||||||
@left.setter
|
@left.setter
|
||||||
def left(self, node):
|
def left(self, node):
|
||||||
|
@ -48,6 +45,7 @@ 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.root.height = 0
|
||||||
|
@ -65,7 +63,7 @@ class AVL:
|
||||||
if node.label < curr_node.label:
|
if node.label < curr_node.label:
|
||||||
curr_node = curr_node.left
|
curr_node = curr_node.left
|
||||||
else:
|
else:
|
||||||
curr_node = curr_node.rigt
|
curr_node = curr_node.right
|
||||||
else:
|
else:
|
||||||
if node.label < dad_node.label:
|
if node.label < dad_node.label:
|
||||||
dad_node.left = node
|
dad_node.left = node
|
||||||
|
@ -92,7 +90,7 @@ class AVL:
|
||||||
|
|
||||||
if abs(height_left - height_right) > 1:
|
if abs(height_left - height_right) > 1:
|
||||||
if height_left > height_right:
|
if height_left > height_right:
|
||||||
left_child = node.getRight()
|
# left_child = node.getRight()
|
||||||
if ():
|
if ():
|
||||||
self.rotate_left(n)
|
self.rotate_left(n)
|
||||||
break
|
break
|
||||||
|
@ -100,12 +98,12 @@ class AVL:
|
||||||
self.double_rotate_right(n)
|
self.double_rotate_right(n)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
right_child = node.getRight()
|
right_child = node.right
|
||||||
if right_child is not None:
|
if right_child is not None:
|
||||||
h_right = (right_child.getRight().getHeight()
|
h_right = (right_child.right.height
|
||||||
if (right_child.getRight() is not None) else 0)
|
if (right_child.right is not None) else 0)
|
||||||
h_left = (right_child.getLeft().getHeight()
|
h_left = (right_child.left.height
|
||||||
if (right_child.getLeft() is not None) else 0)
|
if (right_child.left is not None) else 0)
|
||||||
|
|
||||||
if (h_left > h_right):
|
if (h_left > h_right):
|
||||||
self.double_rotate_left(n)
|
self.double_rotate_left(n)
|
||||||
|
@ -119,9 +117,13 @@ class AVL:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def rotate_right(self, node):
|
def rotate_right(self, node):
|
||||||
n = Node(node.getLabel())
|
aux = node.parent
|
||||||
n.setRight(node.getRight())
|
node.parent = node
|
||||||
n.setLeft(Node(node.getParent().getLabel()))
|
node.left = aux
|
||||||
|
|
||||||
|
print(node.parent.label)
|
||||||
|
print(node.parent.right.label)
|
||||||
|
print(node.parent.left.label)
|
||||||
|
|
||||||
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