AVL insertAVL(AVLType n, AVL node) { AVL root = node; List path; while(node) { path = cons(node, path); if(n < node->element) node = node->left; else if(n > node->element) node = node->right; else if(n == node->element) return root; } node = constructAVL(n); /* construct a node containing n at this null position */ /* Now we can move back up, testing heights and rotating imbalances */ while(path) { node = path->element; current->height = max(height(node->left) - height(node->right))+1; /* Where height of NULL is -1, height of leaves is 0, etc */ AVL parent; if(height(node->left) - height(node->right) == 2) { if(n < node->left->element) rotated = s_rotate_left(node); else rotated = d_rotate_right(node); parent = path->next->element if(parent->left == node) parent->left = rotated; else parent->right = rotated; } /* Then we do the same as above except where the height difference is -2 * and the rotations go right */ path = path->next; } return node; /* This will be the root and the last element of path (possibly a different root than the original due to rotations) */ }
avl_ptr d_rotate_right(avl_ptr k3) { k3->right=s_rotate_left(k3->right); return(s_rotate_right(k3)); }