Update avl_tree.py (#2145)

* Update avl_tree.py

it's true definition of AVL tree,change  left and right rotation,and add avl_tree doctest

* Update avl_tree.py

* Update data_structures/binary_tree/avl_tree.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/binary_tree/avl_tree.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/binary_tree/avl_tree.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/binary_tree/avl_tree.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/binary_tree/avl_tree.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/binary_tree/avl_tree.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/binary_tree/avl_tree.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/binary_tree/avl_tree.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/binary_tree/avl_tree.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/binary_tree/avl_tree.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/binary_tree/avl_tree.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update avl_tree.py

update some function name and update doctest

* Update avl_tree.py

change some code format to fit flake8 review

Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
Markgolzh 2020-06-25 02:55:13 -05:00 committed by GitHub
parent b0c3c0fbf6
commit c7ca9cf0df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,11 @@
""" """
An auto-balanced binary tree! Implementation of an auto-balanced binary tree!
For doctests run following command:
python3 -m doctest -v avl_tree.py
For testing run:
python avl_tree.py
""" """
import math import math
import random import random
@ -11,7 +16,7 @@ class my_queue:
self.head = 0 self.head = 0
self.tail = 0 self.tail = 0
def isEmpty(self): def is_empty(self):
return self.head == self.tail return self.head == self.tail
def push(self, data): def push(self, data):
@ -39,39 +44,39 @@ class my_node:
self.right = None self.right = None
self.height = 1 self.height = 1
def getdata(self): def get_data(self):
return self.data return self.data
def getleft(self): def get_left(self):
return self.left return self.left
def getright(self): def get_right(self):
return self.right return self.right
def getheight(self): def get_height(self):
return self.height return self.height
def setdata(self, data): def set_data(self, data):
self.data = data self.data = data
return return
def setleft(self, node): def set_left(self, node):
self.left = node self.left = node
return return
def setright(self, node): def set_right(self, node):
self.right = node self.right = node
return return
def setheight(self, height): def set_height(self, height):
self.height = height self.height = height
return return
def getheight(node): def get_height(node):
if node is None: if node is None:
return 0 return 0
return node.getheight() return node.get_height()
def my_max(a, b): def my_max(a, b):
@ -80,7 +85,7 @@ def my_max(a, b):
return b return b
def leftrotation(node): def right_rotation(node):
r""" r"""
A B A B
/ \ / \ / \ / \
@ -89,138 +94,171 @@ def leftrotation(node):
Bl Br UB Br C Bl Br UB Br C
/ /
UB UB
UB = unbalanced node UB = unbalanced node
""" """
print("left rotation node:", node.getdata()) print("left rotation node:", node.get_data())
ret = node.getleft() ret = node.get_left()
node.setleft(ret.getright()) node.set_left(ret.get_right())
ret.setright(node) ret.set_right(node)
h1 = my_max(getheight(node.getright()), getheight(node.getleft())) + 1 h1 = my_max(get_height(node.get_right()), get_height(node.get_left())) + 1
node.setheight(h1) node.set_height(h1)
h2 = my_max(getheight(ret.getright()), getheight(ret.getleft())) + 1 h2 = my_max(get_height(ret.get_right()), get_height(ret.get_left())) + 1
ret.setheight(h2) ret.set_height(h2)
return ret return ret
def rightrotation(node): def left_rotation(node):
""" """
a mirror symmetry rotation of the leftrotation a mirror symmetry rotation of the left_rotation
""" """
print("right rotation node:", node.getdata()) print("right rotation node:", node.get_data())
ret = node.getright() ret = node.get_right()
node.setright(ret.getleft()) node.set_right(ret.get_left())
ret.setleft(node) ret.set_left(node)
h1 = my_max(getheight(node.getright()), getheight(node.getleft())) + 1 h1 = my_max(get_height(node.get_right()), get_height(node.get_left())) + 1
node.setheight(h1) node.set_height(h1)
h2 = my_max(getheight(ret.getright()), getheight(ret.getleft())) + 1 h2 = my_max(get_height(ret.get_right()), get_height(ret.get_left())) + 1
ret.setheight(h2) ret.set_height(h2)
return ret return ret
def rlrotation(node): def lr_rotation(node):
r""" r"""
A A Br A A Br
/ \ / \ / \ / \ / \ / \
B C RR Br C LR B A B C LR Br C RR B A
/ \ --> / \ --> / / \ / \ --> / \ --> / / \
Bl Br B UB Bl UB C Bl Br B UB Bl UB C
\ / \ /
UB Bl UB Bl
RR = rightrotation LR = leftrotation RR = right_rotation LR = left_rotation
""" """
node.setleft(rightrotation(node.getleft())) node.set_left(left_rotation(node.get_left()))
return leftrotation(node) return right_rotation(node)
def lrrotation(node): def rl_rotation(node):
node.setright(leftrotation(node.getright())) node.set_right(right_rotation(node.get_right()))
return rightrotation(node) return left_rotation(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.get_data():
node.setleft(insert_node(node.getleft(), data)) node.set_left(insert_node(node.get_left(), data))
if ( if (
getheight(node.getleft()) - getheight(node.getright()) == 2 get_height(node.get_left()) - get_height(node.get_right()) == 2
): # an unbalance detected ): # an unbalance detected
if ( if (
data < node.getleft().getdata() data < node.get_left().get_data()
): # new node is the left child of the left child ): # new node is the left child of the left child
node = leftrotation(node) node = right_rotation(node)
else: else:
node = rlrotation(node) # new node is the right child of the left child node = lr_rotation(node)
else: else:
node.setright(insert_node(node.getright(), data)) node.set_right(insert_node(node.get_right(), data))
if getheight(node.getright()) - getheight(node.getleft()) == 2: if get_height(node.get_right()) - get_height(node.get_left()) == 2:
if data < node.getright().getdata(): if data < node.get_right().get_data():
node = lrrotation(node) node = rl_rotation(node)
else: else:
node = rightrotation(node) node = left_rotation(node)
h1 = my_max(getheight(node.getright()), getheight(node.getleft())) + 1 h1 = my_max(get_height(node.get_right()), get_height(node.get_left())) + 1
node.setheight(h1) node.set_height(h1)
return node return node
def getRightMost(root): def get_rightMost(root):
while root.getright() is not None: while root.get_right() is not None:
root = root.getright() root = root.get_right()
return root.getdata() return root.get_data()
def getLeftMost(root): def get_leftMost(root):
while root.getleft() is not None: while root.get_left() is not None:
root = root.getleft() root = root.get_left()
return root.getdata() return root.get_data()
def del_node(root, data): def del_node(root, data):
if root.getdata() == data: if root.get_data() == data:
if root.getleft() is not None and root.getright() is not None: if root.get_left() is not None and root.get_right() is not None:
temp_data = getLeftMost(root.getright()) temp_data = get_leftMost(root.get_right())
root.setdata(temp_data) root.set_data(temp_data)
root.setright(del_node(root.getright(), temp_data)) root.set_right(del_node(root.get_right(), temp_data))
elif root.getleft() is not None: elif root.get_left() is not None:
root = root.getleft() root = root.get_left()
else: else:
root = root.getright() root = root.get_right()
elif root.getdata() > data: elif root.get_data() > data:
if root.getleft() is None: if root.get_left() is None:
print("No such data") print("No such data")
return root return root
else: else:
root.setleft(del_node(root.getleft(), data)) root.set_left(del_node(root.get_left(), data))
elif root.getdata() < data: elif root.get_data() < data:
if root.getright() is None: if root.get_right() is None:
return root return root
else: else:
root.setright(del_node(root.getright(), data)) root.set_right(del_node(root.get_right(), data))
if root is None: if root is None:
return root return root
if getheight(root.getright()) - getheight(root.getleft()) == 2: if get_height(root.get_right()) - get_height(root.get_left()) == 2:
if getheight(root.getright().getright()) > getheight(root.getright().getleft()): if get_height(root.get_right().get_right()) > \
root = rightrotation(root) get_height(root.get_right().get_left()):
root = left_rotation(root)
else: else:
root = lrrotation(root) root = rl_rotation(root)
elif getheight(root.getright()) - getheight(root.getleft()) == -2: elif get_height(root.get_right()) - get_height(root.get_left()) == -2:
if getheight(root.getleft().getleft()) > getheight(root.getleft().getright()): if get_height(root.get_left().get_left()) > \
root = leftrotation(root) get_height(root.get_left().get_right()):
root = right_rotation(root)
else: else:
root = rlrotation(root) root = lr_rotation(root)
height = my_max(getheight(root.getright()), getheight(root.getleft())) + 1 height = my_max(get_height(root.get_right()), get_height(root.get_left())) + 1
root.setheight(height) root.set_height(height)
return root return root
class AVLtree: class AVLtree:
"""
An AVL tree doctest
Examples:
>>> t = AVLtree()
>>> t.insert(4)
insert:4
>>> print(str(t).replace(" \\n","\\n"))
4
*************************************
>>> t.insert(2)
insert:2
>>> print(str(t).replace(" \\n","\\n").replace(" \\n","\\n"))
4
2 *
*************************************
>>> t.insert(3)
insert:3
right rotation node: 2
left rotation node: 4
>>> print(str(t).replace(" \\n","\\n").replace(" \\n","\\n"))
3
2 4
*************************************
>>> t.get_height()
2
>>> t.del_node(3)
delete:3
>>> print(str(t).replace(" \\n","\\n").replace(" \\n","\\n"))
4
2 *
*************************************
"""
def __init__(self): def __init__(self):
self.root = None self.root = None
def getheight(self): def get_height(self):
# print("yyy") # print("yyy")
return getheight(self.root) return get_height(self.root)
def insert(self, data): def insert(self, data):
print("insert:" + str(data)) print("insert:" + str(data))
@ -233,56 +271,54 @@ class AVLtree:
return return
self.root = del_node(self.root, data) self.root = del_node(self.root, data)
def traversale(self): # a level traversale, gives a more intuitive look on the tree def __str__(self): # a level traversale, gives a more intuitive look on the tree
output = ""
q = my_queue() q = my_queue()
q.push(self.root) q.push(self.root)
layer = self.getheight() layer = self.get_height()
if layer == 0: if layer == 0:
return return output
cnt = 0 cnt = 0
while not q.isEmpty(): while not q.is_empty():
node = q.pop() node = q.pop()
space = " " * int(math.pow(2, layer - 1)) space = " " * int(math.pow(2, layer - 1))
print(space, end="") output += space
if node is None: if node is None:
print("*", end="") output += "*"
q.push(None) q.push(None)
q.push(None) q.push(None)
else: else:
print(node.getdata(), end="") output += str(node.get_data())
q.push(node.getleft()) q.push(node.get_left())
q.push(node.getright()) q.push(node.get_right())
print(space, end="") output += space
cnt = cnt + 1 cnt = cnt + 1
for i in range(100): for i in range(100):
if cnt == math.pow(2, i) - 1: if cnt == math.pow(2, i) - 1:
layer = layer - 1 layer = layer - 1
if layer == 0: if layer == 0:
print() output += "\n*************************************"
print("*************************************") return output
return output += "\n"
print()
break break
print() output += "\n*************************************"
print("*************************************") return output
return
def test(self):
getheight(None) def _test():
print("****") import doctest
self.getheight() doctest.testmod()
if __name__ == "__main__": if __name__ == "__main__":
_test()
t = AVLtree() t = AVLtree()
t.traversale()
lst = list(range(10)) lst = list(range(10))
random.shuffle(lst) random.shuffle(lst)
for i in lst: for i in lst:
t.insert(i) t.insert(i)
t.traversale() print(str(t))
random.shuffle(lst) random.shuffle(lst)
for i in lst: for i in lst:
t.del_node(i) t.del_node(i)
t.traversale() print(str(t))