aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2024-03-08 03:03:38 +0100
committerNao Pross <np@0hm.ch>2024-03-08 03:03:51 +0100
commitd70b66898854c36fd9f4e761867ec5a0e482b632 (patch)
tree6341b52f0699e6172f87e994e799881102483e41
parentFix missing edge case in Expr.children (diff)
downloadmdpoly-d70b66898854c36fd9f4e761867ec5a0e482b632.tar.gz
mdpoly-d70b66898854c36fd9f4e761867ec5a0e482b632.zip
Add tree rotations to Expr
-rw-r--r--mdpoly/abc.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/mdpoly/abc.py b/mdpoly/abc.py
index 14026d6..0f51bbc 100644
--- a/mdpoly/abc.py
+++ b/mdpoly/abc.py
@@ -171,6 +171,27 @@ class Expr(ABC):
return replace_all(self)
+ def rotate_left(self) -> Expr:
+ """ Perform a left tree rotation. """
+ root = copy(self) # current root
+ pivot = copy(self.right) # new parent
+
+ root.right = pivot.left
+ pivot.left = root
+
+ return pivot
+
+ def rotate_right(self) -> Expr:
+ """ Perform a right tree rotation. """
+ root = copy(self) # current root
+ pivot = copy(root.left) # new parent
+
+ root.left = pivot.right
+ pivot.right = root
+
+ return pivot
+
+
# --- Private methods ---
@staticmethod