அல்கோரிதம்avl tree

AVL — சுழற்சிகள்

TT
Testlaa Team
May 14, 20261 min read

AVL — balance factor; rotation restore.

Try this in Python

from __future__ import annotations
from dataclasses import dataclass


@dataclass
class TreeNode:
    val: int
    left: TreeNode | None = None
    right: TreeNode | None = None


def rotate_right(y: TreeNode) -> TreeNode:
    """Classic right rotation (promote y.left)."""
    x = y.left  # type: ignore
    t2 = x.right  # type: ignore
    x.right = y
    y.left = t2
    return x

# toy skew left then fix
root = TreeNode(3, TreeNode(2, TreeNode(1)))
root = rotate_right(root)
print(root.val, root.right.val if root.right else None)

Key takeaways

  • LL/RR/LR/RL paper trace.
  • Height bound.
  • Rotation trace.

Tags:

Tree structuresPythonமாணவர்கள்