Skip to content

Commit 8384cbb

Browse files
committed
finish AVL
1 parent d8a0afc commit 8384cbb

File tree

1 file changed

+56
-26
lines changed

1 file changed

+56
-26
lines changed

data_structures/AVL/AVL.py

+56-26
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def setParent(self, parent):
3939
def setHeight(self, height):
4040
self.height = height
4141

42-
def getHeight(self, height):
42+
def getHeight(self):
4343
return self.height
4444

4545

@@ -72,38 +72,36 @@ def insert(self, value):
7272
if node.getLabel() < dad_node.getLabel():
7373
dad_node.setLeft(node)
7474
dad_node.setHeight(dad_node.getHeight() + 1)
75-
76-
if (dad_node.getRight().getHeight() -
77-
dad_node.getLeft.getHeight() > 1):
78-
self.rebalance(dad_node)
79-
8075
else:
8176
dad_node.setRight(node)
8277
dad_node.setHeight(dad_node.getHeight() + 1)
83-
84-
if (dad_node.getRight().getHeight() -
85-
dad_node.getLeft.getHeight() > 1):
86-
self.rebalance(dad_node)
8778
break
8879

8980
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
81+
height_right = 0
82+
height_left = 0
83+
84+
if node.getRight() is not None:
85+
height_right = node.getRight().getHeight()
86+
87+
if node.getLeft() is not None:
88+
height_left = node.getLeft().getHeight()
89+
90+
if abs(height_left - height_right) > 1:
91+
if height_left > height_right:
92+
right_child = node.getRight()
93+
if (right_child.getLeft().getHeight() >
94+
right_child.getRight().getHeight()):
95+
self.rotate_left(node)
96+
else:
97+
self.double_rotate_right(node)
10398
else:
104-
pass
105-
pass
106-
pass
99+
left_child = node.getLeft()
100+
if (left_child.getLeft().getHeight() >
101+
left_child.getRight().getHeight()):
102+
self.double_rotate_left(node)
103+
else:
104+
self.rotate_right(node)
107105

108106
def rotate_left(self, node):
109107
# TODO: is this pythonic enought?
@@ -129,3 +127,35 @@ def double_rotate_left(self, node):
129127
def double_rotate_right(self, node):
130128
self.rotate_left(node.getLeft().getLeft())
131129
self.rotate_right(node)
130+
131+
def empty(self):
132+
if self.root is None:
133+
return True
134+
return False
135+
136+
def preShow(self, curr_node):
137+
if curr_node is not None:
138+
self.preShow(curr_node.getLeft())
139+
print(curr_node.getLabel(), end=" ")
140+
self.preShow(curr_node.getRight())
141+
142+
def getRoot(self):
143+
return self.root
144+
145+
t = AVL()
146+
t.insert(11)
147+
t.insert(14)
148+
t.insert(3)
149+
t.insert(4)
150+
t.insert(5)
151+
t.insert(6)
152+
t.insert(7)
153+
t.insert(8)
154+
t.insert(9)
155+
t.insert(10)
156+
t.insert(1)
157+
t.insert(12)
158+
t.insert(13)
159+
t.insert(2)
160+
t.insert(15)
161+
t.preShow(t.getRoot())

0 commit comments

Comments
 (0)