From 0cc4a766ca79565e9e65cabe6015ee127372b4cf Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 11:20:38 +0800 Subject: [PATCH 01/15] Update avl_tree.py it's true definition of AVL tree,change left and right rotation,and add avl_tree doctest --- data_structures/binary_tree/avl_tree.py | 79 ++++++++++++++++++------- 1 file changed, 59 insertions(+), 20 deletions(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index cb043cf188b7..6e35fcca8bb2 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -1,6 +1,13 @@ """ -An auto-balanced binary tree! +This is a pure Python implementation of An auto-balanced binary tree! +For doctests run following command: +python -m doctest -v avl_tree.py +or +python3 -m doctest -v avl_tree.py +For testing run: +python avl_tree.py """ + import math import random @@ -80,7 +87,7 @@ def my_max(a, b): return b -def leftrotation(node): +def rightrotation(node): r""" A B / \ / \ @@ -89,7 +96,6 @@ def leftrotation(node): Bl Br UB Br C / UB - UB = unbalanced node """ print("left rotation node:", node.getdata()) @@ -103,7 +109,7 @@ def leftrotation(node): return ret -def rightrotation(node): +def leftrotation(node): """ a mirror symmetry rotation of the leftrotation """ @@ -118,24 +124,24 @@ def rightrotation(node): return ret -def rlrotation(node): +def lrrotation(node): r""" 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 \ / UB Bl RR = rightrotation LR = leftrotation """ - node.setleft(rightrotation(node.getleft())) - return leftrotation(node) + node.setleft(leftrotation(node.getleft())) + return rightrotation(node) -def lrrotation(node): - node.setright(leftrotation(node.getright())) - return rightrotation(node) +def rlrotation(node): + node.setright(rightrotation(node.getright())) + return leftrotation(node) def insert_node(node, data): @@ -149,16 +155,16 @@ def insert_node(node, data): if ( data < node.getleft().getdata() ): # new node is the left child of the left child - node = leftrotation(node) + node = rightrotation(node) else: - node = rlrotation(node) # new node is the right child of the left child + node = lrrotation(node) # new node is the right child of the left child else: node.setright(insert_node(node.getright(), data)) if getheight(node.getright()) - getheight(node.getleft()) == 2: if data < node.getright().getdata(): - node = lrrotation(node) + node = rlrotation(node) else: - node = rightrotation(node) + node = leftrotation(node) h1 = my_max(getheight(node.getright()), getheight(node.getleft())) + 1 node.setheight(h1) return node @@ -201,20 +207,52 @@ def del_node(root, data): return root if getheight(root.getright()) - getheight(root.getleft()) == 2: if getheight(root.getright().getright()) > getheight(root.getright().getleft()): - root = rightrotation(root) + root = leftrotation(root) else: - root = lrrotation(root) + root = rlrotation(root) elif getheight(root.getright()) - getheight(root.getleft()) == -2: if getheight(root.getleft().getleft()) > getheight(root.getleft().getright()): - root = leftrotation(root) + root = rightrotation(root) else: - root = rlrotation(root) + root = lrrotation(root) height = my_max(getheight(root.getright()), getheight(root.getleft())) + 1 root.setheight(height) return root class AVLtree: + """ + An AVL tree doctest + Examples: + >>> t = AVLtree() + >>> t.insert(4) + insert:4 + >>> t.traversale() + 4 + ************************************* + >>> t.insert(2) + insert:2 + >>> t.traversale() + 4 + 2 * + ************************************* + >>> t.insert(3) + insert:3 + right rotation node: 2 + left rotation node: 4 + >>> t.traversale() + 3 + 2 4 + ************************************* + >>> t.getheight() + 2 + >>> t.del_node(3) + delete:3 + >>> t.traversale() + 4 + 2 * + ************************************* + """ def __init__(self): self.root = None @@ -274,6 +312,8 @@ def test(self): if __name__ == "__main__": + import doctest + doctest.testmod() t = AVLtree() t.traversale() lst = list(range(10)) @@ -281,7 +321,6 @@ def test(self): for i in lst: t.insert(i) t.traversale() - random.shuffle(lst) for i in lst: t.del_node(i) From 3a3c2d12c2fa26404d4fcdab777655dbe9ba1ece Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 11:54:42 +0800 Subject: [PATCH 02/15] Update avl_tree.py --- data_structures/binary_tree/avl_tree.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index 6e35fcca8bb2..43f9e6597251 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -1,3 +1,4 @@ + """ This is a pure Python implementation of An auto-balanced binary tree! For doctests run following command: @@ -7,7 +8,6 @@ For testing run: python avl_tree.py """ - import math import random @@ -228,29 +228,29 @@ class AVLtree: >>> t.insert(4) insert:4 >>> t.traversale() - 4 + 4 ************************************* >>> t.insert(2) insert:2 >>> t.traversale() - 4 - 2 * + 4 + 2 * ************************************* >>> t.insert(3) insert:3 right rotation node: 2 left rotation node: 4 >>> t.traversale() - 3 - 2 4 + 3 + 2 4 ************************************* >>> t.getheight() 2 >>> t.del_node(3) delete:3 >>> t.traversale() - 4 - 2 * + 4 + 2 * ************************************* """ def __init__(self): From fe131e69bc13f879e40e06c44adcf2c0cc170fb9 Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 02:47:32 -0500 Subject: [PATCH 03/15] Update data_structures/binary_tree/avl_tree.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/avl_tree.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index 43f9e6597251..6f308d0a1561 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -1,4 +1,3 @@ - """ This is a pure Python implementation of An auto-balanced binary tree! For doctests run following command: From e5eb918ac562705f70ee9754faf533b6467537c2 Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 02:47:42 -0500 Subject: [PATCH 04/15] Update data_structures/binary_tree/avl_tree.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/avl_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index 6f308d0a1561..7ae2ace6ed7d 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -1,5 +1,5 @@ """ -This is a pure Python implementation of An auto-balanced binary tree! +Implementation of an auto-balanced binary tree! For doctests run following command: python -m doctest -v avl_tree.py or From 767497c2f424ecbd0dea23747f8a578b2a0b5e68 Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 02:47:56 -0500 Subject: [PATCH 05/15] Update data_structures/binary_tree/avl_tree.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/avl_tree.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index 7ae2ace6ed7d..c7d5ef5ac1ce 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -1,8 +1,6 @@ """ Implementation of an auto-balanced binary tree! For doctests run following command: -python -m doctest -v avl_tree.py -or python3 -m doctest -v avl_tree.py For testing run: python avl_tree.py From a20e42d8df29147a5d463e5fc042d01c85b66505 Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 02:48:06 -0500 Subject: [PATCH 06/15] Update data_structures/binary_tree/avl_tree.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/avl_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index c7d5ef5ac1ce..8aad03ec9073 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -84,7 +84,7 @@ def my_max(a, b): return b -def rightrotation(node): +def right_rotation(node): r""" A B / \ / \ From 0a563496f83f8a2b508707b60a08b9a49ef6ab67 Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 02:48:14 -0500 Subject: [PATCH 07/15] Update data_structures/binary_tree/avl_tree.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/avl_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index 8aad03ec9073..2eb4cf494c6e 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -106,7 +106,7 @@ def right_rotation(node): return ret -def leftrotation(node): +def left_rotation(node): """ a mirror symmetry rotation of the leftrotation """ From 89ea5682254640fb3bd988c8c071acda97420955 Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 02:49:18 -0500 Subject: [PATCH 08/15] Update data_structures/binary_tree/avl_tree.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/avl_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index 2eb4cf494c6e..af6689c24da5 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -204,7 +204,7 @@ def del_node(root, data): return root if getheight(root.getright()) - getheight(root.getleft()) == 2: if getheight(root.getright().getright()) > getheight(root.getright().getleft()): - root = leftrotation(root) + root = left_rotation(root) else: root = rlrotation(root) elif getheight(root.getright()) - getheight(root.getleft()) == -2: From 5daafcdb1890e3f1b49a2af07d15e3e7368314d5 Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 02:49:26 -0500 Subject: [PATCH 09/15] Update data_structures/binary_tree/avl_tree.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/avl_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index af6689c24da5..51aff436dcc6 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -206,7 +206,7 @@ def del_node(root, data): if getheight(root.getright().getright()) > getheight(root.getright().getleft()): root = left_rotation(root) else: - root = rlrotation(root) + root = rl_rotation(root) elif getheight(root.getright()) - getheight(root.getleft()) == -2: if getheight(root.getleft().getleft()) > getheight(root.getleft().getright()): root = rightrotation(root) From 7ab81864e22fa411cce6575a4c6b1e470cc5eec2 Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 02:49:36 -0500 Subject: [PATCH 10/15] Update data_structures/binary_tree/avl_tree.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/avl_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index 51aff436dcc6..c44b5fbe33ff 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -209,7 +209,7 @@ def del_node(root, data): root = rl_rotation(root) elif getheight(root.getright()) - getheight(root.getleft()) == -2: if getheight(root.getleft().getleft()) > getheight(root.getleft().getright()): - root = rightrotation(root) + root = right_rotation(root) else: root = lrrotation(root) height = my_max(getheight(root.getright()), getheight(root.getleft())) + 1 From 02b14a2052c7daaf4ddc5d8bc534b154c238a14b Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 02:49:43 -0500 Subject: [PATCH 11/15] Update data_structures/binary_tree/avl_tree.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/avl_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index c44b5fbe33ff..5c01f6e7a328 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -211,7 +211,7 @@ def del_node(root, data): if getheight(root.getleft().getleft()) > getheight(root.getleft().getright()): root = right_rotation(root) else: - root = lrrotation(root) + root = lr_rotation(root) height = my_max(getheight(root.getright()), getheight(root.getleft())) + 1 root.setheight(height) return root From 67af63e20449de9b80c1ca5d596ada2284192602 Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 02:57:07 -0500 Subject: [PATCH 12/15] Update data_structures/binary_tree/avl_tree.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/avl_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index 5c01f6e7a328..e2269015d8b1 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -121,7 +121,7 @@ def left_rotation(node): return ret -def lrrotation(node): +def lr_rotation(node): r""" A A Br / \ / \ / \ From 33e76e22a1afde1be2df027d55ab17691d7c905f Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 02:57:31 -0500 Subject: [PATCH 13/15] Update data_structures/binary_tree/avl_tree.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/avl_tree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index e2269015d8b1..55be4961ad76 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -132,8 +132,8 @@ def lr_rotation(node): UB Bl RR = rightrotation LR = leftrotation """ - node.setleft(leftrotation(node.getleft())) - return rightrotation(node) + node.setleft(left_rotation(node.get_left())) + return right_rotation(node) def rlrotation(node): From ed3fffbd1eee211517b44c53736c41d6bf2b5397 Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 17:15:59 +0800 Subject: [PATCH 14/15] Update avl_tree.py update some function name and update doctest --- data_structures/binary_tree/avl_tree.py | 213 ++++++++++++------------ 1 file changed, 105 insertions(+), 108 deletions(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index 55be4961ad76..75fe3ad9f64b 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -15,7 +15,7 @@ def __init__(self): self.head = 0 self.tail = 0 - def isEmpty(self): + def is_empty(self): return self.head == self.tail def push(self, data): @@ -43,39 +43,39 @@ def __init__(self, data): self.right = None self.height = 1 - def getdata(self): + def get_data(self): return self.data - def getleft(self): + def get_left(self): return self.left - def getright(self): + def get_right(self): return self.right - def getheight(self): + def get_height(self): return self.height - def setdata(self, data): + def set_data(self, data): self.data = data return - def setleft(self, node): + def set_left(self, node): self.left = node return - def setright(self, node): + def set_right(self, node): self.right = node return - def setheight(self, height): + def set_height(self, height): self.height = height return -def getheight(node): +def get_height(node): if node is None: return 0 - return node.getheight() + return node.get_height() def my_max(a, b): @@ -95,29 +95,29 @@ def right_rotation(node): UB UB = 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())) + 1 - node.setheight(h1) - h2 = my_max(getheight(ret.getright()), getheight(ret.getleft())) + 1 - ret.setheight(h2) + print("left rotation node:", node.get_data()) + ret = node.get_left() + node.set_left(ret.get_right()) + ret.set_right(node) + h1 = my_max(get_height(node.get_right()), get_height(node.get_left())) + 1 + node.set_height(h1) + h2 = my_max(get_height(ret.get_right()), get_height(ret.get_left())) + 1 + ret.set_height(h2) return ret 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()) - ret = node.getright() - node.setright(ret.getleft()) - ret.setleft(node) - h1 = my_max(getheight(node.getright()), getheight(node.getleft())) + 1 - node.setheight(h1) - h2 = my_max(getheight(ret.getright()), getheight(ret.getleft())) + 1 - ret.setheight(h2) + print("right rotation node:", node.get_data()) + ret = node.get_right() + node.set_right(ret.get_left()) + ret.set_left(node) + h1 = my_max(get_height(node.get_right()), get_height(node.get_left())) + 1 + node.set_height(h1) + h2 = my_max(get_height(ret.get_right()), get_height(ret.get_left())) + 1 + ret.set_height(h2) return ret @@ -130,90 +130,90 @@ def lr_rotation(node): Bl Br B UB Bl UB C \ / UB Bl - RR = rightrotation LR = leftrotation + RR = right_rotation LR = left_rotation """ - node.setleft(left_rotation(node.get_left())) + node.set_left(left_rotation(node.get_left())) return right_rotation(node) -def rlrotation(node): - node.setright(rightrotation(node.getright())) - return leftrotation(node) +def rl_rotation(node): + node.set_right(right_rotation(node.get_right())) + return left_rotation(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 data < node.get_data(): + node.set_left(insert_node(node.get_left(), data)) if ( - getheight(node.getleft()) - getheight(node.getright()) == 2 + get_height(node.get_left()) - get_height(node.get_right()) == 2 ): # an unbalance detected if ( - data < node.getleft().getdata() + data < node.get_left().get_data() ): # new node is the left child of the left child - node = rightrotation(node) + node = right_rotation(node) else: - node = lrrotation(node) # new node is the right child of the left child + node = lr_rotation(node) # new node is the right child of the left child else: - node.setright(insert_node(node.getright(), data)) - if getheight(node.getright()) - getheight(node.getleft()) == 2: - if data < node.getright().getdata(): - node = rlrotation(node) + node.set_right(insert_node(node.get_right(), data)) + if get_height(node.get_right()) - get_height(node.get_left()) == 2: + if data < node.get_right().get_data(): + node = rl_rotation(node) else: - node = leftrotation(node) - h1 = my_max(getheight(node.getright()), getheight(node.getleft())) + 1 - node.setheight(h1) + node = left_rotation(node) + h1 = my_max(get_height(node.get_right()), get_height(node.get_left())) + 1 + node.set_height(h1) return node -def getRightMost(root): - while root.getright() is not None: - root = root.getright() - return root.getdata() +def get_rightMost(root): + while root.get_right() is not None: + root = root.get_right() + return root.get_data() -def getLeftMost(root): - while root.getleft() is not None: - root = root.getleft() - return root.getdata() +def get_leftMost(root): + while root.get_left() is not None: + root = root.get_left() + return root.get_data() 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() + if root.get_data() == data: + if root.get_left() is not None and root.get_right() is not None: + temp_data = get_leftMost(root.get_right()) + root.set_data(temp_data) + root.set_right(del_node(root.get_right(), temp_data)) + elif root.get_left() is not None: + root = root.get_left() else: - root = root.getright() - elif root.getdata() > data: - if root.getleft() is None: + root = root.get_right() + elif root.get_data() > data: + if root.get_left() 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: + root.set_left(del_node(root.get_left(), data)) + elif root.get_data() < data: + if root.get_right() is None: return root else: - root.setright(del_node(root.getright(), data)) + root.set_right(del_node(root.get_right(), data)) if root is None: return root - if getheight(root.getright()) - getheight(root.getleft()) == 2: - if getheight(root.getright().getright()) > getheight(root.getright().getleft()): + if get_height(root.get_right()) - get_height(root.get_left()) == 2: + if get_height(root.get_right().get_right()) > get_height(root.get_right().get_left()): root = left_rotation(root) else: root = rl_rotation(root) - elif getheight(root.getright()) - getheight(root.getleft()) == -2: - if getheight(root.getleft().getleft()) > getheight(root.getleft().getright()): + elif get_height(root.get_right()) - get_height(root.get_left()) == -2: + if get_height(root.get_left().get_left()) > get_height(root.get_left().get_right()): root = right_rotation(root) else: root = lr_rotation(root) - height = my_max(getheight(root.getright()), getheight(root.getleft())) + 1 - root.setheight(height) + height = my_max(get_height(root.get_right()), get_height(root.get_left())) + 1 + root.set_height(height) return root @@ -224,12 +224,12 @@ class AVLtree: >>> t = AVLtree() >>> t.insert(4) insert:4 - >>> t.traversale() + >>> print(str(t).replace(" \\n","\\n")) 4 ************************************* >>> t.insert(2) insert:2 - >>> t.traversale() + >>> print(str(t).replace(" \\n","\\n").replace(" \\n","\\n")) 4 2 * ************************************* @@ -237,15 +237,15 @@ class AVLtree: insert:3 right rotation node: 2 left rotation node: 4 - >>> t.traversale() + >>> print(str(t).replace(" \\n","\\n").replace(" \\n","\\n")) 3 2 4 ************************************* - >>> t.getheight() + >>> t.get_height() 2 >>> t.del_node(3) delete:3 - >>> t.traversale() + >>> print(str(t).replace(" \\n","\\n").replace(" \\n","\\n")) 4 2 * ************************************* @@ -253,9 +253,9 @@ class AVLtree: def __init__(self): self.root = None - def getheight(self): + def get_height(self): # print("yyy") - return getheight(self.root) + return get_height(self.root) def insert(self, data): print("insert:" + str(data)) @@ -268,57 +268,54 @@ def del_node(self, data): return 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.push(self.root) - layer = self.getheight() + layer = self.get_height() if layer == 0: - return + return output cnt = 0 - while not q.isEmpty(): + while not q.is_empty(): node = q.pop() space = " " * int(math.pow(2, layer - 1)) - print(space, end="") + output += space if node is None: - print("*", end="") + output += "*" q.push(None) q.push(None) else: - print(node.getdata(), end="") - q.push(node.getleft()) - q.push(node.getright()) - print(space, end="") + output += str(node.get_data()) + q.push(node.get_left()) + q.push(node.get_right()) + output += space 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() + output += "\n*************************************" + return output + output += "\n" break - print() - print("*************************************") - return + output += "\n*************************************" + return output - def test(self): - getheight(None) - print("****") - self.getheight() + +def _test(): + import doctest + doctest.testmod() if __name__ == "__main__": - import doctest - doctest.testmod() - t = AVLtree() - t.traversale() + _test() + t = AVLtree() lst = list(range(10)) random.shuffle(lst) for i in lst: t.insert(i) - t.traversale() + print(str(t)) random.shuffle(lst) for i in lst: t.del_node(i) - t.traversale() + print(str(t)) From 5993542ed337bb2a49d4ae1755e595b0088f691e Mon Sep 17 00:00:00 2001 From: Markgolzh <1134386961@qq.com> Date: Mon, 22 Jun 2020 17:30:14 +0800 Subject: [PATCH 15/15] Update avl_tree.py change some code format to fit flake8 review --- data_structures/binary_tree/avl_tree.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index 75fe3ad9f64b..71dede2ccacc 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -5,6 +5,7 @@ For testing run: python avl_tree.py """ + import math import random @@ -154,7 +155,7 @@ def insert_node(node, data): ): # new node is the left child of the left child node = right_rotation(node) else: - node = lr_rotation(node) # new node is the right child of the left child + node = lr_rotation(node) else: node.set_right(insert_node(node.get_right(), data)) if get_height(node.get_right()) - get_height(node.get_left()) == 2: @@ -203,12 +204,14 @@ def del_node(root, data): if root is None: return root if get_height(root.get_right()) - get_height(root.get_left()) == 2: - if get_height(root.get_right().get_right()) > get_height(root.get_right().get_left()): + if get_height(root.get_right().get_right()) > \ + get_height(root.get_right().get_left()): root = left_rotation(root) else: root = rl_rotation(root) elif get_height(root.get_right()) - get_height(root.get_left()) == -2: - if get_height(root.get_left().get_left()) > get_height(root.get_left().get_right()): + if get_height(root.get_left().get_left()) > \ + get_height(root.get_left().get_right()): root = right_rotation(root) else: root = lr_rotation(root) @@ -304,12 +307,12 @@ def __str__(self): # a level traversale, gives a more intuitive look on the tre def _test(): import doctest - doctest.testmod() + doctest.testmod() if __name__ == "__main__": _test() - t = AVLtree() + t = AVLtree() lst = list(range(10)) random.shuffle(lst) for i in lst: