ADTs, Data Structures, Recursion
ADTs, Data Structures, Recursion
➢ Some ADTs
o Stacks, queues, linked lists, trees, graphs,
hash tables
int index = 1;
index = i + 1;
value = array[i];
System.out.println(index);
}
Algorithms CASE STUDY (Character Sequence ADT)
➢ A computational problem is solved by performing Stage 1 – Specification
finite set of instructions called an algorithm. ➢ Data:
➢ Well-defined procedure that processes input values o Sequence of characters – the length is from
to generate desired output values 0 up to 255 characters.
➢ It implements operations using data structures ➢ Operations
➢ Abu Ja’Far Muhammad lbn Musa Al-Khwarizmi o toString – function that returns the
o A mathematician and an astronomer conversation of the sequence to string
o Wrote the books (discovered by Europeans) o isFull – functions that returns true if the
that are containing the procedures to characters sequence is full.
perform math operations o Append – subroutine that adds a character
o Al-Khwarizmi -> Algorithm at the end of the sequence, unless it is
➢ Describe process precisely and concisely already full.
o Using natural language (ex. English)
▪ This process can be ambiguous. Stage 2 – Representation
o Using programming language (ex. Java, C,
C++)
▪ This process can be too technical and
very hard to read.
o Using Pseudocode – combination of NL and Stage 3 - Implementation
PL
▪ Recommended process; this allows
narrative but well-defined description
of steps.
➢ Ex ADT:
o Path of points in 3-D
o Measure total path length
➢ In C:
Jamboard Link:
https://jamboard.google.com/d/1sgBFVWqK3Hf4Nm_E
YzXbKoApJzIcqUzSLgQ42Nm8wPk/viewer?pli=1&f=1
Iteration vs Recursion
➢ Ways to repeatedly execute a set of instructions in
an algorithm
➢ Basic statements used in algorithms: decorations, Recursive Definition 2
input-output statements, assignments, conditional
and looping statements.
Iteration
➢ An execution of a loop’s body
➢ Iterating a block of statements (looping once)
➢ An algorithm that solves a problem using iteration is
called an iterative algorithm
Recursion
➢ If a chain of procedure contains the same procedure
at least twice, the algo is said to be recursive
➢ A programming technique in which a function calls
themselves; process of solving a problem by reducing
it into smaller versions of itself.
➢ Solves the problem by breaking it into smaller Recursive Definition 3
instances or parts of a problem
➢ It is similar to the ff:
o Mathematical induction
o Recursive algorithm
o Recurrence relations
o Recursive definitions
➢ 2 elements
o Basis – gives direct definition, proof or
solution to the smallest part
o Recursive step – solves a problem in terms of
by defining, proving, or solving something in
terms of the smaller version of itself; it is
applied to the smaller version. Tower of Hanoi
Recursive definition 1
Recursion Pros and Cons
• Alternate solution to a problem of a size n
• State recursive step?
• State basis?
• Con – translating to algorithm
• Pros – follows logic, easy to look at than iteration
• Con – less efficient, because it has redundant
calculations
• Conversion to iteration?