Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Welcome to CSL 201 – Data Structures
Narayanan (CK) C Krishnan Department of Computer Science and Engineering IIT Ropar
Administrative Trivia q Course Structure
n 3-0-4 (5 credits)
q Class Timings n Monday 9.55-10.45 n Tuesday 10.50-11.40 n Wednesday 11.45-12.35
q Lab hours n Thursday 9.00-12.35
q Teaching Assistant n Yayati Gupta
q Office Hours n Instructor – M,T,W
immediately after class n TA – during lab hours
q Course Google group – [email protected]
q Pre-registered students have already been added n other students – send
email to the instructor
q Pseudonym n 5 character n July 31, 5.00pm
2
Reference Material
3
Data Structures and Algorithms in Java Goodrich, Tamasia and Goldwasser 6th edition
Introduction to Algorithms Cormen, Leiserson, Rivest, Stein 3rd edition
Fundamentals of Data Structures Horowitz Sahni
Learning every week
4
Tentative Course Schedule
5
Quizzes – 30% q Almost every alternate
Thursday n 9.00-10.30am n L2
q Q3 and Q6 will also have programming problems
q Cover material discussed from the previous quiz till the current week
q Duration – 30-45m q Rest of the time –
problem solving/tutorial 6
Quiz Number Date
Q1 Aug 6
Q2 Aug 20
Q3 Sep 3
Q4 Oct 1
Q5 Oct 15
Q6 Oct 29
Q7 Nov 12
Labs – 20% q Due almost every
alternate Friday 11.55pm q Programming
Assignments q TA is available for any
assistance. q Java – programming
language n Introductory Tutorial -
July 30 9-10.30am in L2
Lab Number Date
L1 Aug 14
L2 Aug 28
L3 Sep 4
L4 Sep 24
L5 Oct 8
L6 Oct 22
L7 Nov 5
L8 Nov 19
7
Grading Scheme q Tentative Breakup
n Quizzes (6-7) 30% n Labs (6-8) – 20% n Mid-Semester Exam – 25% n End-Semester Exam – 25%
8
Passing Criteria: A student has to secure an overall score of 40(out of 100) and a combined score of 60(out of 200) in the exams to pass the course
Attendance – Bonus 1% q No mandatory attendance q Encourage you to attend classes q Attendance will be taken during every
class q 1% will count towards the final score
n Borderline grades will have an edge
9
Honor Code q Encourage to form study groups to review
class lectures/textbook material n Group discussions are encouraged
q Unless explicitly stated all quizzes and labs n are to be done individually. n web trawled code or code copied from classmates
is strictly forbidden.
q Any infraction will be dealt in severest terms n Direct ‘F’ grade in the course.
10
I reserve the right to question you, if I suspect any misconduct in any submission.
Course Website q http://cse.iitrpr.ac.in/ckn/courses/f2015/
csl201/csl201.html
q All class related material will be accessible from the course webpage.
11
Data Structures q What are data structures?
12
Data Structures q What are data structures?
n a way to organize data n easier access/manipulate to data
q Topics for the next couple of weeks n Concrete Data Structures
w Arrays and Linked Lists
n Algorithmic Analysis n Recursion
13
Arrays 14
Arrays
© 2014 Goodrich, Tamassia, Goldwasser
Java Example: Game Entries q A game entry stores the name of a player and her best score so far in a game
© 2014 Goodrich, Tamassia, Goldwasser Arrays 15
Java Example: Scoreboard
© 2014 Goodrich, Tamassia, Goldwasser Arrays 16
q Keep track of players and their best scores in an array, board n The elements of board are objects of class GameEntry
n Array board is sorted by score
Arrays 17
Array Definition q An array is a sequenced collection of
variables all of the same type. Each variable, or cell, in an array has an index, which uniquely refers to the value stored in that cell. The cells of an array, A, are numbered 0, 1, 2, and so on.
q Each value stored in an array is often called an element of that array.
A 0 1 2 n i
© 2014 Goodrich, Tamassia, Goldwasser
Arrays 18
Array Length and Capacity q Since the length of an array determines the
maximum number of things that can be stored in the array, we will sometimes refer to the length of an array as its capacity.
q In Java, the length of an array named a can be accessed using the syntax a.length. Thus, the cells of an array, a, are numbered 0, 1, 2, and so on, up through a.length−1, and the cell with index k can be accessed with syntax a[k].
a 0 1 2 n k
© 2014 Goodrich, Tamassia, Goldwasser
Declaring Arrays (first way) q The first way to create an array is to use an
assignment to a literal form when initially declaring the array, using a syntax as:
q The elementType can be any Java base type or class name, and arrayName can be any valid Java identifier. The initial values must be of the same type as the array.
© 2014 Goodrich, Tamassia, Goldwasser Arrays 19
Declaring Arrays (second way) q The second way to create an array is to use
the new operator. n However, because an array is not an instance of a
class, we do not use a typical constructor. Instead we use the syntax:
new elementType[length] q length is a positive integer denoting the length
of the new array. q The new operator returns a reference to the
new array, and typically this would be assigned to an array variable.
© 2014 Goodrich, Tamassia, Goldwasser Arrays 20
Arrays of Characters or Object References q An array can store primitive elements, such as
characters.
q An array can also store references to objects.
© 2014 Goodrich, Tamassia, Goldwasser Arrays 21
Arrays 22
Adding an Entry q To add an entry e into array board at index i, we
need to make room for it by shifting forward the n - i entries board[i], …, board[n – 1]
board 0 1 2 n i
board 0 1 2 n i
0 1 2 n e i
© 2014 Goodrich, Tamassia, Goldwasser
board
Java Example
© 2014 Goodrich, Tamassia, Goldwasser Arrays 23
Arrays 24
Removing an Entry q To remove the entry e at index i, we need to fill the hole
left by e by shifting backward the n - i - 1 elements board[i + 1], …, board[n – 1]
0 1 2 n i
0 1 2 n e i
0 1 2 n i
© 2014 Goodrich, Tamassia, Goldwasser
board
board
board
Java Example
© 2014 Goodrich, Tamassia, Goldwasser Arrays 25
Sorting an Array – Insertion Sort
26 Arrays
Code Fragment 3.6
Insertion Sort - Example
Arrays 27
insert
insert
insert
0
0
0
0
0
0
0
0
00
0 0
Done!
0
C E H G F
B C A E H G FD
B E H G FC D
A H G FB C D E
A FB C D E H
A G FB C D E H
E H G FDCBB E
C
F
F
G
H G FDC
A FB C D E H
C D E HG
BA
D
A B C D E G H A B C
A
G
H
E
A
D
D E HG
HGFED
A
C
B
B A
2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 71 2 3 4 5 6 7
1
1
2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7
cur
1 2 3 4 5 6 7
move movemove
no move
no move
no move
no move
move no move
move move no move
Figure 3.5
Drawbacks of Arrays q Fixed capacity q Insertions and Deletions involve many
shifting operations
Arrays 28
Singly Linked Lists 29
Singly Linked Lists
Singly Linked Lists 30
Singly Linked List q A singly linked list is a
concrete data structure consisting of a sequence of nodes, starting from a head pointer
q Each node stores n element n link to the next node
next
element node
A B C D
∅
head
© 2014 Goodrich, Tamassia, Goldwasser
Singly Linked Lists 31
A Nested Node Class
© 2014 Goodrich, Tamassia, Goldwasser
Singly Linked Lists 32
Accessor Methods
© 2014 Goodrich, Tamassia, Goldwasser
Singly Linked Lists 33
Inserting at the Head • Allocate new
node • Insert new
element • Have new
node point to old head
• Update head to point to new node
© 2014 Goodrich, Tamassia, Goldwasser
Inserting at the Tail • Allocate a new node
• Insert new element
• Have new node point to null
• Have old last node point to new node
• Update tail to point to new node
Singly Linked Lists 34 © 2014 Goodrich, Tamassia, Goldwasser
Singly Linked Lists 35
Java Methods
© 2014 Goodrich, Tamassia, Goldwasser
Removing at the Head
• Update head to point to next node in the list
• Allow garbage collector to reclaim the former first node
Singly Linked Lists 36 © 2014 Goodrich, Tamassia, Goldwasser
Singly Linked Lists 37
Java Method
© 2014 Goodrich, Tamassia, Goldwasser
Removing at the Tail
• Removing at the tail of a singly linked list is not efficient!
• There is no constant-time way to update the tail to point to the previous node
Singly Linked Lists 38 © 2014 Goodrich, Tamassia, Goldwasser
Doubly Linked Lists 39
Doubly Linked Lists
Doubly Linked Lists 40
Doubly Linked List q A doubly linked list can be traversed
forward and backward q Nodes store:
n element n link to the previous node n link to the next node
q Special trailer and header nodes
prev next
element
trailer header nodes/positions
elements
node
© 2014 Goodrich, Tamassia, Goldwasser
Doubly Linked Lists 41
Insertion q Insert a new node, q, between p and its successor.
A B X C
A B C
p
A B C
p
X
q
p q
© 2014 Goodrich, Tamassia, Goldwasser
Doubly Linked Lists 42
Deletion q Remove a node, p, from a doubly linked list.
A B C D
p
A B C
D
p
A B C © 2014 Goodrich, Tamassia, Goldwasser
Doubly-Linked List in Java
© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 43
Doubly-Linked List in Java, 2
© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 44
Doubly-Linked List in Java, 3
© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 45
Doubly-Linked List in Java, 4
© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 46