||
Insert In AVL
// A utility function to left
// rotate subtree rooted with x
// See the diagram given above.
Node leftRotate(Node x)
// A utility function to right
// rotate subtree rooted with y
// See the diagram given above.
Node rightRotate (Node y)
{
Node x = y.left;
Node T2 = x.right;
// Perform rotation
x.right = y;
y.left = 12;
// Update heights
y.height
x.height
max (height(y.left),
height(y.right)) + 1;
max(height(x.left),
height (x.right)) + 1;
// Return new root
return x;
{
Node y
x.right;
Node T2 = y.left;
}
// Perform rotation
y.left = x;
x.right = T2;
// Update heights
x.height
y.height
max(height (x. left),
height (x.right)) + 1;
max(height (y.left),
height (y.right)) + 1;
// Return new root
return y;
شرج الكود التالي بالتفصيل
Assistant
بالطبع، سأشرح لك الكود المقدم بالتفصيل مع توضيح وظيفته وأقسامه.
مقدمة:
الكود هو جزء من تنفيذ شجرة AVL (شجرة بحث متوازنة ذاتيًا) يُستخدم لتدوير العقد (Nodes) من أجل الحفاظ على توازن الشجرة بعد عمليات الإضافة أو الحذف. في شجرة AVL، كل عملية إدخال أو حذف قد تسبب عدم توازن، لذا من الضروري إجراء تدويرات (Rotations) لإعادة توازنها.
وظيفة التدوير في AVL Tree:
التدوير الأيسر (Left Rotate): يُستخدم عندما تصبح شجرة الفرع الأيمن أثقل (أي ارتفاعه أكبر) من الفرع الأيسر.
التدوير الأيمن (Right Rotate): يُستخدم عندما يصبح الفرع الأيسر أثقل.