0% found this document useful (0 votes)
11 views52 pages

5 List

This lecture covers data structures, specifically focusing on lists and ArrayLists, including their operations and time complexity. It emphasizes the importance of understanding abstract data types (ADTs) and recursion in programming. The course aims to enhance coding skills and provide hands-on experience in implementing data structures.

Uploaded by

thirtythr33spam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views52 pages

5 List

This lecture covers data structures, specifically focusing on lists and ArrayLists, including their operations and time complexity. It emphasizes the importance of understanding abstract data types (ADTs) and recursion in programming. The course aims to enhance coding skills and provide hands-on experience in implementing data structures.

Uploaded by

thirtythr33spam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Data Structures

<Lecture 5: List>

Sungmin Cha
New York University

09.18.2024
Outline
• Notice

• Review the previous lecture

• List
– ArrayList
– Abstracted Data Type (ADT)
– Operations

2
Outline
• Notice

• Review the previous lecture

• List
– ArrayList
– Abstracted Data Type (ADT)
– Operations

3
Notice
• HW
– HW1 is posted to our Notion page!
 Deadline: 11:59PM 10/06(Sun)
– HW must be completed individually!
– Late submission
 A 50% penalty to the score
– I will upload a template code and make a submission slot in
Gradescope after this lecture

• Lab 2– Problems
– I uploaded the solution code and test cases of Problem 2 to Ed
Workspace
4
Notice
• Let’s use ED discussion more
– There are many similar Q&As by email
– Post your question to share the question and answer

• Late submission in Gradescope


– If you make a late submission, a 50% penalty will be automatically
applied.
– If your previous submission score is higher than the penalty-
adjusted score of the late submission, make sure to activate
the earlier submission in the submission history

5
Outline
• Notice

• Review the previous lecture

• List
– ArrayList
– Abstracted Data Type (ADT)

6
Time Complexity
• Asymptotic Running Time
– In other words, it is time complexity
– The primary method for expressing time complexity is Big-O
 𝚶(): asymptotic upper bound
 Ω(): asymptotic lower bound You will learn them
 Θ(): asymptotic tight bound during the Algorithm class

Figure from this link 7


Time Complexity
• Big-O notation considers only the highest-order term
– Why?

n 𝒏𝟐 𝒏𝟐 − 𝟐𝒏 𝒏𝟐 − 𝟐𝒏 + 𝟏
10 100 80 81
20 400 360 361
50 2,500 2,400 2,401
100 10,000 9,800 9,801
1000 1,000,000 998,000 998,001

As n become larger, the time complexity is


ultimately dominated by the highest-order term

8
Time Complexity
• What is the worst-case time complexity (Big-O)?
– [Group Discussion] Example 7:

– For the input size of n, what is the time complexity?

9
Recursion
• What is recursion?
– Recursion occurs when the definition of a concept or process
depends on a simpler or previous version of itself
 From Wikipedia

– Examples in the real world


: nesting dolls, fractals

10
Recursion
• What is recursion?
– Example: Factorial (n!)

𝑛! = 𝑛 × 𝑛 − 1 × 𝑛 − 2 × 𝑛 − 3 × 𝑛 − 4 × … × 2 × 1

(𝑛 − 1)!

A factorial (n) includes the factorial (n-1)

 Let 𝑓 𝑛 is 𝑛!, 𝑓 𝑛 is defined as below

𝑛 × 𝑓 𝑛−1 … 𝑛≥1
𝑓 𝑛 =ቊ
1 … 𝑛=1
11
Recursive Function
• Recursion in programming language (JAVA)
– Recursive function refers to a function that calls itself within
its own function

 When a recursion function is called, how does the layout of the stack
memory change?

12
Recursive Function
• Understanding the flow of a recursive function
– When a recursive function is called, a copy of the recursive
function is created, and the copy is then executed
– When a recursive function returns, the return value is sent back
to the position where the recursive function was called in the
previous function
 Then, the subsequent statements are executed

13
Recursive Function
• The exit condition in recursion
– Setting the exit condition well is important

– Why?
 If there is no proper exit condition, the function calls functions infinitely
o stack overflow
14
Outline
• Notice

• Review the previous lecture

• List
– ArrayList
– Abstracted Data Type (ADT)
– Operations

15
Overview of this course
• Two parts of this course
1. Preliminary (1-2 weeks)
 JAVA programming
 Algorithm complexity and recursion.

2. Data Structures (12 weeks)


 Linear Data Structures (e.g., Array, List, Stack, etc.)
 Basic sorting algorithms
 Non-linear Data Structures (e.g., Tree and Graph, etc.)

16
Goal

• The goals of this lecture


1. Gain a deep understanding of data structures.
 Understand their characteristics, operations for maintenance, time
complexity of them, and their pros and cons.
2. Improve coding skills through implementing these data
structures.
3. Provide hands-on experience in solving programming problems
using each data structure.

• Evaluation
– Assignments and labs (recitation) assess these goals
– The goal of the exam is primarily to assess the achievement of
the first goal! 17
List
• Let’s consider we implement a simple program
– Program to store and manage information of 𝑘 users
1. (Integer) Age of 𝑘 individuals
2. (Float) Height of 𝑘 individuals
3. (String) Name of 𝑘 individuals

• How can we efficiently store and manage those data?


– Using 𝑘 single variables
: inefficient and difficult to manage these data
– We can use an Array as a List!

18
ArrayList
• Using an Array as a List
– Arrays in JAVA are always stored on the heap in consecutive
memory locations
– Example:
Allocate object on the heap Size = 4byte * 10

address 100 104 108 112 … 136

0 0 0 0 0 0 0 0 0 0 heap

array

stack 100
19
ArrayList
• The memory map of Array Heap memory
1bit
– Example:


0x12AB85
4byte

Stack memory 4byte


….
4byte
ages = 0x12AB85


20
ArrayList
• The memory map of Array Heap memory
1bit
– Example:


0x12AB85
15(binary) 4byte

Stack memory 4byte


….
4byte
ages = 0x12AB85


21
ArrayList
• The memory map of Array Heap memory
1bit
– Example:


0x12AB85
15(binary) 4byte

Stack memory 21(binary) 4byte


….
4byte
ages = 0x12AB85


22
ArrayList
• The memory map of Array Heap memory
1bit
– Example:


0x12AB85
15(binary) 4byte

Stack memory 21(binary) 4byte


….
ages = 0x12AB85 33(binary) 4byte


23
ArrayList
• What operations are necessary to maintain and manage
this list?
– Add a value to the specific location
– Remove a value in the i-th location
– Get a value in the i-th location
– Find a value
– …, etc.
How can we effectively define and specify
these operations for implementation?

Using Abstract Data Type (ADT)

24
ADT
• 1) ADT design for ArrayList
– Example:

– ADT expresses the operations without specifying the details


of how those operations are implemented
 It focuses on the operations
 There is no strict format for designing ADT
25
ADT
• 2) Designing JAVA interface based on ADT

– Define an interface of ADT ‘list’


– Any class adhering to the ListInterface must:
 Provide the three specified operations listed here
 Ensure that the return types, parameter counts, and types for each
operation conform to this definition
26
ADT
• 3) Designing JAVA Class based on Interface

– Implement each operation


 Our role!
 Define variables and functions to implement the operations
– The implemented operations must follow the specified
information in ListInterface

27
ADT
• In the user program

– Users can utilize the three operations provided by the


ArrayList without needing to know the implementation
– In conclusion, ADT enables the usage of objects of that type at
an abstract level

28
ADT
• From ADT to the user program
1. Designing ADT
: First, we will design ADT of each data structure
2. Designing JAVA Interface
: we will define JAVA interface of each data structure
(e.g., instructions of homework)
3. Implementing Class
: we will go over the implementation details for some
operations, but the actual coding will be assigned as HW
4. User program

29
ADT of ArrayList
• Designing ADT for ArrayList

– We consider that 10 operations should be implemented to


manage an array as a list
– We are considering implementing ArrayList that can
accommodate various data types such as integers, floats,
and more.
 Implement ArrayList using Generics in JAVA (Intro to Generics [Video])
30
Add()
• Implementation of add(k, x) function
– Example: before and after applying add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

31
Add()
• Implementation of add(k, x) function
– Example: before and after applying add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

Here
32
Add()
• Implementation of add(k, x) function
– Example: before and after applying add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
Move one position to the right
from the rightmost element
Here
33
Add()
• Implementation of add(k, x) function
– Example: before and after applying add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
Move one position to the right
from the rightmost element
Here
34
Add()
• Implementation of add(k, x) function
– Example: before and after applying add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
Move one position to the right
from the rightmost element
Here
35
Add()
• Implementation of add(k, x) function
– Example: before and after applying add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

add(2, 34)
numItems = 5
item[0] Insert 34
item[ ] 23 12 34 59 32 9

Here
36
Add()
• Implementation of add(k, x) function
– Example: before and after applying add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

Update numItems
add(2, 34)
numItems = 6
item[0]
item[ ] 23 12 34 59 32 9

Here
37
Add()
• [Group Discussion] Defensive programming
– Considering the exception case of add(k,x), what is the general
form of add(k,x)?

38
Add()
• [Group Discussion] Defensive programming
– Considering the exception case of add(k,x), what is the general
form of add(k,x)?

For defensive programming, we always


consider the exception case!

39
Add()
• [Group Discussion] Time complexity
– What is the worst-case time complexity (Big-O)?
 Big-O: asymptotic upper bound

– What is the worst case of add(k,x)?

40
Append()
• Implementation of append(x) function
– Example: before and after applying append(24)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

41
Append()
• Implementation of append(x) function
– Example: before and after applying append(24)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

append(24)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

42
Append()
• Implementation of append(x) function
– Example: before and after applying append(24)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

append(24)
numItems = 5
item[0]
item[ ] 23 12 59 32 9 24

Add 24 to item[numItems]
43
Append()
• Implementation of append(x) function
– Example: before and after applying append(24)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

Update numItems
append(24)
numItems = 6
item[0]
item[ ] 23 12 59 32 9 24

44
Append()
• Defensive programming
– Considering the exception case of append(x), what is the
general form of append(x)?

• Time complexity of append(x)

45
Remove()
• [Group Discussion] Implementation of remove(k)
– Example: before and after applying remove(2)
numItems = 5
item[0]
item[ ] 23 12 59 32 9

46
Remove()
• Defensive programming
– Considering the exception case of remove(k), what is the
general form of remove(k)?

47
Remove()
• Time complexity
– What is the worst-case time complexity (Big-O)?
 Big-O: asymptotic upper bound

– What is the worst case of remove(k)?


 Remove a value at the first element (remove(0))

48
Other Operations
• Other operations
– ListInterface of ArrayList

Already introduce
operations, time complexity,
and exceptions

Homework
: Implement operations
using JAVA, what is the
time complexity?

49
ArrayList
• Advantages and disadvantages of ArrayList
– Advantages
1. Simple to implement
2. Data referencing is convenient: it allows quick access anywhere based
on the index value

– Disadvantages
1. The length of the array must be determined initially; if it needs to be
changed, a new allocation of the array is necessary
2. During the process of adding or deleting data, frequent data
movement (copying) occurs

50
ArrayList
• Discussion on the first disadvantage Heap memory
– Can we expand the array? 1bit


0x12AB85
15(binary) 4byte

Stack memory 21(binary) 4byte


….
ages = 0x12AB85 33(binary) 4byte

51
Thank you!

E-mail: [email protected]

52

You might also like