what is data structures? · what is data structures? example:library is composed of elements...
TRANSCRIPT
![Page 1: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/1.jpg)
CHAPTER 0: INTRODUTION
What is Data Structures? A data structure is defined by
(1) the logical arrangement of data elements, combined with
(2) the set of operations we need to access the elements.
![Page 2: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/2.jpg)
Atomic Variables
Atomic variables can only store one value at a time.int num;
float s;
A value stored in an atomic variable cannot be subdivided.
![Page 3: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/3.jpg)
What is Data Structures?
Example:library is composed of elements (books)
Accessing a particular book requires knowledge of the arrangement of the books
Users access books only through the librarian
the logical arrangement of data elements, combined withthe set of operations we need to access the elements.
![Page 4: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/4.jpg)
Basic Data Structures
Structures include linked lists
Stack, Queue
binary trees
…and others
![Page 5: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/5.jpg)
Need for Data Structures
Data structures organize data ⇒ more efficient programs.
More powerful computers ⇒ more complex applications.
More complex applications demand more calculations.
![Page 6: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/6.jpg)
Organizing Data
Any organization for a collection of records that can be searched, processed in any order, or modified.
The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days.
![Page 7: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/7.jpg)
Efficiency
A solution is said to be efficient if it solves the problem within its resource constraints.
Space Time
The cost of a solution is the amount of resources that the solution consumes.
![Page 8: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/8.jpg)
Selecting a Data Structure
Select a data structure as follows:1. Analyze the problem to determine the resource constraints a
solution must meet.2. Determine the basic operations that must be supported. Quantify
the resource constraints for each operation.3. Select the data structure that best meets these requirements.
![Page 9: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/9.jpg)
Data Structure Philosophy
Each data structure has costs and benefits.
Rarely is one data structure better than another in all situations.
A data structure requires: space for each data item it stores,
time to perform each basic operation,
programming effort.
![Page 10: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/10.jpg)
What is Algorithm?
Algorithm: A computable set of steps to achieve a desired result
Ralationship to Data Structure Example: Find an element
1 2 3 4 5 6 7
1
2
3
4
5
6
7
![Page 11: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/11.jpg)
![Page 12: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/12.jpg)
C LANGUAGE
1. ADDRESS2. POINTERS3. ARRAYS4. ADDRESS OF EACH ELEMENT IN AN ARRAY5. ACCESSING & MANIPULATING AN ARRAY USING
POINTERS6. ANOTHER CASE OF MANIPULATING AN ARRAY
USING POINTERS7. TWO-DIMENSIONAL ARRAY8. POINTER ARRAYS9. STRUCTURES10.STRUCTURE POINTERS
![Page 13: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/13.jpg)
The C++ Preprocessor
C++ is based on C, which was written in the early 1970s
Any command starting with a # in the first column is not a C/C++ statement, but rather a preprocessor statement The preprocessor performed very basic text-based (or
lexical) substitutions
The output is sent to the compiler
![Page 14: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/14.jpg)
The C++ Preprocessor
The sequence is:
file (filename.cpp) → preprocessor → compiler (g++)
Note, this is done automatically by the compiler: no additional steps are necessary
At the top of any C++ program, you will see one or more directives starting with a #, e.g.,
#include <iostream>
![Page 15: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/15.jpg)
Libraries
You will write your code in a file such as Single_list.h where you will implement a data structure
This file will be included in our tester file Single_list_tester.h with a statement such as:
#include "Single_list.h"
The file Single_list_int_driver.cpp then includes the tester file:
#include "Single_list_tester.h"
![Page 16: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/16.jpg)
Libraries
You will note the difference:
#include <iostream>
#include "Single_list.h"
The first looks for a file iostream.h which is shipped with the compiler (the standard library)
The second looks in the current directory
![Page 17: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/17.jpg)
Libraries
In this class, you will put all code in the header file
This is not normal practice: Usually the header (.h) file only contains declarations
The definitions (the actual implementations) are stored in a related file and compiled into an object file
![Page 18: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/18.jpg)
The C++ Preprocessor
![Page 19: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/19.jpg)
The C++ Preprocessor
This class definition contains only the signatures (or prototypes) of the operations
The actual member function definitions may be defined elsewhere, either in: The same file, or
Another file which is compiled into an object file
We will use the first method
![Page 20: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/20.jpg)
The File as the Unit of Compilation
In C/C++, the file is the base unit of compilation: Any .cpp file may be compiled into object code
Only files containing an int main() function can be compiled into an executable
The signature of main is:int main () {
// does some stuff
return 0;
}
The operating system is expecting a return value Usually 0
![Page 21: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/21.jpg)
The File as the Unit of Compilation
This file (example.cpp) contains two functions
#include<iostream>
int sqr( int n ) { // Function declaration and definition
return n*n;
}
int main() {
cout << "The square of 3 is " << sqr(3) << endl;
return 0;
}
![Page 22: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/22.jpg)
Operators
Operators have similar functionality for built-in datatypes:
Assignment =
Arithmetic + - * / %
+= -= *= /= %=
Autoincrement ++
Autodecrement --
Logical && || !
Relational == != < <= >= >
Comments /* */
// to end of line
Bitwise & | ^ ~
&= |= ^=
Bit shifting << >>
<<= >>=
![Page 23: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/23.jpg)
C LANGUAGE
1. ADDRESSFor every variable there are two attributes: address and
value
cout << "Value of 'y' is: " << y << "\n"; cout << "Address of 'y' is: " << &y << "\n\n";
In memory with address 3: value: 45. In memory with address 2: value "Dave"
![Page 24: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/24.jpg)
C LANGUAGE
2. POINTERS1. is a variable whose value is also an address.
2. A pointer to an integer is a variable that can store the address of that integer
ia: value of variable
&ia: address of ia
*ia means you are printing the value at the location specified by ia
![Page 25: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/25.jpg)
C LANGUAGE
int i; //Aint * ia; //Bcout<<"The address of i "<< &i << " value="<<i <<endl; cout<<"The address of ia " << &ia << " value = " << ia<< endl;
i = 10; //Cia = &i; //D
cout<<"after assigning value:"<<endl;cout<<"The address of i "<< &i << " value="<<i <<endl; cout<<"The address of ia " << &ia << " value = " << ia<< " point to: "<< *ia;
![Page 26: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/26.jpg)
C LANGUAGE
Points to Remember
• Pointers give a facility to access the value of a variable indirectly.
• You can define a pointer by including a * before the name of the variable.
• You can get the address where a variable is stored by using &.
![Page 27: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/27.jpg)
C LANGUAGE
3. ARRAYS1. An array is a data structure 2. used to process multiple elements with the
same data type when a number of such elements are known.
3. An array is a composite data structure; that means it had to be constructed from basic data types such as array integers.
1. int a[5]; 2. for(int i = 0;i<5;i++)
1. {a[i]=i; }
![Page 28: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/28.jpg)
What is Array Name?
‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue.
For example, if we have the code
int a, b;
then we can write
b = 2;a = b;a = 5;
But we cannot write
2 = a;
![Page 29: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/29.jpg)
Array Layout
x[1]
x[2]
x[3]
x[4]
x[5]
x[0]
Array cells are contiguous in computer memory
The memory can be thought of as an array
![Page 30: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/30.jpg)
C LANGUAGE
4. ADDRESS OF EACH ELEMENT IN AN ARRAYEach element of the array has a memory address.
void printdetail(int a[]){
for(int i = 0;i<5;i++){
cout<< "value in array “<< a[i] <<“ at address: “ << &a[i]);}
![Page 31: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/31.jpg)
C LANGUAGE 5. ACCESSING & MANIPULATING AN ARRAY USING
POINTERS
You can access an array element by using a pointer.
If an array stores integers->use a pointer to integer to access array elements.
![Page 32: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/32.jpg)
C LANGUAGE
6. ANOTHER CASE OF MANIPULATING AN ARRAY USING POINTERSThe array limit is a pointer constant : cannot change its
value in the program.
int a[5]; int *b;
a=b; //error
b=a; //OK
It works correctly even using a++ ???
![Page 33: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/33.jpg)
C LANGUAGE 7. TWO-DIMENSIONAL ARRAY
int a[3][2];
![Page 34: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/34.jpg)
C LANGUAGE 8. POINTER ARRAYS
You can define a pointer array (similarly to an array of integers).
In the pointer array, the array elements store the pointer that points to integer values.
![Page 35: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/35.jpg)
C LANGUAGE
9. STRUCTURES Structures are used when
you want to process data of multiple data types
But you still want to refer to the data as a single entity
Access data: structurename.membername
![Page 36: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/36.jpg)
C LANGUAGE
10. STRUCTURE POINTERSProcess the structure using a structure pointer
![Page 37: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/37.jpg)
FUNCTION & RECURSION
1. FUNCTION 2. THE CONCEPT OF STACK 3. THE SEQUENCE OF EXECUTION DURING A
FUNCTION CALL 4. PARAMETER PASSING 5. CALL BY REFERENCE 6. RESOLVING VARIABLE REFERENCES 7. RECURSION 8. STACK OVERHEADS IN RECURSION 9. WRITING A RECURSIVE FUNCTION 10. TYPES OF RECURSION
![Page 38: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/38.jpg)
FUNCTION & RECURSION
1. FUNCTION provide modularity to the software
divide complex tasks into small manageable tasks
avoid duplication of work
![Page 39: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/39.jpg)
FUNCTION & RECURSION
2. THE CONCEPT OF STACK A stack is memory in which values are stored and
retrieved in "last in first out" manner by using operations called push and pop.
![Page 40: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/40.jpg)
FUNCTION & RECURSION
3. THE SEQUENCE OF EXECUTION DURING A FUNCTION CALLWhen the function is called, the current
execution is temporarily stopped and the control goes to the called function. After the call, the execution resumes from the point at which the execution is stopped.
To get the exact point at which execution is resumed, the address of the next instruction is stored in the stack. When the function call completes, the address at the top of the stack is taken.
![Page 41: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/41.jpg)
FUNCTION & RECURSION
3. THE SEQUENCE OF EXECUTION DURING A FUNCTION CALL Functions or sub-programs are implemented using a
stack.
When a function is called, the address of the next instruction is pushed into the stack.
When the function is finished, the address for execution is taken by using the pop operation.
![Page 42: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/42.jpg)
FUNCTION & RECURSION
3. THE SEQUENCE OF EXECUTION DURING A FUNCTION CALL
Result:?
![Page 43: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/43.jpg)
FUNCTION & RECURSION
4. PARAMETER * REFERENCE PASSING passing by value
the value before and after the call remains the same
passing by reference changed value after the function completes
![Page 44: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/44.jpg)
FUNCTION & RECURSION
6. RESOLVING VARIABLE REFERENCES
When a variable can be resolved by using multiple references, the local definition is given more preference
![Page 45: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/45.jpg)
FUNCTION & RECURSION
7. RECURSION A method of programming
whereby a function directly or indirectly calls itself
Problems: stop recursion?
![Page 46: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/46.jpg)
FUNCTION & RECURSION
7. RECURSION
![Page 47: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/47.jpg)
FUNCTION & RECURSION
7. RECURSION: Hanoi tower
![Page 48: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/48.jpg)
FUNCTION & RECURSION 7. RECURSION
![Page 49: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/49.jpg)
FUNCTION & RECURSION
8. STACK OVERHEADS IN RECURSION two important results: the depth of recursion and the
stack overheads in recursion
![Page 50: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/50.jpg)
FUNCTION & RECURSION
9. WRITING A RECURSIVE FUNCTION Recursion enables us to write a program in a natural
way. The speed of a recursive program is slower because of stack overheads.
In a recursive program you have to specify recursive conditions, terminating conditions, and recursive expressions.
![Page 51: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/51.jpg)
FUNCTION & RECURSION
10. TYPES OF RECURSION LINEAR RECURSION
TAIL RECURSION
BINARY RECURSION
EXPONENTIAL RECURSION
NESTED RECURSION
MUTUAL RECURSION
![Page 52: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/52.jpg)
FUNCTION & RECURSION
10. TYPES OF RECURSION LINEAR RECURSION
only makes a single call to itself each time the function runs
![Page 53: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/53.jpg)
FUNCTION & RECURSION
10. TYPES OF RECURSION TAIL RECURSION
Tail recursion is a form of linear recursion.
In tail recursion, the recursive call is the last thing the function does. Often, the value of the recursive call is returned.
![Page 54: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/54.jpg)
FUNCTION & RECURSION
10. TYPES OF RECURSION BINARY RECURSION
Some recursive functions don't just have one call to themself, they have two (or more).
![Page 55: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/55.jpg)
FUNCTION & RECURSION
10. TYPES OF RECURSION EXPONENTIAL RECURSION
An exponential recursive function is one that, if you were to draw out a representation of all the function calls, would have an exponential number of calls in relation to the size of the data set
(exponential meaning if there were n elements, there would be O(an) function calls where a is a positive number)
![Page 56: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/56.jpg)
FUNCTION & RECURSION
10. TYPES OF RECURSION EXPONENTIAL RECURSION
![Page 57: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/57.jpg)
FUNCTION & RECURSION
10. TYPES OF RECURSION NESTED RECURSION
In nested recursion, one of the arguments to the recursive function is the recursive function itself
These functions tend to grow extremely fast.
![Page 58: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/58.jpg)
FUNCTION & RECURSION
10. TYPES OF RECURSION MUTUAL RECURSION
A recursive function doesn't necessarily need to call itself.
Some recursive functions work in pairs or even larger groups. For example, function A calls function B which calls function C which in turn calls function A.
![Page 59: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/59.jpg)
FUNCTION & RECURSION
10. TYPES OF RECURSION MUTUAL RECURSION
![Page 60: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/60.jpg)
Recursion Excercises
Write a program to compute: S = 1 + 2 + 3 + …n using recursion.
Write a program to print a revert number Example: input n=12345. Print out: 54321.
Write a program to print this number Example: input n=12345. Print out: 12345.
E4. Write a recursion function to calculate: S=a[0]+a[1]+…a[n-1]
A: array of integer numbers
![Page 61: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/61.jpg)
Stack
LIFO (last in first out)
![Page 62: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/62.jpg)
Stack
Managing Top element
![Page 63: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/63.jpg)
Application of stack
Stack: Expression evaluation a*(b–c)/d => abc–*d/
![Page 64: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/64.jpg)
Use of Stack
Example of use: prefix, infix, postfix expressions. Consider the expression A+B: we think of applying the operator “+” to
the operands A and B. “+” is termed a binary operator: it takes two operands. Writing the sum as A+B is called the infix form of the expression.
![Page 65: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/65.jpg)
Prefix, Infix, Postfix
Two other ways of writing the expression are
+ A B prefixA B + postfix
The prefixes “pre” and “post” refer to the position of the operator with respect to the two operands.
![Page 66: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/66.jpg)
Prefix, Infix, Postfix
Consider the infix expressionA + B * C
We “know” that multiplication is done before addition.
The expression is interpreted as A + ( B * C )
Multiplication has precedence over addition.
![Page 67: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/67.jpg)
Prefix, Infix, Postfix
Conversion to postfix
A + ( B * C ) infix form
![Page 68: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/68.jpg)
Prefix, Infix, Postfix
Conversion to postfix
A + ( B * C ) infix form
A + ( B C * ) convert multiplication
![Page 69: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/69.jpg)
Prefix, Infix, Postfix
Conversion to postfix
A + ( B * C ) infix form
A + ( B C * ) convert multiplication
A ( B C * ) + convert addition
![Page 70: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/70.jpg)
Prefix, Infix, Postfix
Conversion to postfix
A + ( B * C ) infix form
A + ( B C * ) convert multiplication
A ( B C * ) + convert addition
A B C * + postfix form
![Page 71: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/71.jpg)
Prefix, Infix, Postfix
Conversion to postfix
(A + B ) * C infix form
![Page 72: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/72.jpg)
Prefix, Infix, Postfix
Conversion to postfix
(A + B ) * C infix form
( A B + ) * C convert addition
![Page 73: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/73.jpg)
Prefix, Infix, Postfix
Conversion to postfix
(A + B ) * C infix form
( A B + ) * C convert addition
( A B + ) C * convert multiplication
![Page 74: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/74.jpg)
Prefix, Infix, Postfix
Conversion to postfix
(A + B ) * C infix form
( A B + ) * C convert addition
( A B + ) C * convert multiplication
A B + C * postfix form
![Page 75: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/75.jpg)
Converting Infix to Postfix
Consider the infix expressions ‘A+B*C’ and ‘ (A+B)*C’.
The postfix versions are ‘ABC*+’ and ‘AB+C*’.
The order of operands in postfix is the same as the infix.
In scanning from left to right, the operand ‘A’ can be inserted into postfix expression.
![Page 76: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/76.jpg)
Converting Infix to Postfix
The ‘+’ cannot be inserted until its second operand has been scanned and inserted.
The ‘+’ has to be stored away until its proper position is found. When ‘B’ is seen, it is immediately inserted into the postfix expression. Can the ‘+’ be inserted now? In the case of ‘A+B*C’ cannot
because * has precedence.
![Page 77: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/77.jpg)
Converting Infix to Postfix
Example: A + B * Csymb postfix stackA A
![Page 78: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/78.jpg)
Converting Infix to Postfix
Example: A + B * Csymb postfix stackA A+ A +
![Page 79: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/79.jpg)
Converting Infix to Postfix
Example: A + B * Csymb postfix stackA A+ A +B AB +
![Page 80: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/80.jpg)
Converting Infix to Postfix
Example: A + B * Csymb postfix stackA A+ A +B AB +* AB + *
![Page 81: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/81.jpg)
Converting Infix to Postfix
Example: A + B * Csymb postfix stackA A+ A +B AB +* AB + *C ABC + *
![Page 82: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/82.jpg)
Converting Infix to Postfix
Example: A + B * Csymb postfix stackA A+ A +B AB +* AB + *C ABC + *
ABC * +
![Page 83: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/83.jpg)
Converting Infix to Postfix
Example: A + B * Csymb postfix stackA A+ A +B AB +* AB + *C ABC + *
ABC * +ABC * +
![Page 84: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/84.jpg)
Precedence of Operators
The five binary operators are: addition, subtraction, multiplication, division and exponentiation.
The order of precedence is (highest to lowest)
Exponentiation ↑
Multiplication/division*, /
Addition/subtraction +, -
![Page 85: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/85.jpg)
Precedence of Operators
For operators of same precedence, the left-to-right rule applies:
A+B+C means (A+B)+C.
For exponentiation, the right-to-left rule applies
A ↑ B ↑ C means A ↑ ( B ↑ C )
![Page 86: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/86.jpg)
Infix to Postfix
Infix Postfix
A + B A B +
12 + 60 – 23 12 60 + 23 –
(A + B)*(C – D ) A B + C D – *
A ↑ B * C – D + E/F A B ↑ C*D – E F/+
![Page 87: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/87.jpg)
Infix to Postfix
Note that the postfix form an expression does not require parenthesis.
Consider ‘4+3*5’ and ‘(4+3)*5’. The parenthesis are not needed in the first but they are necessary in the second.
The postfix forms are:4+3*5 435*+ (4+3)*5 43+5*
![Page 88: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/88.jpg)
Reverse-Polish Notation
Normally, mathematics is written using what we call in-fix notation:
(3 + 4) × 5 – 6
The operator is placed between to operands
One weakness: parentheses are required
(3 + 4) × 5 – 6 = 293 + 4 × 5 – 6= 173 + 4 × (5 – 6) = –1
(3 + 4) × (5 – 6) = –7
![Page 89: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/89.jpg)
Reverse-Polish NotationAlternatively, we can place the operands first, followed by the operator:
(3 + 4) × 5 – 63 4 + 5 × 6 –
Parsing reads left-to-right and performs any operation on thelast two operands:
3 4 + 5 × 6 –
7 5 × 6 –
35 6 –
29
![Page 90: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/90.jpg)
Reverse-Polish Notation
This is called reverse-Polish notation after the mathematician Jan Łukasiewicz As you will see in ECE 222, this forms the basis
of the recursive stack used on all processors
He also made significant contributions tologic and other fields Including humour…
http://www.audiovis.nac.gov.pl/http://xkcd.com/645/
![Page 91: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/91.jpg)
Reverse-Polish NotationOther examples:
3 4 5 × + 6 –3 20 + 6 –
23 6 –17
3 4 5 6 – × +3 4 –1 × +3 –4 +
–1
![Page 92: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/92.jpg)
Reverse-Polish Notation
Benefits: no ambiguity and no brackets are required
this is the same process used by a computer to perform computations: operands must be loaded into registers before operations
can be performed on them
reverse-Polish can be processed using stacks
![Page 93: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/93.jpg)
Reverse-Polish Notation
The easiest way to parse reverse-Polish notation is to use an operand stack: operands are processed by pushing them onto the stack
when processing an operator: pop the last two items off the operand stack,
perform the operation, and
push the result back onto the stack
![Page 94: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/94.jpg)
Reverse-Polish NotationEvaluate the following reverse-Polish expression using a stack:
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
![Page 95: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/95.jpg)
Reverse-Polish Notation
Push 1 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
1
![Page 96: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/96.jpg)
Reverse-Polish Notation
Push 1 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
21
![Page 97: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/97.jpg)
Reverse-Polish Notation
Push 3 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
321
![Page 98: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/98.jpg)
Reverse-Polish Notation
Pop 3 and 2 and push 2 + 3 = 5
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
51
![Page 99: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/99.jpg)
Reverse-Polish Notation
Push 4 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
451
![Page 100: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/100.jpg)
Reverse-Polish Notation
Push 5 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
5451
![Page 101: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/101.jpg)
Reverse-Polish Notation
Push 6 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
65451
![Page 102: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/102.jpg)
Reverse-Polish NotationPop 6 and 5 and push 5 × 6 = 30
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
30451
![Page 103: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/103.jpg)
Reverse-Polish NotationPop 30 and 4 and push 4 – 30 = –26
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–2651
![Page 104: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/104.jpg)
Reverse-Polish Notation
Push 7 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
7–2651
![Page 105: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/105.jpg)
Reverse-Polish NotationPop 7 and –26 and push –26 × 7 = –182
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–18251
![Page 106: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/106.jpg)
Reverse-Polish NotationPop –182 and 5 and push –182 + 5 = –177
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–1771
![Page 107: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/107.jpg)
Reverse-Polish Notation
Pop –177 and 1 and push 1 – (–177) = 178
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
178
![Page 108: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/108.jpg)
Reverse-Polish Notation
Push 8 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
8178
![Page 109: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/109.jpg)
Reverse-Polish Notation
Push 1 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9× +
98
178
![Page 110: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/110.jpg)
Reverse-Polish NotationPop 9 and 8 and push 8 × 9 = 72
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
72178
![Page 111: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/111.jpg)
Reverse-Polish NotationPop 72 and 178 and push 178 + 72 = 250
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
250
![Page 112: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/112.jpg)
Reverse-Polish Notation
Thus
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
evaluates to the value on the top: 250
The equivalent in-fix notation is
((1 – ((2 + 3) + ((4 – (5 × 6)) × 7))) + (8 × 9))
We reduce the parentheses using order-of-operations:
1 – (2 + 3 + (4 – 5 × 6) × 7) + 8 × 9
![Page 113: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/113.jpg)
Reverse-Polish Notation
Incidentally,
1 – 2 + 3 + 4 – 5 × 6 × 7 + 8 × 9 = –132which has the reverse-Polish notation of
1 2 – 3 + 4 + 5 6 7 × × – 8 9 × +
For comparison, the calculated expression was
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
![Page 114: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/114.jpg)
Evaluating Postfix
Each operator in a postfix expression refers to the previous two operands.
Each time we read an operand, we push it on a stack.
When we reach an operator, we pop the two operands from the top of the stack, apply the operator and push the result back on the stack.
![Page 115: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/115.jpg)
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
![Page 116: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/116.jpg)
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
![Page 117: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/117.jpg)
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
![Page 118: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/118.jpg)
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
![Page 119: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/119.jpg)
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
![Page 120: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/120.jpg)
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
![Page 121: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/121.jpg)
Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
![Page 122: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/122.jpg)
Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
![Page 123: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/123.jpg)
Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
![Page 124: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/124.jpg)
Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
![Page 125: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/125.jpg)
Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
![Page 126: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/126.jpg)
Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
![Page 127: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/127.jpg)
Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
↑ 7 2 49 49
![Page 128: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/128.jpg)
Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
↑ 7 2 49 49
3 7 2 49 49,3
![Page 129: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/129.jpg)
Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
↑ 7 2 49 49
3 7 2 49 49,3
+ 49 3 52 52
![Page 130: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/130.jpg)
Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
↑ 7 2 49 49
3 7 2 49 49,3
+ 49 3 52 52
![Page 131: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/131.jpg)
Queue
An Abstract Queue (Queue ADT) is an abstract data type that emphasizes specific operations: Uses a explicit linear ordering
Insertions and removals are performed individually
There are no restrictions on objects inserted into (pushed onto) the queue—that object is designated the back of the queue
The object designated as the front of the queue is the object which was in the queue the longest
The remove operation (popping from the queue) removes the current front of the queue
![Page 132: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/132.jpg)
QUEUE
FIFO (first in first out)
![Page 133: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/133.jpg)
QUEUE: implement using array
![Page 134: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/134.jpg)
QueueAlso called a first-in–first-out (FIFO) data structure Graphically, we may view these operations as follows:
![Page 135: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/135.jpg)
Queue
Alternative terms may be used for the four operations on a queue, including:
![Page 136: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/136.jpg)
Queue
There are two exceptions associated with this abstract data structure: It is an undefined operation to call either pop or front on
an empty queue
![Page 137: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/137.jpg)
ApplicationsThe most common application is in client-server models Multiple clients may be requesting services from one or
more servers
Some clients may have to wait while the servers are busy
Those clients are placed in a queue and serviced in the order of arrival
Grocery stores, banks, and airport security use queues
The SSH Secure Shell and SFTP are clients
Most shared computer services are servers: Web, file, ftp, database, mail, printers, WOW, etc.
![Page 138: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/138.jpg)
Applications
For example, in downloading these presentations from the web server, those requests not currently being downloaded aremarked as “Queued”
![Page 139: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/139.jpg)
Array ImplementationWe need to store an array: In C++, this is done by storing the address of the first entry
Type *array;
We need additional information, including: The number of objects currently in the queue and the
front and back indices
int queue_size;int ifront; // index of the front entryint iback; // index of the back entry
The capacity of the array
int array_capacity;
![Page 140: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/140.jpg)
Back and front
Before we initialize the values, we will state that iback is the index of the most-recently pushed object
ifront is the index of the object at the front of the queue
To push, we will increment iback and place the new item at that location To make sense of this, we will initialize
iback = -1;
ifront = 0;
After the first push, we will increment iback to 0, place the pushed item at that location, and now
![Page 141: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/141.jpg)
Back and front
Again, we must initialize the values We must allocate memory for the array and initialize the
member variables
The call to new Type[array_capacity] makes a request to the operating system for array_capacity objects
![Page 142: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/142.jpg)
FunctionsSuppose that: The array capacity is 16
We have performed 16 pushes
We have performed 5 pops The queue size is now 11
We perform one further push
In this case, the array is not full and yet we cannot place any more objects in to the array
![Page 143: What is Data Structures? · What is Data Structures? Example:library is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books](https://reader030.vdocuments.us/reader030/viewer/2022040302/5e7b91cc291b250a1902c1e5/html5/thumbnails/143.jpg)
QUEUE: implement using array#define MAX 10void main(){
int queue[MAX];int bottom,top,count=0;bottom=top=-1;
enqueue(queue,count,top, 100 );int value;dequeue(queue,count,bottom,top,value);
}