Skip to content

Commit 0b7d3a5

Browse files
committed
Rotate left and right done
1 parent 3ead193 commit 0b7d3a5

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

data_structures/AVL/AVL.py

+29-6
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ def __init__(self):
4949
self.root = None
5050
self.size = 0
5151

52-
# def __init__(self, root):
53-
# self.root = root
54-
# self.size = 1
55-
5652
def insert(self, value):
5753
node = Node(value)
5854
if self.root is None:
@@ -91,13 +87,40 @@ def insert(self, value):
9187
break
9288

9389
def rebalance(self, node):
90+
if (node.getRight().getHeight() -
91+
node.getLeft.getHeight() > 1):
92+
if (node.getRight().getHeight() >
93+
node.getLeft.getHeight()):
94+
pass
95+
else:
96+
pass
97+
pass
98+
elif (node.getRight().getHeight() -
99+
node.getLeft.getHeight() > 2):
100+
if (node.getRight().getHeight() >
101+
node.getLeft.getHeight()):
102+
pass
103+
else:
104+
pass
105+
pass
94106
pass
95107

96108
def rotate_left(self, node):
97-
pass
109+
# TODO: is this pythonic enought?
110+
aux = node.getLabel()
111+
node = aux.getRight()
112+
node.setHeight(node.getHeight() - 1)
113+
node.setLeft(Node(aux))
114+
node.getLeft().setHeight(node.getHeight() + 1)
115+
node.getRight().setHeight(node.getRight().getHeight() - 1)
98116

99117
def rotate_right(self, node):
100-
pass
118+
aux = node.getLabel()
119+
node = aux.getLeft()
120+
node.setHeight(node.getHeight() - 1)
121+
node.setRight(Node(aux))
122+
node.getLeft().setHeight(node.getHeight() + 1)
123+
node.getLeft().setHeight(node.getLeft().getHeight() - 1)
101124

102125
def double_rotate_left(self, node):
103126
pass

0 commit comments

Comments
 (0)