Single Right rotation (compare to RotateWithLeftChild page 148 text) void rotateRight(AVLNode *& curr) { AVLNode * kid = curr->left; curr->left = kid->right;

Presentation on theme: "Single Right rotation (compare to RotateWithLeftChild page 148 text) void rotateRight(AVLNode *& curr) { AVLNode * kid = curr->left; curr->left = kid->right;"— Presentation transcript:

Single Right rotation (compare to RotateWithLeftChild page 148 text) void rotateRight(AVLNode *& curr) { AVLNode * kid = curr->left; curr->left = kid->right; kid->right = curr; curr->height = max(height(curr->left), height(curr->right)) + 1; kid>height = max(height(kid->left), height(kid->right)) + 1 curr = kid; }

Why did we need to pass the root by *& ? If you want the value of an int to change, you pass it by reference. If you want the value of a POINTER to change, you pass the pointer by reference. You can pass the address of an int to a routine and change the value of the int. But passing an address DOES NOT allow you to change the address itself, unless you pass it by reference.

Pass by value doit(int x) {x = 10; cout << “doit x” << x; } main() { int x; x = 5; doit(x); cout << “main x “ << x; } x x 5 5 10 0x100 0x200

Pass by address doit(int *x) {*x = 10; cout << “doit x” << *x; } main() { int x; x = 5; doit( &x); cout << “main x “ << x; } x x 5 10 0x100 0x200

Pass by reference doit(int &x) { x = 10; cout << “doit x” << x; } main() { int x; x = 5; doit( x); cout << “main x “ << x; } x x 5 10 0x100 0x200 Similar to pass by address, but the compiler does all the work for you.

Pass by value with pointers doit(int *x) {x = new int(); *x = 27 cout << “doit x” << x << *x; } main() { int * x = NULL; doit(x); cout << “main x “ << x <<*x; } x x NULL NULL 0x300 0x100 0x200 0x300 27

Pass by address with pointers doit(int **x) {*x = new int(); **x = 27 cout << “doit x” << *x << **x; } main() { int * x = NULL; doit(&x); cout << “main x “ << x <<*x; } x x NULL 0x300 0x100 0x200 0x300 27

Pass by pointer reference doit(int * &x) { x = new int(); *x = 27 cout << “doit x” << x << *x; } main() { int * x = NULL; doit( x); cout << “main x “ << x <<*x; } x x NULL 0x300 0x100 0x200 0x300 27

Assigning pointers – simply copies contents (addresses) int * t = new int (); *t = 15; int * s = t; int ** addr = &t; *addr = new int(); t s 0x200 0x300 0x400 0x500 15 0x400 0x700 addr 0x200 0x500

So what about rotateRight? void rotateRight(AVLNode *& curr) { AVLNode * kid = curr->left; curr->left = kid->right; kid->right = curr; curr->height = max(height(curr->left), height(curr->right)) + 1; kid>height = max(height(kid->left), height(kid- >right)) + 1 curr = kid; } 0x100 25 5 10 5015 0x400 0x320 0x2000x80 0x200 0x320 0x400 000 0 00 curr 0x100 When the calling routine passes in root->left, the compiler records the ADDRESS of the root’s left pointer (which is 0x100 in our case). kid0x320 curr

So what about rotateRight? void rotateRight(AVLNode *& curr) { AVLNode * kid = curr->left; curr->left = kid->right; kid->right = curr; curr->height = max(height(curr->left), height(curr->right)) + 1; kid>height = max(height(kid->left), height(kid- >right)) + 1 curr = kid; } 0x100 25 5 10 5015 0x400 0x320 0x2000x80 0x320 0 0x400 000 0x200 00 curr 0x100 When the calling routine passes in root->left, the compiler records the ADDRESS of the root’s left pointer (which is 0x100 in our case). kid0x320 curr

WARNING: If the calling routine passes in a COPY of the address (say temp pointed to 0x200), only the COPY is changed. In our case, the only pointer to node 15 is changed.

Download ppt "Single Right rotation (compare to RotateWithLeftChild page 148 text) void rotateRight(AVLNode *& curr) { AVLNode * kid = curr->left; curr->left = kid->right;"

Similar presentations