From d70b66898854c36fd9f4e761867ec5a0e482b632 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Fri, 8 Mar 2024 03:03:38 +0100 Subject: Add tree rotations to Expr --- mdpoly/abc.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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 -- cgit v1.2.1