# -*- coding: utf-8 -*-'''An auto-balanced binary tree!'''import mathimport randomclass my_queue:def __init__(self):self.data = []self.head = 0self.tail = 0def isEmpty(self):return self.head == self.taildef push(self,data):self.data.append(data)self.tail = self.tail + 1def pop(self):ret = self.data[self.head]self.head = self.head + 1return retdef count(self):return self.tail - self.headdef print(self):print(self.data)print("**************")print(self.data[self.head:self.tail])class my_node:def __init__(self,data):self.data = dataself.left = Noneself.right = Noneself.height = 1def getdata(self):return self.datadef getleft(self):return self.leftdef getright(self):return self.rightdef getheight(self):return self.heightdef setdata(self,data):self.data = datareturndef setleft(self,node):self.left = nodereturndef setright(self,node):self.right = nodereturndef setheight(self,height):self.height = heightreturndef getheight(node):if node is None:return 0return node.getheight()def my_max(a,b):if a > b:return areturn bdef leftrotation(node):r'''A B/ \ / \B C Bl A/ \ --> / / \Bl Br UB Br C/UBUB = unbalanced node'''print("left rotation node:",node.getdata())ret = node.getleft()node.setleft(ret.getright())ret.setright(node)h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1node.setheight(h1)h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1ret.setheight(h2)return retdef rightrotation(node):'''a mirror symmetry rotation of the leftrotation'''print("right rotation node:",node.getdata())ret = node.getright()node.setright(ret.getleft())ret.setleft(node)h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1node.setheight(h1)h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1ret.setheight(h2)return retdef rlrotation(node):r'''A A Br/ \ / \ / \B C RR Br C LR B A/ \ --> / \ --> / / \Bl Br B UB Bl UB C\ /UB BlRR = rightrotation LR = leftrotation'''node.setleft(rightrotation(node.getleft()))return leftrotation(node)def lrrotation(node):node.setright(leftrotation(node.getright()))return rightrotation(node)def insert_node(node,data):if node is None:return my_node(data)if data < node.getdata():node.setleft(insert_node(node.getleft(),data))if getheight(node.getleft()) - getheight(node.getright()) == 2: #an unbalance detectedif data < node.getleft().getdata(): #new node is the left child of the left childnode = leftrotation(node)else:node = rlrotation(node) #new node is the right child of the left childelse:node.setright(insert_node(node.getright(),data))if getheight(node.getright()) - getheight(node.getleft()) == 2:if data < node.getright().getdata():node = lrrotation(node)else:node = rightrotation(node)h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1node.setheight(h1)return nodedef 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 rootelse:root.setleft(del_node(root.getleft(),data))elif root.getdata() < data:if root.getright() is None:return rootelse:root.setright(del_node(root.getright(),data))if root is None:return rootif 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())) + 1root.setheight(height)return rootclass AVLtree:def __init__(self):self.root = Nonedef getheight(self):# 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!")returnself.root = del_node(self.root,data)def traversale(self): #a level traversale, gives a more intuitive look on the treeq = my_queue()q.push(self.root)layer = self.getheight()if layer == 0:returncnt = 0while not q.isEmpty():node = q.pop()space = " "*int(math.pow(2,layer-1))print(space,end = "")if node is None:print("*",end = "")q.push(None)q.push(None)else:print(node.getdata(),end = "")q.push(node.getleft())q.push(node.getright())print(space,end = "")cnt = cnt + 1for i in range(100):if cnt == math.pow(2,i) - 1:layer = layer -1if layer == 0:print()print("*************************************")returnprint()breakprint()print("*************************************")returndef test(self):getheight(None)print("****")self.getheight()if __name__ == "__main__":t = AVLtree()t.traversale()l = list(range(10))random.shuffle(l)for i in l:t.insert(i)t.traversale()random.shuffle(l)for i in l:t.del_node(i)t.traversale()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。