Skip to content

Commit 34d2246

Browse files
Refactor zig-zag rotation cases in splay tree
Refactor zig-zag case handling in splay tree rotation logic.
1 parent 7696cbe commit 34d2246

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

data_structures/binary_tree/splay_tree.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,14 @@ def _splay(self, node: SplayNode) -> None:
177177
else:
178178
self._rotate_left(grandparent)
179179
self._rotate_left(parent)
180+
elif node is parent.left:
181+
# Zig-zag case: opposite directions (left child)
182+
self._rotate_right(parent)
183+
self._rotate_left(grandparent)
180184
else:
181-
# Zig-zag case: opposite directions
182-
if node is parent.left:
183-
self._rotate_right(parent)
184-
self._rotate_left(grandparent)
185-
else:
186-
self._rotate_left(parent)
187-
self._rotate_right(grandparent)
185+
# Zig-zag case: opposite directions (right child)
186+
self._rotate_left(parent)
187+
self._rotate_right(grandparent)
188188

189189
def _find_node(self, value: Any) -> SplayNode | None:
190190
"""Find a node with the given value, splaying it to root if found."""

0 commit comments

Comments
 (0)