CS 234: Assignment 3 Solutions
Spring 2016, University of Waterloo
Problem 1. We use a Stack to traverse the tree non-recursively. The algorithm is shown below
in which T is a non-empty binary tree with n nodes.
Algorithm 1 NonRecursiveInOrder(T )
1:
2:
3
Wrapping Things Up
What Weve Learned about Programming
Languages
What Weve Learned about Data Structure
What Weve Learned about
Programming Languages
Object based programming: Organize
around objects.
Object oriented programming:
Abstraction.
Encaps
Shortest Paths
A
8
B
8
2
2013 Goodrich, Tamassia, Goldwasser
Shortest Paths
2
7
5
E
C
3
0
2
4
1
9
D
8
F
3
5
1
Weighted Graphs
In a weighted graph, each edge has an associated numerical
value, called the weight of the edge
Edge weights may represent, dist
Spanning Trees
Subgraph of a graph G = (V,E) is any graph that can be obtained by
removing edges and/or vertices from G
Spanning subgraph of G is a connected subgraph of G which contains
all vertices in G
Tree is a minimum connected acyclic graph
Span
Graphs
What do the following problems have in common?
Traveling Salesman Problem: Given a list of cities and
the distances between them, find the shortest tour that
visits all cities and returns to the start.
0
Graphs
What do the following problems have i
Searching
Data Structures and Algorithms Using Python
Searching
The process of selecting particular information
from a collection of data based on specific criteria.
Can be performed on different data structures.
sequence search
search key (or key)
compou
Sorting
Data Structures and Algorithms Using Python
Sorting
The process of arranging a collection of items
such that each item and its successor satisfy a
prescribed relationship.
Items arranged in ascending or descending order.
sequence sort
sort key
sor
Linked Lists
Data Structures and Algorithms Using Python
Building Linked Lists
There are more ways to build a linked list than
simply prepending nodes to the front.
Appending nodes
Sorted linked lists
Data Structures and Algorithms Using Python
Chapter 5:
Linked Structures
Data Structures and Algorithms Using Python
Review
Arrays
Basic sequence container
Provides easy and direct element access.
Supported at the hardware level.
Limited functionality.
Fixed size.
Data Structures and Algorithms Using Python
L
Analysis of Algorithms
Key Concepts
Asymptotic Complexity and Big-O Notation
The Complexity of Lists and Loops
Big-O Properties
Amortized Analysis
References
Chapter 4 in course text
Data Structures and Algorithms Using Python
CS234 Spring 2015
1
Algorith
Python Sets
Key Concepts
Python Set
Set Commands
Set ADT
Set ADT implementation
References
Chapter 3 in course text.
Note: diagrams taken directly from the course text
CS234 Spring 2015
1
Introduction
The Set is a common container used in programmin
Spanning Trees
Subgraph of a graph G = (V,E) is any graph that can be obtained by
removing edges and/or vertices from G
Spanning subgraph of G is a connected subgraph of G which contains
all vertices in G
Tree is a minimum connected acyclic graph
Span
Graphs
What do the following problems have in common?
Traveling Salesman Problem: Given a list of cities and
the distances between them, find the shortest tour that
visits all cities and returns to the start.
0
Graphs
What do the following problems have i
Splay Trees
6
v
8
3
z
4
2013 Goodrich, Tamassia, Goldwasser
Splay Trees
1
Splay Trees are Binary Search Trees
all the keys in the blue
region are 20
(20,Z)
note that two keys of
equal value may be wellseparated
(10,A)
(35,R)
BST Rules:
entries stored onl
CS 234 Assignment 4 Solutions
University of Waterloo
Spring 2016
Problem 1. AVL trees [5 marks].
Show the AVL tree that results after each of the integer keys 9, 27, 50, 15, 2, 21 and 36 are inserted, in that order, into an initially-empty AVL tree. Clear
Instructions
Answer all questions on this booklet.
You have 1 hour and 50 minutes to
complete this exam.
This exam has 25 marks.
No aids (textbooks, calculators, etc)
are allowed.
All solutions must be written in the
spaces provided.
Please DO NOT separat
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
D IJKSTRA ' S A LGORITHM D EMO
Algorithms
F O U R T H
E D I T I O N
R OBERT S EDGEWICK | K EVIN W AYNE
http:/algs4.cs.princeton.edu
Dijkstra's algorithm demo
Consider vertices in increasing order of distance f
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
K RUSKAL ' S A LGORITHM D EMO
Algorithms
F O U R T H
E D I T I O N
R OBERT S EDGEWICK | K EVIN W AYNE
http:/algs4.cs.princeton.edu
Kruskal's algorithm demo
Consider edges in ascending order of weight.
Add next
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
P RIM ' S A LGORITHM D EMO
Prim's algorithm
lazy implementation
Algorithms
F O U R T H
E D I T I O N
R OBERT S EDGEWICK | K EVIN W AYNE
http:/algs4.cs.princeton.edu
eager implementation
P RIM ' S A LGORITHM
Text Compression
Given a string X, efficiently encode X into a
smaller string Y
Saves memory and/or bandwidth
A good approach: Huffman encoding
Compute frequency f(c) for each character c.
Encode high-frequency characters with short code
words
No code wor
Prims/Jarniks Algorithm
Idea: Pick an arbitrary vertex and grow the minimum spanning tree
from there.
At each step, pick the vertex outside the spanning tree with the
minimum edge connecting it to the tree, and add it to the tree.
Implementation: Every
Recitation 2 Recurrences
Parallel and Sequential Data Structures and Algorithms, 15-210 (Fall 2013)
September 4, 2013
1
Announcements
HW1 is due on Monday, September 9. Hopefully you have all started by now; if not, now
would be a good time.
If you are
CS 234
Data Types and Structures
Spring 2015
Instructors:
Martin Derka, Safaa Bedawi
Slides based on notes and slides from L. Case, J.P. Pretti,
K. Lanctot with additions by M. Derka and S. Bedawi.
Many diagrams are taken from the course text, Data Struct
Growth-rate Functions
O(1) constant time, the time is independent of n,
e.g. array look-up
O(log n) logarithmic time, usually the log is base
2, e.g. binary search
O(n) linear time, e.g. linear search
O(n*log n) e.g. efficient sorting algorithms
O(n2) qua
Abstract Data Types
Key Concepts
Types, Data Types
Abstraction
Abstract Data Types
Preconditions, Postconditions
Bag ADT example
References
Chapter 1 and 2 in course text
Note: definitions taken directly from the course
text
CS234 Spring 2015
1
Typ
Round (#, nearest to round to)
/ integer returned
* exponent
int(3)
type(int(3) => class is int
int() not work for float numbers
if want it for a float number use
float(6.2) => 6.2
a= eval(input(please enter a number)
please enter a number5
type(a)
<class
"
CS 234 - Python Example 1
Illustrate simple types, expressions and console i/o
"
# This is a comment
#
# 1.1 Print Statements
#
#Old version of print statement
print "Hello world!"
#New version of print statement, has parenthesis
print("Hi!")
#Strings c
CS 234 Assignment 2
University of Waterloo
Spring 2016
Due Date: 1 July 2016 at 4:00PM
Please read the Submission Instructions at the end and follow them carefully.
Problem 1. Linked List Traversal [4 marks].
Suppose that we are given a singly linked list
CS 234 Assignment 3
University of Waterloo
Spring 2016
Due Date: 15 July 2016 at 4:00PM
Please read the Submission Instructions at the end and follow them carefully.
Problem 1. In-order Traversal [4 marks].
Recall the recursive algorithm for performing in
CS 234 Assignment 2 Solutions
University of Waterloo
Spring 2016
Problem 1. Linked List Traversal [4 marks].
(a) The linked list might be traversed more than once.
Traverse the linked list once to get its length and then increment the start of the list
b
CS 234 Assignment 1
University of Waterloo
Spring 2016
Due Date: June 10, 2016 at 4:00PM
Please see the Submission Instructions at the end and follow them carefully.
Problem 1. Order Notations [4 marks].
Suppose that f (n), g(n) and h(n) are all positive
CS 234 Assignment 4
University of Waterloo
Spring 2016
Due Date: July 29th at 4:00 PM
Please read the Submission Instructions at the end and follow them carefully.
Problem 1. AVL trees [5 marks].
Show the AVL tree that results after each of the integer ke