1 / 12

Single Right rotation (compare to RotateWithLeftChild page 148 text)

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;

gordy
Télécharger la présentation

Single Right rotation (compare to RotateWithLeftChild page 148 text)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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; }

  2. 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.

  3. 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

  4. 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

  5. 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.

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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).

  11. 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).

  12. 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.

More Related