mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-21 08:42:03 +00:00
Update AVLtree.py
add comments
This commit is contained in:
parent
d1dba51326
commit
9bbc4d9021
|
@ -53,7 +53,6 @@ class my_node:
|
||||||
return
|
return
|
||||||
|
|
||||||
def getheight(node):
|
def getheight(node):
|
||||||
# print("xxx")
|
|
||||||
if node is None:
|
if node is None:
|
||||||
return 0
|
return 0
|
||||||
return node.getheight()
|
return node.getheight()
|
||||||
|
@ -66,6 +65,17 @@ def my_max(a,b):
|
||||||
|
|
||||||
|
|
||||||
def leftrotation(node):
|
def leftrotation(node):
|
||||||
|
'''
|
||||||
|
A B
|
||||||
|
/ \ / \
|
||||||
|
B C Bl A
|
||||||
|
/ \ --> / / \
|
||||||
|
Bl Br UB Br C
|
||||||
|
/
|
||||||
|
UB
|
||||||
|
|
||||||
|
UB = unbalanced node
|
||||||
|
'''
|
||||||
print("left rotation node:",node.getdata())
|
print("left rotation node:",node.getdata())
|
||||||
ret = node.getleft()
|
ret = node.getleft()
|
||||||
node.setleft(ret.getright())
|
node.setleft(ret.getright())
|
||||||
|
@ -77,6 +87,9 @@ def leftrotation(node):
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def rightrotation(node):
|
def rightrotation(node):
|
||||||
|
'''
|
||||||
|
a mirror symmetry rotation of the leftrotation
|
||||||
|
'''
|
||||||
print("right rotation node:",node.getdata())
|
print("right rotation node:",node.getdata())
|
||||||
ret = node.getright()
|
ret = node.getright()
|
||||||
node.setright(ret.getleft())
|
node.setright(ret.getleft())
|
||||||
|
@ -87,24 +100,35 @@ def rightrotation(node):
|
||||||
ret.setheight(h2)
|
ret.setheight(h2)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
def rlrotation(node):
|
||||||
|
'''
|
||||||
|
A A Br
|
||||||
|
/ \ / \ / \
|
||||||
|
B C RR Br C LR B A
|
||||||
|
/ \ --> / \ --> / / \
|
||||||
|
Bl Br B UB Bl UB C
|
||||||
|
\ /
|
||||||
|
UB Bl
|
||||||
|
RR = rightrotation LR = leftrotation
|
||||||
|
'''
|
||||||
|
node.setleft(rightrotation(node.getleft()))
|
||||||
|
return leftrotation(node)
|
||||||
|
|
||||||
def lrrotation(node):
|
def lrrotation(node):
|
||||||
node.setright(leftrotation(node.getright()))
|
node.setright(leftrotation(node.getright()))
|
||||||
return rightrotation(node)
|
return rightrotation(node)
|
||||||
|
|
||||||
def rlrotation(node):
|
|
||||||
node.setleft(rightrotation(node.getleft()))
|
|
||||||
return leftrotation(node)
|
|
||||||
|
|
||||||
def insert_node(node,data):
|
def insert_node(node,data):
|
||||||
if node is None:
|
if node is None:
|
||||||
return my_node(data)
|
return my_node(data)
|
||||||
if data < node.getdata():
|
if data < node.getdata():
|
||||||
node.setleft(insert_node(node.getleft(),data))
|
node.setleft(insert_node(node.getleft(),data))
|
||||||
if getheight(node.getleft()) - getheight(node.getright()) == 2:
|
if getheight(node.getleft()) - getheight(node.getright()) == 2: #an unbalance detected
|
||||||
if data < node.getleft().getdata():
|
if data < node.getleft().getdata(): #new node is the left child of the left child
|
||||||
node = leftrotation(node)
|
node = leftrotation(node)
|
||||||
else:
|
else:
|
||||||
node = rlrotation(node)
|
node = rlrotation(node) #new node is the right child of the left child
|
||||||
else:
|
else:
|
||||||
node.setright(insert_node(node.getright(),data))
|
node.setright(insert_node(node.getright(),data))
|
||||||
if getheight(node.getright()) - getheight(node.getleft()) == 2:
|
if getheight(node.getright()) - getheight(node.getleft()) == 2:
|
||||||
|
@ -178,7 +202,7 @@ class AVLtree:
|
||||||
print("Tree is empty!")
|
print("Tree is empty!")
|
||||||
return
|
return
|
||||||
self.root = del_node(self.root,data)
|
self.root = del_node(self.root,data)
|
||||||
def traversale(self):
|
def traversale(self): #a level traversale, gives a more intuitive look on the tree
|
||||||
q = my_queue()
|
q = my_queue()
|
||||||
q.push(self.root)
|
q.push(self.root)
|
||||||
layer = self.getheight()
|
layer = self.getheight()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user