Lecture No. 10. Reference Variables. 22-Nov-18. One should be careful about transient objects that are stored by. reference in data structures.

Similar documents
Algorithms. AVL Tree

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

CSCI2100B Data Structures Trees

COMP171. AVL-Trees (Part 1)

CSI33 Data Structures

Data Structures and Algorithms

ECE250: Algorithms and Data Structures AVL Trees (Part A)

Data Structures in Java

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

CPSC 223 Algorithms & Data Abstract Structures

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

LECTURE 18 AVL TREES

AVL Trees. Version of September 6, AVL Trees Version of September 6, / 22

3137 Data Structures and Algorithms in C++

Data Structures Lesson 7

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Balanced Binary Search Trees

CS350: Data Structures AVL Trees

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

CP2 Revision. theme: dynamic datatypes & data structures

Trees. (Trees) Data Structures and Programming Spring / 28

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Part 2: Balanced Trees

Fundamental Algorithms

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

COMP : Trees. COMP20012 Trees 219

Analysis of Algorithms

Lecture 13: AVL Trees and Binary Heaps

CPSC 223 Algorithms & Data Abstract Structures

Section 4 SOLUTION: AVL Trees & B-Trees

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

Trees. A tree is a directed graph with the property

Advanced Tree Data Structures

Balanced Search Trees. CS 3110 Fall 2010

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

Binary search trees (chapters )

CS 261 Data Structures. AVL Trees

Chapter 2: Basic Data Structures

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

Trees. Eric McCreath

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Advanced Set Representation Methods

ADVANCED DATA STRUCTURES STUDY NOTES. The left subtree of each node contains values that are smaller than the value in the given node.

DATA STRUCTURES AND ALGORITHMS

Binary search trees (chapters )

AVL Trees. See Section 19.4of the text, p

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

AVL Trees (10.2) AVL Trees

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Trees. R. J. Renka 10/14/2011. Department of Computer Science & Engineering University of North Texas. R. J. Renka Trees

Recall: Properties of B-Trees

CHAPTER 10 AVL TREES. 3 8 z 4

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013)

Why Do We Need Trees?

CS Transform-and-Conquer

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

More BSTs & AVL Trees bstdelete

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

AVL trees and rotations

CS102 Binary Search Trees

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

CS350: Data Structures Red-Black Trees

A dictionary interface.

Unit III - Tree TREES

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

Trees. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

Tutorial AVL TREES. arra[5] = {1,2,3,4,5} arrb[8] = {20,30,80,40,10,60,50,70} FIGURE 1 Equivalent Binary Search and AVL Trees. arra = {1, 2, 3, 4, 5}

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Lecture Notes on Priority Queues

Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees

CMSC 341 Lecture 15 Leftist Heaps

Search Structures. Kyungran Kang

AVL Trees Heaps And Complexity

Chapter 22 Splay Trees

CMSC 341 Lecture 15 Leftist Heaps

Ch04 Balanced Search Trees

CMPE 160: Introduction to Object Oriented Programming

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

ITEC2620 Introduction to Data Structures

Search Trees - 1 Venkatanatha Sarma Y

Self-Balancing Search Trees. Chapter 11

The questions will be short answer, similar to the problems you have done on the homework

CSI33 Data Structures

Algorithms. Deleting from Red-Black Trees B-Trees

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Recall from Last Time: AVL Trees

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Computer Science Foundation Exam

CISC 235: Topic 4. Balanced Binary Search Trees

Transcription:

Lecture No. Reference Variables One should be careful about transient objects that are stored by reference in data structures. Consider the following code that stores and retrieves objects in a queue.

Reference Variables void loadcustomer( Queue& q) { Customer c( irfan ); Customer c( sohail ; q.enqueue( c ); q.enqueue( c ); } Reference Variables void servicecustomer( Queue& q) { } Customer c = q.dequeue(); cout << c.getname() << endl; We got the reference but the object is gone! The objects were created on the call stack. They disappeared when the loadcustomer function returned.

Reference Variables void loadcustomer( Queue& q) { } Customer* c = new Customer( irfan ); Customer* c = new Customer( sohail ; q.enqueue( c ); // enqueue takes pointers q.enqueue( c ); The pointer variables c and c are on the call stack. They will go but their contents (addresses) are queued. The Customer objects are created in the heap. They will live until explicitly deleted. Memory Organization Process (browser) Process (word) Process (ourtest.exe) Process (dev-c++) Windows OS Code Static data Stack Heap

Reference Variables Call stack layout when q.enqueue(c) called in loadcustomer. elt 7 68 6 6... c c loadcustomer 6 (6) (elt) 6 enqueue sp stack grows downwards Reference Variables Heap layout during call to loadcustomer. heap grows upwards c 68 c sohail Customer( sohail ) -> c 6 irfan Customer( irfan ) -> c 6

Reference Variables void servicecustomer( Queue& q) { Customer* c = q.dequeue(); cout << c->getname() << endl; delete c; // the object in heap dies } Must use the c-> syntax because we get a pointer from the queue. The object is still alive because it was created in the heap. The const Keyword The const keyword is often used in function signatures. The actual meaning depends on where it occurs but it generally means something is to held constant. Here are some common uses.

The const Keyword Use : The const keyword appears before a function parameter. E.g., in a chess program: int movepiece(const Piece& currentpiece) The parameter must remain constant for the life of the function. If you try to change the value, e.g., parameter appears on the left hand side of an assignment, the compiler will generate and error. The const Keyword This also means that if the parameter is passed to another function, that function must not change it either. Use of const with reference parameters is very common. This is puzzling; why are we passing something by reference and then make it constant, i.e., don t change it? Doesn t passing by reference mean we want to change it? 6

The const Keyword The answer is that, yes, we don t want the function to change the parameter, but neither do we want to use up time and memory creating and storing an entire copy of it. So, we make the original object available to the called function by using pass-by-reference. We also mark it constant so that the function will not alter it, even by mistake. The const Keyword Use : The const keyword appears at the end of class member s function signature: EType& findmin( ) const; Such a function cannot change or write to member variables of that class. This type of usage often appears in functions that are suppose to read and return member variables. 7

The const Keyword Use : The const keyword appears at the beginning of the return type in function signature: const EType& findmin( ) const; Means, whatever is returned is constant. The purpose is typically to protect a reference variable. This also avoids returning a copy of an object. Degenerate inary Search Tree ST for,,, 9, 7, 8,,, 6,, 7 9 8 7 6 7 8

Degenerate inary Search Tree ST for 7 9 6 7 8 7 9 6 7 8 Degenerate inary Search Tree ST for 7 9 6 7 8 7 9 Linked List! 6 7 8 9

alanced ST We should keep the tree balanced. One idea would be to have the left and right subtrees have the same height alanced ST 7 9 6 7 8 Does not force the tree to be shallow.

alanced ST We could insist that every node must have left and right subtrees of same height. ut this requires that the tree be a complete binary tree To do this, there must have ( d+ ) data items, where d is the depth of the tree. This is too rigid a condition. AVL Tree AVL (Adelson-Velskii and Landis) tree. An AVL tree is identical to a ST except height of the left and right subtrees can differ by at most. height of an empty tree is defined to be ( ).

AVL Tree An AVL Tree level 8 7 AVL Tree Not an AVL tree 6 level 8

alanced inary Tree The height of a binary tree is the maximum level of its leaves (also called the depth). The balance of a node in a binary tree is defined as the height of its left subtree minus height of its right subtree. Here, for example, is a balanced tree. Each node has an indicated balance of,, or. alanced inary Tree - -

alanced inary Tree Insertions and effect on balance - - U U U U U U 6 U 7 U 8 U 9 U U U alanced inary Tree Tree becomes unbalanced only if the newly inserted node is a left descendant of a node that previously had a balance of (U to U 8 ), or is a descendant of a node that previously had a balance of (U 9 to U )

alanced inary Tree Insertions and effect on balance - - U U U U U U 6 U 7 U 8 U 9 U U U alanced inary Tree Consider the case of node that was previously - - U U U U U U 6 U 7 U 8 U 9 U U U

Inserting New Node in AVL Tree A T T T Inserting New Node in AVL Tree A T T T new 6

Inserting New Node in AVL Tree A A T T T T T T new new Inorder: T T A T Inorder: T T A T AVL Tree uilding Example Let us work through an example that inserts numbers in a balanced search tree. We will check the balance after each insert and rebalance if necessary using rotations. 7

AVL Tree uilding Example Insert() AVL Tree uilding Example Insert() 8

AVL Tree uilding Example Insert() single left rotation - AVL Tree uilding Example Insert() single left rotation - 9

AVL Tree uilding Example Insert() AVL Tree uilding Example Insert()

AVL Tree uilding Example Insert() - AVL Tree uilding Example Insert()

AVL Tree uilding Example Insert(6) - 6 AVL Tree uilding Example Insert(6) 6

AVL Tree uilding Example Insert(7) - 6 7 AVL Tree uilding Example Insert(7) 6 7 6

AVL Tree uilding Example Insert(6) 6 7 6 7 AVL Tree uilding Example Insert() 6 7-6 8

AVL Tree uilding Example Insert() 6 6-7 9 Cases for Rotation Single rotation does not seem to restore the balance. The problem is the node is in an inner subtree that is too deep. Let us revisit the rotations.

Cases for Rotation Let us call the node that must be rebalanced. Since any node has at most two children, and a height imbalance requires that s two subtrees differ by two (or ), the violation will occur in four cases: Cases for Rotation. An insertion into left subtree of the left child of.. An insertion into right subtree of the left child of.. An insertion into left subtree of the right child of.. An insertion into right subtree of the right child of. 6

Cases for Rotation The insertion occurs on the outside (i.e., left-left or rightright) in cases and Single rotation can fix the balance in cases and. Insertion occurs on the inside in cases and which single rotation cannot fix. Cases for Rotation Single right rotation to fix case. k k k Z Level n- X k X Y Level n- Y Z new Level n new 7

Cases for Rotation Single left rotation to fix case. k k X k Level n- k Y Level n- X Y Z Z Level n Cases for Rotation Single right rotation fails to fix case. k k k Z Level n- X k X Y Level n- Y Z new Level n new 6 8