Update AVLtree.py

add delete function
add demo with shuffled list
add print lines to trace the addition or deletion
This commit is contained in:
Awfifcuihc 2018-11-21 03:25:07 +08:00
parent 6001215b60
commit 9f96c155be

View File

@ -3,6 +3,7 @@
An auto-balanced binary tree!
'''
import math
import random
class my_queue:
def __init__(self):
self.data = []
@ -65,6 +66,7 @@ def my_max(a,b):
def leftrotation(node):
print("left rotation node:",node.getdata())
ret = node.getleft()
node.setleft(ret.getright())
ret.setright(node)
@ -75,6 +77,7 @@ def leftrotation(node):
return ret
def rightrotation(node):
print("right rotation node:",node.getdata())
ret = node.getright()
node.setright(ret.getleft())
ret.setleft(node)
@ -113,6 +116,52 @@ def insert_node(node,data):
node.setheight(h1)
return node
def getRightMost(root):
while root.getright() is not None:
root = root.getright()
return root.getdata()
def getLeftMost(root):
while root.getleft() is not None:
root = root.getleft()
return root.getdata()
def del_node(root,data):
if root.getdata() == data:
if root.getleft() is not None and root.getright() is not None:
temp_data = getLeftMost(root.getright())
root.setdata(temp_data)
root.setright(del_node(root.getright(),temp_data))
elif root.getleft() is not None:
root = root.getleft()
else:
root = root.getright()
elif root.getdata() > data:
if root.getleft() is None:
print("No such data")
return root
else:
root.setleft(del_node(root.getleft(),data))
elif root.getdata() < data:
if root.getright() is None:
return root
else:
root.setright(del_node(root.getright(),data))
if root is None:
return root
if getheight(root.getright()) - getheight(root.getleft()) == 2:
if getheight(root.getright().getright()) > getheight(root.getright().getleft()):
root = rightrotation(root)
else:
root = lrrotation(root)
elif getheight(root.getright()) - getheight(root.getleft()) == -2:
if getheight(root.getleft().getleft()) > getheight(root.getleft().getright()):
root = leftrotation(root)
else:
root = rlrotation(root)
height = my_max(getheight(root.getright()),getheight(root.getleft())) + 1
root.setheight(height)
return root
class AVLtree:
def __init__(self):
self.root = None
@ -120,27 +169,43 @@ class AVLtree:
# print("yyy")
return getheight(self.root)
def insert(self,data):
print("insert:"+str(data))
self.root = insert_node(self.root,data)
def del_node(self,data):
print("delete:"+str(data))
if self.root is None:
print("Tree is empty!")
return
self.root = del_node(self.root,data)
def traversale(self):
q = my_queue()
q.push(self.root)
layer = self.getheight()
if layer == 0:
return
cnt = 0
while not q.isEmpty():
node = q.pop()
space = " "*int(math.pow(2,layer) - 1)
print(space,end = " ")
space = " "*int(math.pow(2,layer-1))
print(space,end = "")
if node is None:
print("*",end = " ")
print("*",end = "")
q.push(None)
q.push(None)
else:
print(node.getdata(),end = " ")
print(node.getdata(),end = "")
q.push(node.getleft())
q.push(node.getright())
print(space,end = " ")
print(space,end = "")
cnt = cnt + 1
for i in range(100):
if cnt == math.pow(2,i) - 1:
layer = layer -1
if layer == 0:
print()
print("*************************************")
return
print()
break
print()
@ -168,26 +233,57 @@ if __name__ == "__main__":
t = AVLtree()
t.traversale()
# t.insert(7)
## t.traversale()
#
# t.insert(8)
# t.traversale()
t.insert(8)
t.traversale()
t.insert(3)
t.traversale()
t.insert(6)
t.traversale()
t.insert(1)
t.traversale()
t.insert(10)
t.traversale()
t.insert(14)
t.traversale()
t.insert(13)
t.traversale()
# t.insert(3)
# t.traversale()
t.insert(4)
# t.insert(6)
# t.traversale()
# t.insert(1)
# t.traversale()
# t.insert(10)
# t.traversale()
# t.insert(14)
# t.traversale()
# t.insert(13)
# t.traversale()
## t.traversale()
# t.insert(4)
# t.traversale()
# t.insert(7)
# t.traversale()
#
#
# t.del_node(8)
# t.traversale()
# t.del_node(3)
# t.traversale()
# t.del_node(6)
# t.traversale()
# t.del_node(1)
# t.traversale()
# t.del_node(10)
# t.traversale()
# t.del_node(14)
# t.traversale()
# t.del_node(13)
# t.traversale()
## t.traversale()
# t.del_node(4)
# t.traversale()
# t.del_node(7)
# t.traversale()
l = list(range(10))
random.shuffle(l)
for i in l:
t.insert(i)
t.traversale()
t.insert(7)
random.shuffle(l)
for i in l:
t.del_node(i)
t.traversale()
@ -218,5 +314,6 @@ if __name__ == "__main__":