Announcements
(1) Test 2
* Dec 01 (Thur) *
2:30pm 4:30pm
Venue: KB223
Topics:
* Everything up to and including Sorting by comparison;
emphasis on topics not in Test 1 *
(2) Programming assignment
- Any language as long as TAs know how to grade it.
- Deadl

Algorithms
An algorithm is similar to a computer program in
that it may specify essentially the same
computational steps as a specific program written in
a specific language such as Basic, Pascal, or C+.
Yet, the purpose of an algorithm is to communicat

Example
Attempt 1:
For each pixel d do
decide whether d should be on or off
Attempt 2:
For each pixel d with coordinates (x,y) cfw_
compute a function c = f(x,y)
if (c threshold) then
plot (d)
6

THE UNIVERSITY OF HONG KONG
FACULTY OF ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE
CSIS1119/COMP2119 Introduction to Data Structures and Algorithms
Date: 16 Dec, 2014
Time: 2:30pm - 5:30pm
Write Your University Number here and also on every page:
Please re

Chapter 5
Trees
1
A 2-dimensional structure
A linked list is a one dimensional data
structure each element is followed by
exactly one other element (except for the
last).
2
A 2-dimensional structure
We can extend the concept of linked list so
that an el

Chapter 5
Trees
1
A 2-dimensional structure
A linked list is a one dimensional data
structure each element is followed by
exactly one other element (except for the
last).
2
A 2-dimensional structure
We can extend the concept of linked list so
that an el

Chapter 6
Searching
1
The problem
Given a set of n elements, search for one
with a specific key.
We have studied 2 ways of searching: linear
search and hashing.
2
The problem
For linear search:
average/worst case complexity is O(n)
For hashing:
averag

Chapter 7
Binary Search Trees
1
Binary search on a list
Can we do a binary search on a linked list of
elements?
Recall that the principle of binary search is to
examine the middle element and use the result to
eliminate half of the elements from conside

Chapter 6
Searching
1
The problem
Given a set of n elements, search for one
with a specific key.
We have studied 2 ways of searching: linear
search and hashing.
2
The problem
For linear search:
average/worst case complexity is O(n)
For hashing:
averag

Chapter 8
Balanced Trees
1
Balancing a search tree
With binary search trees, average search time (with
random inserts and no deletes) is O(lg(n).
However, the worst case search time is O(n).
Observation:
If the search tree is not balanced, the tree coul

Chapter 8
Balanced Trees
1
Balancing a search tree
With binary search trees, average search time (with
random inserts and no deletes) is O(lg(n). However,
the worst case search time is still O(n).
Observation:
If the search tree is not balanced, the tre

Chapter 4
Hashing
The key is the address
1
The Dictionary ADT
A dictionary is a set data type that supports 3
operations: INSERT, DELETE, and SEARCH.
Which operation do you think is used most often?
A linked list can be used to implement a dictionary.
H

Example
Attempt 1:
For each pixel d do
decide whether d should be on or off
Attempt 2:
For each pixel d with coordinates (x,y) cfw_
compute a function c = f(x,y)
if (c threshold) then
plot (d)
6

Choosing an algorithm
Usually, we get to choose an algorithm to
solve a problem. How do we measure which
algorithm is better? We need to pick a
measure for
problem size, and
computational resources required by the
algorithm (expressed in terms of the pro

Programs
A program specifies in the exact syntax of some
programming language the computation one
expects a computer to perform. It specifies a finite
sequence of computer instructions which tells the
machine what to do exactly (fine details).
text file

Searching and Binary Search Tree (Outcome (2)
Review: lets revisit the searching problem.
Problem: Given a set of n elements and an element x, return the
location of the element x or report that such an element does not
exist in the set.
Sequential (Linea

Outcome (2): Another data structure - tree
Do you remember whats a graph G(V, E) ?
Tree
a
Edge
Cycle
c
b
1) Directed / Undirected graph g
d
f
e Node/vertex
h
2) A path from vi to vj: a sequence <vi, vi+1,. vj-1, vj> such
that (vk, vk+1) E (set of edges) f

Outcome (1) and (3): How to analyze recursive algorithms?
Will teach an algortihm design technique.
Recurrence, recursive function, divide-and-conquer approach
Review:
A function is recursive if it is defined in terms of itself.
Let n be positive integers

Balanced
AVL Tree
Binary Search
Tree
A binary search tree can perform the following
operations, Insert, Delete, Search, Minimum, Maximum,
Predecessor, Successor, in O(h) time where h is the
Review height of the tree
Aim: we want to achieve O(log n)
Worst

Outcome (2): A useful data structure - hashing
Application
You need to provide a system to store a set of student records
Name: Peter
Student No.: h02xxxxx
Age: 20
Year: 1
Curriculum: BEng(SE)
Hobbies: working on assignments, programming
Each record can b

Analysis of algorithms (Outcome (1)
Q: how good a given
Time
algorithm is?
- Not measure actual time (why?)
Recall
Two major concerns
Other issues (not our concern):
e.g. is it difficult to implement?
- Can be used to predict the
actual running time and f

Outcome (2): Introduction to data structures
An Introduction to Data Structure
Terms: Data Types, Data Structures, Abstract Data Type (ADT)
Data Types
- provided by a programming language
- a set of values + a set of operations on these values
- e.g. int

Algorithms
An algorithm is the (programming) language independent
and machine independent strategy that a program uses.
Algorithms are to programs like plots are to novels.
We can consider an algorithm to be a finite sequence of well
defined operations

Program = data structure + algorithm
A program thus consists of 2 parts:
Algorithm: the computational procedure which
transforms an input into the desired output as
specified by the problem statement.
Data Structures: the representation,
organization and