AVL樹的python實現
阿新 • • 發佈:2018-12-25
AVL樹是一種自平衡二叉樹,他要求左右子樹的高度相差不能超過1.
python實現程式碼如下:
#coding:utf-8
class TreeNode(object):
def __init__(self):
self.data=0
self.left=None
self.right=None
self.height=0
class BTree(object):
def __init__(self):
self.root=None
def __Max(self,h1,h2):
if h1>h2:
return h1
elif h1<=h2:
return h2
def __LL(self,r):#左左情況,向右旋轉
node=r.left
r.left=node.right
node.right=r
r.height=self.__Max(self.getHeight(r.right),self.getHeight(r.left))+1
node.height=self.__Max(self.getHeight(node.right),self.getHeight(node.left))+1
return node
def __RR(self,r):#右右,左旋
node = r.right
r.right = node.left
node.left = r
r.height = self.__Max(self.getHeight(r.right), self.getHeight(r.left)) + 1
node.height = self.__Max(self.getHeight(node.right), self.getHeight(node.left)) + 1
return node
def __LR(self,r):#左右,先左旋再右旋
r.left=self.__RR(r.left)
return self.__LL(r)
def __RL(self,r):#右左,先右旋再左旋
r.right=self.__LL(r.right)
return self.__RR(r)
def __insert(self,data,r):
if r==None:
node=TreeNode()
node.data=data
return node
elif data==r.data:
return r
elif data<r.data:
r.left=self.__insert(data,r.left)
if self.getHeight(r.left)-self.getHeight(r.right)>=2:
if data<r.left.data:
r=self.__LL(r)
else:
r=self.__LR(r)
else:
r.right=self.__insert(data,r.right)
if self.getHeight(r.right)-self.getHeight(r.left)>=2:
if data>r.right.data:
r=self.__RR(r)
else:
r=self.__RL(r)
r.height=self.__Max(self.getHeight(r.left),self.getHeight(r.right))+1
return r
# 刪除data節點
def __delete(self,data,r):
if r==None:
print "don't have %d"%data
return r
elif r.data==data:
if r.left==None:#如果只有右子樹,直接將右子樹賦值到此節點
return r.right
elif r.right==None:#如果只有左子樹,直接將左子樹賦值到此節點
return r.left
else:#如果同時有左右子樹
if self.getHeight(r.left)>self.getHeight(r.right):#左子樹高度大於右子樹
#找到最右節點 返回節點值 並刪除該節點
node=r.left
while(node.right!=None):
node=node.right
r=self.__delete(node.data,r)
r.data=node.data
return r
else:#右子樹高度大於左子樹
node=r.right
while node.left!=None:
node=node.left
r=self.__delete(node.data,r)
r.data=node.data
return r
elif data<r.data:
r.left=self.__delete(data,r.left)#在左子樹中刪除
if self.getHeight(r.right)-self.getHeight(r.left)>=2:#右子樹高度與左子樹高度相差超過1
if self.getHeight(r.right.left)>self.getHeight(r.right.right):
r=self.__RL(r)
else:
r=self.__RR(r)
elif data>r.data:
r.right=self.__delete(data,r.right)#右子樹中刪除
if self.getHeight(r.left)-self.getHeight(r.right)>=2:#左子樹與右子樹高度相差超過1
if self.getHeight(r.left.right)>self.getHeight(r.left.left):
r=self.__LR(r)
else:
r=self.__LL(r)
r.height=self.__Max(self.getHeight(r.left),self.getHeight(r.right))+1
return r
#先序遍歷
def __show(self,root):
if root!=None:
print root.data
self.__show(root.left)
self.__show(root.right)
else:
return 0
def Insert(self,data):
self.root=self.__insert(data,self.root)
return self.root
def Delete(self,data):
self.root=self.__delete(data,self.root)
#求結點的高度
def getHeight(self,node):
if node==None:
return -1
#print node
return node.height
def Show(self):
self.__show(self.root)
if __name__=='__main__':
bi=BTree()
array=[5,1,2,3,4]
for i in array:
bi.Insert(i)
bi.Delete(2)
bi.Insert(2)
bi.Show()