Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Lecture 3 data structures and algorithms
-
Upload
aakash-singhal -
Category
Education
-
view
397 -
download
2
Transcript of Lecture 3 data structures and algorithms
![Page 1: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/1.jpg)
1
Data Structure and AlgorithmCS-102
Ashok K Turuk
![Page 2: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/2.jpg)
2
Data Structure vs
Storage Structure
• Data Structure : The logical or mathematical model of a particular organization of data
• Storage Structure : Representation of a particular data structure in the memory of a computer
• There are many possible storage structure to a particular data structure • Ex: there are a number of storage structure for a
data structure such as array
– It is possible for two DS to represented by the same storage structure
![Page 3: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/3.jpg)
3
Classification
• Data Structure– Linear – Non-Linear
• A Data structure is said to be linear if its elements from a sequence or in other words form a linear list
![Page 4: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/4.jpg)
4
Representation in Memory
• Two basic representation in memory – Have a linear relationship between the
elements represented by means of sequential memory locations [ Arrays]
– Have the linear relationship between the elements represented by means of pointer or links [ Linked List]
![Page 5: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/5.jpg)
5
Operation on Linear Structure
• Traversal : Processing each element in the list
• Search : Finding the location of the element with a given value or the record with a given key
• Insertion: Adding a new element to the list• Deletion: Removing an elements from the list• Sorting : Arranging the elements in some
type of order• Merging : Combining two list into a single list
![Page 6: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/6.jpg)
6
Array
![Page 7: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/7.jpg)
7
Linear Arrays
• A linear array is a list of a finite number of n homogeneous data elements ( that is data elements of the same type) such that – The elements are of the arrays are
referenced respectively by an index set consisting of n consecutive numbers
– The elements of the arrays are stored respectively in successive memory locations
![Page 8: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/8.jpg)
8
Linear Arrays
• The number n of elements is called the length or size of the array.
• The index set consists of the integer 1, 2, … n
• Length or the number of data elements of the array can be obtained from the index set byLength = UB – LB + 1 where UB is the largest index called the upper bound and LB is the smallest index called the lower bound of the arrays
![Page 9: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/9.jpg)
9
Linear Arrays
• Element of an array A may be denoted by – Subscript notation A1, A2, , …. , An
– Parenthesis notation A(1), A(2), …. , A(n)– Bracket notation A[1], A[2], ….. , A[n]
• The number K in A[K] is called subscript or an index and A[K] is called a subscripted variable
![Page 10: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/10.jpg)
10
Representation of Linear Array in Memory
1000
1001
1002
1003
1004
1005
Computer Memory
:
![Page 11: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/11.jpg)
11
Representation of Linear Array in Memory
• Let LA be a linear array in the memory of the computer
• LOC(LA[K]) = address of the element LA[K] of the array LA
• The element of LA are stored in the successive memory cells
• Computer does not need to keep track of the address of every element of LA, but need to track only the address of the first element of the array denoted by Base(LA) called the base address of LA
![Page 12: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/12.jpg)
12
Representation of Linear Array in Memory
• LOC(LA[K]) = Base(LA) + w(K – lower bound) where w is the number of words per memory cell of the array LA [w is aka size of the data type]
![Page 13: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/13.jpg)
13
Example 1
200
201
202
203
204
205
206
207
LA[1]
LA[3]
LA[4]
LA[5]
LA[6]
LA[7]
LA[8]
LA[2]Find the address for LA[6]Each element of the array occupy 1 byte
LOC(LA[K]) = Base(LA) + w(K – lower bound)
LOC(LA[6]) = 200 + 1(6 – 1) = 205
:
![Page 14: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/14.jpg)
14
Example 2
200
201
202
203
204
205
206
207
LA[1]
LA[2]
LA[3]
LA[4]
Find the address for LA[16]Each element of the array occupy 2 byte
LOC(LA[K]) = Base(LA) + w(K – lower bound)
LOC(LA[16]) = 200 + 2(16 – 1) = 230
:
![Page 15: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/15.jpg)
15
Representation of Linear Array in Memory
• Given any value of K, time to calculate LOC(LA[K]) is same
• Given any subscript K one can access and locate the content of LA[K] without scanning any other element of LA
• A collection A of data element is said to be index if any element of A called Ak can be located and processed in time that is independent of K
![Page 16: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/16.jpg)
16
Traversing Linear Arrays
• Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones
•••
Linear Array
![Page 17: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/17.jpg)
17
Traversing Linear Arrays
• Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones
•••
Linear Array
![Page 18: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/18.jpg)
18
Traversing Linear Arrays
• Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones
•••
Linear Array
![Page 19: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/19.jpg)
19
Traversing Linear Arrays
• Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones
•••
Linear Array
![Page 20: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/20.jpg)
20
Traversing Linear Arrays
• Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones
•••
Linear Array
![Page 21: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/21.jpg)
21
Traversing Linear Arrays
• Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones
•••
Linear Array
![Page 22: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/22.jpg)
22
Traversing Linear Arrays
• Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones
•••
Linear Array
1. Repeat for K = LB to UBApply PROCESS to LA[K][End of Loop]
2. Exit
![Page 23: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/23.jpg)
23
Inserting and Deleting
• Insertion: Adding an element– Beginning–Middle– End
• Deletion: Removing an element – Beginning–Middle– End
![Page 24: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/24.jpg)
24
Insertion
1 Brown
2 Davis
3 Johnson
4 Smith
5 Wagner
6
7
8
1 Brown
2 Davis
3 Johnson
4 Smith
5 Wagner
6 Ford
7
8
Insert Ford at the End of Array
![Page 25: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/25.jpg)
25
Insertion
1 Brown
2 Davis
3 Johnson
4 Smith
5 Wagner
6
7
8
1 Brown
2 Davis
3 Johnson
4 Smith
5
6 Wagner
7
8
Insert Ford as the 3rd Element of Array
1 Brown
2 Davis
3 Johnson
4
5 Smith
6 Wagner
7
8
1 Brown
2 Davis
3
4 Johnson
5 Smith
6 Wagner
7
8
Ford
Insertion is not Possible without loss of data if the array is FULL
![Page 26: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/26.jpg)
26
Deletion
1 Brown
2 Davis
3 Ford
4 Johnson
5 Smith
6 Taylor
7 Wagner
8
1 Brown
2 Davis
3 Ford
4 Johnson
5 Smith
6 Taylor
7
8
Deletion of Wagner at the End of Array
![Page 27: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/27.jpg)
27
Deletion
1 Brown
2 Davis
3 Ford
4 Johnson
5 Smith
6 Taylor
7 Wagner
8
1 Brown
2
3 Ford
4 Johnson
5 Smith
6 Taylor
7 Wagner
8
Deletion of Davis from the Array
1 Brown
2 Ford
3
4 Johnson
5 Smith
6 Taylor
7 Wagner
8
1 Brown
2 Ford
3 Johnson
4
5 Smith
6 Taylor
7 Wagner
8
![Page 28: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/28.jpg)
28
Deletion
1 Brown
2 Ford
3 Johnson
4 Smith
5 Taylor
6 Wagner
7
8
No data item can be deleted from an empty array
![Page 29: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/29.jpg)
29
Insertion Algorithm
• INSERT (LA, N , K , ITEM) [LA is a linear array with N elements and K is a positive integers such that K ≤ N. This algorithm insert an element ITEM into the Kth position in LA ] 1. [Initialize Counter] Set J := N2. Repeat Steps 3 and 4 while J ≥ K3. [Move the Jth element downward ] Set LA[J + 1] := LA[J] 4. [Decrease Counter] Set J := J -15 [Insert Element] Set LA[K] := ITEM6. [Reset N] Set N := N +1;7. Exit
![Page 30: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/30.jpg)
30
Deletion Algorithm
• DELETE (LA, N , K , ITEM) [LA is a linear array with N elements and K is a positive integers such that K ≤ N. This algorithm deletes Kth element from LA ] 1. Set ITEM := LA[K]2. Repeat for J = K to N -1: [Move the J + 1st element upward] Set LA[J]
:= LA[J + 1] 3. [Reset the number N of elements] Set N := N - 1;4. Exit
![Page 31: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/31.jpg)
31
Multidimensional Array
• One-Dimensional Array• Two-Dimensional Array• Three-Dimensional Array• Some programming Language allows
as many as 7 dimension
![Page 32: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/32.jpg)
32
Two-Dimensional Array
• A Two-Dimensional m x n array A is a collection of m . n data elements such that each element is specified by a pair of integer (such as J, K) called subscript with property that 1 ≤ J ≤ m and 1 ≤ K ≤ n
The element of A with first subscript J and second subscript K will be denoted by AJ,K
or A[J][K]
![Page 33: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/33.jpg)
2D Arrays
The elements of a 2-dimensional array a is shown as below
a[0][0] a[0][1] a[0][2] a[0][3]a[1][0] a[1][1] a[1][2] a[1][3]a[2][0] a[2][1] a[2][2] a[2][3]
![Page 34: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/34.jpg)
Rows Of A 2D Array
a[0][0] a[0][1] a[0][2] a[0][3] row 0a[1][0] a[1][1] a[1][2] a[1][3] row 1a[2][0] a[2][1] a[2][2] a[2][3] row 2
![Page 35: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/35.jpg)
Columns Of A 2D Array
a[0][0] a[0][1] a[0][2] a[0][3]a[1][0] a[1][1] a[1][2] a[1][3]a[2][0] a[2][1] a[2][2] a[2][3]
column 0 column 1
column 2
column 3
![Page 36: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/36.jpg)
36
2D Array
• Let A be a two-dimensional array m x n• The array A will be represented in the
memory by a block of m x n sequential memory location
• Programming language will store array A either – Column by Column (Called Column-Major
Order) Ex: Fortran, MATLAB– Row by Row (Called Row-Major Order) Ex:
C, C++ , Java
![Page 37: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/37.jpg)
37
2D Array in Memory
A Subscript
(1,1)
(2,1)
(3,1)
(1,2)
(2,2)
(3,2)
(1,3)
(2,3)
(3,3)
(1,4)
(2,4)
(3,4)
Column 1
Column 2
Column 3
Column 4
A Subscript
(1,1)
(1,2)
(1,3)
(1,4)
(2,1)
(2,2)
(2,3)
(2,4)
(3,1)
(3,2)
(3,3)
(3,4)
Row 1
Row 3
Row 2
Column-Major Order Row-Major Order
![Page 38: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/38.jpg)
38
2D Array
• LOC(LA[K]) = Base(LA) + w(K -1)
• LOC(A[J,K]) of A[J,K]Column-Major Order
LOC(A[J,K]) = Base(A) + w[m(K-1) + (J-1)]
Row-Major OrderLOC(A[J,K]) = Base(A) + w[n(J-1) + (K-1)]
![Page 39: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/39.jpg)
39
2D Array Example
• Consider a 25 x 4 array A. Suppose the Base(A) = 200 and w =4. Suppose the programming store 2D array using row-major. Compute LOC(A[12,3])
• LOC(A[J,K]) = Base(A) + w[n(J-1) + (K-1)]
• LOC(A[12,3]) = 200 + 4[4(12-1) + (3 -1)]= 384
![Page 40: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/40.jpg)
40
Multidimensional Array
• An n-dimensional m1 x m2 x …. X mn array B is a collection of m1.m2…mn data elements in which each element is specified by a list of n integers – such as K1, K2, …., Kn – called subscript with the property that1≤K1≤m1, 1≤K2≤m2, …. 1≤Kn≤mn
The Element B with subscript K1, K2, …,Kn will be denoted by
BK1,K2, …,Kn or B[K1,K2,….,Kn]
![Page 41: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/41.jpg)
41
Multidimensional Array
• Let C be a n-dimensional array• Length Li of dimension i of C is the
number of elements in the index setLi = UB – LB + 1
• For a given subscript Ki, the effective index Ei of Li is the number of indices preceding Ki in the index set
Ei = Ki – LB
![Page 42: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/42.jpg)
42
Multidimensional Array
• Address LOC(C[K1,K2, …., Kn]) of an arbitrary element of C can be obtained asColumn-Major Order
Base( C) + w[((( … (ENLN-1 + EN-
1)LN-2) + ….. +E3)L2+E2)L1+E1]
Row-Major OrderBase( C) + w[(… ((E1L2 + E2)L3 +
E3)L4 + ….. +EN-1)LN +EN]
![Page 43: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/43.jpg)
43
Example
• MAZE(2:8, -4:1, 6:10)• Calculate the address of MAZE[5,-
1,8]• Given: Base(MAZE) = 200, w = 4,
MAZE is stored in Row-Major order • L1 = 8-2+1 = 7, L2 = 6, L3 = 5• E1 = 5 -2 = 3, E2 = 3, E3 = 2
![Page 44: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/44.jpg)
44
Example Contd ..
• Base( C) + w[(… ((E1L2 + E2)L3 + E3)L4 + ….. +EN-1)LN +EN]
• E1L2 = 3 . 6 = 18
• E1L2 + E2 = 18 + 3 = 21
• (E1L2 + E2)L3 = 21 . 5 = 105
• (E1L2+E2)L3 + E3 = 105 + 2 = 107
• MAZE[5,-1,8] = 200 + 4(107) = 200 + 248 = 628
![Page 45: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/45.jpg)
45
Pointer, Pointer Array
• Let DATA be any array• A variable P is called a pointer if P
points to an element in DATA i.e if P contains the address of an element in DATA
• An array PTR is called a pointer array if each element of PTR is a pointer
![Page 46: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/46.jpg)
46
Group 1
Group 2
Group 3
Group 4
Evan Conrad Davis Baker
Harris Felt Segal Cooper
Lewis Glass Ford
Shaw Hill Gray
Penn Jones
Silver Reed
Troy
Wagner
King
Two Dimensional 4x9 or 9x4 array
![Page 47: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/47.jpg)
47
1 Evan
2 Harris
3 Lewis
4 Shaw
5 Conrad
: :
13 Wagner
14 Davis
15 Segal
16 Baker
: :
21 Reed
Group 1
Group 2
Group 3
Group 4
![Page 48: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/48.jpg)
48
1 Evan
: :
4 Shaw
5 $$$
6 Conrad
: :
14 Wagner
15 $$$
16 Davis
17 Segal
18 $$$
19 Baker
: :
24 Reed
25 $$$
Group 1
Group 2
Group 3
Group 4
Group are not index in this representation
![Page 49: Lecture 3 data structures and algorithms](https://reader035.fdocuments.net/reader035/viewer/2022062419/557cfd96d8b42a89158b4efb/html5/thumbnails/49.jpg)
49
1 Evan
2 Harris
3 Lewis
4 Shaw
5 Conrad
: :
13
Wagner
14
Davis
15
Segal
16
Baker
: :
21
Reed
22
$$$
Group 1
Group 2
Group 3
Group 4
1 1
2 5
3 14
4 16
5 22
Group