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

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

1 Pointers and Strings Section 5.4, , Lecture 12.
For(int i = 1; i
Part 1 Landscape Hannu Laine. Pointer parameters //prototype of swap void swap(int *a, int *b); //application void main(void) { in number1 = 1, number2.
PHYS707: Special Topics C++ Lectures Lecture 3. Summary of Today’s lecture: 1.Functions (call-by-referencing) 2.Arrays 3.Pointers 4.More Arrays! 5.More.

Inheritance (2).
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Functions Prototypes, parameter passing, return values, activation frams.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Passing Streams to Functions. Passing Streams to Functions One Rule: always pass a stream as a reference.
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
CS 261 – Data Structures AVL Trees. Binary Search Tree: Balance Complexity of BST operations: proportional to the length of the path from the root to.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Pointer. Kegunaan pointer yang utama adalah untuk menyimpan memory address dari sebuah variable (data type atau object dari class). Selain menyimpan address.
CPSC 388 – Compiler Design and Construction Parameter Passing.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
Pointers 1 Pointers Pointers  In chapter three we looked at passing parameters to functions using call by value, and using call by reference, using reference.
AVL Trees Data Structures Fall 2006 Evan Korth Adopted from a presentation by Simon Garrett and the Mark Allen Weiss book.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
1 AVL Trees II Implementation. 2 Download: 2011_03_28_AVL_Tree/ File AVL_Tree_Demo.zip
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
AVL Tree 1. is a binary search tree that For each node: –The height of its left and right subtree can only differ at most 1. –If a tree is an empty tree,
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Source Page US:official&tbm=isch&tbnid=Mli6kxZ3HfiCRM:&imgrefurl=
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
CSci 162 Lecture 6 Martin van Bommel. Functions on Structures Rather than pass a copy of structure to a function, or return a copy back as the function.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
Путешествуй со мной и узнаешь, где я сегодня побывал.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Day 12 – September 11th Objective: To write an equations of a line given its slope and a point on the line  
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
CS 261 – Recitation 7 Fall 2010 CS 261 – Data Structures
Dissection of AVL tree operations
AVL Tree Mohammad Asad Abbasi Lecture 12
Trees (Chapter 4) Binary Search Trees - Review Definition
Pointers Psst… over there.
Page 1. Page 2 Page 3 Page 4 Page 5 Page 6 Page 7.
Pointers Psst… over there.
AVL Trees CENG 213 Data Structures.
Trees.
Back Cover Cover Page Page 1-Triple click in this textbox and enter your information for Page 1 of your book. Page2-Triple click in this textbox and enter.
Return by Reference CSCE 121 J. Michael Moore.
Pointers & Functions.
Shaker.
Example passing the address of x x pint or
Stack Frames and Functions
Dynamic Memory A whole heap of fun….
Pointers Lecture 2 Tue, Jan 24, 2006.
Back Cover Cover Page Page 1-Triple-click in this text box and enter your information for Page 1 of your book. Page 2-Triple-click in this text box and.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Pointers & Functions.
The Stack.
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 x100 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 x100 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 x100 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 0x x400 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; } 0x x400 0x320 0x2000x80 0x200 0x320 0x 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; } 0x x400 0x320 0x2000x80 0x x x 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.