From 210e03ad6aa39f8c6434498f3c4976cc8a731afe Mon Sep 17 00:00:00 2001 From: Caiden Pyle <40413115+Caidentp@users.noreply.github.com> Date: Sun, 14 Oct 2018 15:27:10 -0500 Subject: [PATCH] Update bst.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I am new to data structures so correct me if I'm wrong! I think you have some unnecessary code in your _left_rotation and _right_rotation methods. After setting pivot.left = pivot.right, the parent is already guaranteed to be the pivot, so you don't need to update it with the if block. Right? Also, the last line of code in each method doesn't need to update the parent of the pivot because the parent does not change … I think. Unless it is for some special case maybe? I am still learning so take my suggestions with a grain of salt. Thanks! --- bst.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/bst.py b/bst.py index cf33233..d9837de 100644 --- a/bst.py +++ b/bst.py @@ -275,12 +275,10 @@ def _rotate_right(self): if self.left is not None: self.left.parent = self pivot.left = pivot.right - if pivot.left is not None: - pivot.left.parent = pivot pivot.right = self.right if pivot.right is not None: pivot.right.parent = pivot - self.right, pivot.parent = pivot, self + self.right = pivot def _rotate_left(self): """Perform a single left tree rotation.""" @@ -292,12 +290,10 @@ def _rotate_left(self): if self.right is not None: self.right.parent = self pivot.right = pivot.left - if pivot.right is not None: - pivot.right.parent = pivot pivot.left = self.left if pivot.left is not None: pivot.left.parent = pivot - self.left, pivot.parent = pivot, self + self.left = pivot def _self_balance(self): """Balance the subtree from given node."""