120 likes | 236 Views
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;
E N D
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; } 5 10 x 5 x 0x200 0x100
Pass by address doit(int*x) {*x = 10; cout << “doit x” << *x; } main() { int x; x = 5; doit( &x); cout << “main x “ << x; } 0x100 x 5 10 x 0x200 0x100
Pass by reference doit(int&x) {x = 10; cout << “doit x” << x; } main() { int x; x = 5; doit( x); cout << “main x “ << x; } 0x100 x 5 10 x 0x200 0x100 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; } NULL 0x300 x NULL x 0x200 0x100 27 0x300
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; } 0x100 x NULL0x300 x 0x200 0x100 27 0x300
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; } 0x100 x NULL 0x300 x 0x200 0x100 27 0x300
Assigning pointers – simply copies contents (addresses) int * t = new int (); *t = 15; int * s = t; int ** addr = &t; *addr = new int(); 0x200 t 0x400 0x500 0x300 0x400 s 0x400 15 0x700 0x200 addr 0x500
So what about rotateRight? 0x100 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; } 0x200 25 0x80 curr 0x200 0x80 0x320 15 0 0 50 0 kid 0x320 0x320 0x400 10 0 0x100 0x400 curr 0 5 0 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).
So what about rotateRight? 0x100 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; } 0x320 25 0x80 curr 0x200 0x80 0 15 0 0 50 0 kid 0x320 0x320 0x400 10 0x200 0x100 0x400 curr 0 5 0 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).
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.