Agenda' Data Structures and Algorithms. Floa+ng'Point'Numbers' Floa+ng'Point'Numbers'

Similar documents
CS 240 Data Structure Spring 2018 Exam I 03/01/2018

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Chapter 17 - Notes Recursion

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Functions and Recursion

Tree Travsersals and BST Iterators

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

CMSC 202 Midterm Exam 1 Fall 2015

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Data Structures Lab II. Binary Search Tree implementation

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Week 3: Pointers (Part 2)

2. (5 Points) When calculating a combination (n choose k) the recursive definition is n k =1 if

CS 61BL Midterm 2 Review Session. Chris Gioia, Patrick Lutz, Ralph Arroyo, Sarah Kim

Discussion 2C Notes (Week 5, February 4) TA: Brian Choi Section Webpage:

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Physics 234: Computational Physics

BENG (HONS) ELECTRONIC ENGINEERING BENG (HONS) TELECOMMUNICATIONS. Cohort: BEE/10A/FT, BEE/10B/FT &BTEL/10B/FT

Getting started with C++ (Part 2)

CS490: Problem Solving in Computer Science Lecture 3: Input/Output

Week 3: File I/O and Formatting 3.7 Formatting Output

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

CSCE 2014 Final Exam Spring Version A

Binary Search Trees. EECS 214, Fall 2018

CMPS 221 Sample Final

Lab Instructor : Jean Lai

Name. Classes and Objects 1. We re going to develop a class to help out Café Below is the definition for a class called

CS 206 Introduction to Computer Science II

WARM UP LESSONS BARE BASICS

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Section 05: Midterm Review

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

MTH 307/417/515 Final Exam Solutions

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Program Organization and Comments

CS 315 Data Structures mid-term 2

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Solution to CSE 250 Final Exam

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

a. a * c - 10 = b. a % b + (a * d) + 7 =

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

UNIVERSITY REGULATIONS

Discussion 2C Notes (Week 5, February 4) TA: Brian Choi Section Webpage:

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Recursion. ! When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function.

Binary Tree Applications

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

I/O Streams and Standard I/O Devices (cont d.)

BSc (Hons) Computer Science. with Network Security. Examinations for / Semester1

PROGRAMMING EXAMPLE: Checking Account Balance

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

CS302 - Data Structures using C++

Ch 6. Functions. Example: function calls function

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

C++ Input/Output: Streams

Honors Computer Science C++ Mr. Clausen Program 4A, 4B, 4C (4D, 4G)

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Why Is Repetition Needed?

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

EE 109 Lab 8a Conversion Experience

Pointers. Variable Declaration. Chapter 10

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

University of KwaZulu-Natal Pietermaritzburg Campus Examinations

Introduction to C++ (Extensions to C)

Binary Search Trees Part Two

To become familiar with array manipulation, searching, and sorting.

CSE 333 Lecture 9 - intro to C++

C++ Scope Resolution Operator ::

PIC 10A. Review for Midterm I

CSE 250: Data Structures Fall 2015 Sample Final Exam

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Chapter 10 - Notes Applications of Arrays

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

Part I: Short Answer (12 questions, 65 points total)

Unit 14. Passing Arrays & C++ Strings

lecture09: Linked Lists

EC 413 Computer Organization

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

University of Dublin

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

INFO-I308 Laboratory Session #2

CSC102 INTRO TO PROGRAMMING WITH PYTHON LECTURE 27 CRASH COURSE IN C++ MICHAEL GROSSBERG

As an example using arrays, let s write some code to get started with the Upthrust game. We can use a 2D array to represent the game board.

Sample Code: OUTPUT Daily Highs & Lows

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

UEE1303(1070) S12: Object-Oriented Programming Template

CSCI 104 Runtime Complexity. Mark Redekopp David Kempe

C Functions. Object created and destroyed within its block auto: default for local variables

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Arrays. Week 4. Assylbek Jumagaliyev

DELHI PUBLIC SCHOOL TAPI

Transcription:

" Data Structures and Algorithms Agenda' Floa%ng(Point(Numbers( Hashing( Recurrence(Rela%ons( Binary(Search(Tree( Discussion 3: Week of Sep 21, 2011 1 2 Floa+ng'Point'Numbers'!double d1 = 1.1234;"!double d2 = 2.1234;"!cout << d2 - d1 << endl;"!// 1 or 1.0000?( Floa+ng'Point'Numbers'!double d1 = 1.1234;"!double d2 = 2.1234;"!cout << d2 - d1 << endl;"!// outputs 1"!printf( %.4f\n,d2 - d1);"!// outputs 1.0000( 3 4

Floa+ng'Point'Numbers' #include <iostream>! #include <iomanip>! using namespace std;! int main()! {!!double d1 = 1.1234;!!double d2 = 2.1234;!!cout << setprecision (3);!!cout << d1 << endl;!!cout << d2 << endl;!!cout << setprecision (9) << d1 << endl;!!cout << fixed;!!cout << setprecision (3) << d1 << endl;!!cout << setprecision (9) << d1 << endl;!!return 0;! }! Floa+ng'Point'Numbers' (!if (1 == 1.0000) {"!!cout << "Equal" << endl;"!}"!else {"!!cout << "Not equal" << endl;"!}" 5 6 Floa+ng'Point'Numbers' (!if (1 == 1.0000000000000001) {"!!cout << "Equal" << endl;"!}"!else {"!!cout << "Not equal" << endl;"!}" Float'Limita+on' Float'value' Hex' Decimal' 1.99999976(( 0x3FFFFFFE( 1073741822( 1.99999988( 0x3FFFFFFF( 1073741823( 2.00000000( 0x4000000( 1073741824( 2.00000024( 0x4000001( 1073741825( 2.00000048( 0x4000002( 1073741826( To(store(values(between(1.99999988(and(2,( you(need(to(either(use(a(double(or(parse(the( input(as(characters(and(use(a(type(that(has( enough(bits(to(fit( 7 8

Hashing' Associa%ve(container( No(concept(of( previous/next ( Insert/delete/lookup(in(O(1)(%me( Separate Chaining Hash(a(key(into(index,(store(the(value(into( hash_map[index]( Collision(resolu%on( Separate(chaining(( Probing/open(addressing( linear( quadra%c( 9 10 Open addressing 11 12

Complexity?(Size(of(hash(table(=(N( 13 14 15 16

17 18 How(about({(return(abs(value);(}(?( How(about({(return(abs(value);(}(?( 19 20

How(about({(return(abs(value);(}(?( Does(not(work(for((i2^31),(can t(store(2^31(((>(int_max)( 21 How(about({(return(abs(value);(}(?( Does(not(work(for((i2^31),(can t(store(2^31(((>(int_max)( More(importantly,(input(distribu%on(decides(output(distribu%on( 22 How(about({(return(abs(value);(}(?( Does(not(work(for((i2^31),(can t(store(2^31(((>(int_max)( More(importantly,(input(distribu%on(decides(output(distribu%on( {(return(abs(value(%(large_prime);(}( 23 Recurrence'Rela+ons' Usually(used(to(analyze(algorithm(run%mes.(( Many(algorithms(loop(on(a(problem(set,(do(something(each( %me,(and(create(smaller(subiproblems(to(solve.(this(is(the( idea(behind(a(recurrence(rela%on.(( To(solve(them( (think!(( How(is(this(problem(set(changing(each(%me?(Decreasing( exponen%ally?(variably?(constantly?(( How(many(new(subiproblems(are(being(created(each(%me?(( 24

Recurrence'Rela+ons' Recurrence(rela%ons(are(those(that(are(defined(in(terms(of( themselves.( Recurrence'Rela+ons' Recurrence(rela%ons(are(those(that(are(defined(in(terms(of( themselves.( S(0) = 0 S(n) = n + S(n - 1) S(0) = 0 S(n) = n + S(n - 1) What s(the(closed(form(of(s(n)?( What s(the(closed(form(of(s(n)?( S(n) = n + (n-1) + + 1 + 0 S(n) = n * (n - 1)/2( 25 26 Master'Theorem''' Suppose( (T(n)(=(a(*(T(n/c)(+(f(n)(( where((a>=1,(c>1(and(n/c(means(either(!n/c"(or(#n/c$( Then( 1)(If(f(n)=O(n^(log c aiε))(for(some(ε>0,(then( T(n)=Θ(n^log c a)( 2)(If(f(n)=Θ(n^log c a),(then(t(n)=θ((n^log c a)logn)( 3)(If(f(n)=Ω(n^(log c a+ε))(for(some(ε>0(and(if( a*f(n/c) kf(n)(for(some(constant(k<1(and(all( sufficiently(large(n,(then(t(n)=θ (f(n))( ( 27 Recurrence'Rela+ons' How would we express the Fibonacci sequence as a recurrence relation? What about the tribonacci sequence? 28

Recurrence'Rela+ons' How would we express the Fibonacci sequence as a recurrence relation? Recursive'Func+ons' We can use the following method to define a function with the natural numbers as its domain: F(0)=0,(F(1)=1,(F(n)=F(ni1)(+(F(ni2)( What about the tribonacci sequence? (((((((T(0)=1,(T(1)=1,(T(2)=2,(T(n)=T(ni1)+T(ni2)+T(ni3) 1. Specify the value of the function at zero. 2. Give a rule for finding its value at any integer from its values at smaller integers. Such a definition is called recursive. 29 30 Recursive'Func+ons' Example: f(0) = 3 f(n + 1) = 2f(n) + 3 f(0) = 3 f(1) = 2f(0) + 3 = 2 3 + 3 = 9 f(2) = 2f(1) + 3 = 2 9 + 3 = 21 f(3) = 2f(2) + 3 = 2 21 + 3 = 45 f(4) = 2f(3) + 3 = 2 45 + 3 = 93 Recursive'Func+ons' How can we recursively define the factorial function f(n) = n!? f(0) = 1 f(n + 1) = (n + 1)f(n) f(0) = 1 f(1) = 1f(0) = 1 1 = 1 f(2) = 2f(1) = 2 1 = 2 f(3) = 3f(2) = 3 2 = 6 f(4) = 4f(3) = 4 6 = 24 31 32

Recursive Function Iterative version of factorial function Function does NOT calls itself 33 34 BST' Binary(Tree( Every(node(has(0,(1,(2(children( Full(Binary(Tree( Every(node(other(than(leaves(has(2(children( All(leave(nodes(have(same(path(length( Also(called(proper(binary(tree,(strictly(binary(tree( Complete(Binary(Tree( Every(level(above(the(last(level(is(completely(filled( Nodes(in(the(last(level(are(as(far(lew(as(possible( BST' (Node({( (Key;( (Value;( }( (Node*(lew;( (Node*(right;( ( all(keys(in(lew(subtree(<(current(key(<(all(keys(in(right(subtree(( Binary(Search(Tree(( Ordered(binary(tree( 35 36

BST' BST' Search( Insert( Delete( Average'Case' Worst'Case' Average'Case' Worst'Case' Search( O(log(n)( O(n)( Insert( O(log(n)( O(n)( Delete( O(log(n)( O(n)( 37 38 Inser+ng'a'node'to'BST' Inser+ng'a'node'to'BST' void insert(node* &root, key, value)! {!!if (root == NULL) {!!!root = new node(key,value);!!else if (key < root->key) {!!!insert(root->left,key,value);!!else {!!!insert(root->right,key,value);! }" "!!! void insert(node* &root, key, value)! {!!if (root == NULL) {!!!root = new node(key,value);!!else if (key < root->key) {!!!insert(root->left,key,value);!!else {!!!insert(root->right,key,value);! }" " Can!you!write!it!as!a!non.recursion?!!!! 39 40

Inser+ng'a'node'to'BST' void insert(node* &root, key, value)! {!!if (root == NULL) {!!!root = new node(key, value);!!!return;!!node* cur = root;!!while (true) {!!!if (key < cur->key) {!!!!if (cur->left == NULL) {!!!!!cur->left = new node(key, value);!!!!!break;!!!!!!else!!!!!cur = cur->left;!!!!else {!!!!if (cur->right == NULL) {!!!!!cur->right = new node(key, value);!!!!!break;!!!!!!else!!!!!cur = cur->right;!! }! 41 Delete'a'node'from'BST' Dele+ng'a'leaf'(node'with'no'children):(Dele%ng( a(leaf(is(easy,(as(we(can(simply(remove(it(from( the(tree.( Dele+ng'a'node'with'one'child:(Remove(the( node(and(replace(it(with(its(child.( Dele+ng'a'node'with'two'children:(Call(the(node( to(be(deleted(n.(do(not(delete(n.(instead,(choose( either(its(iniorder(successor(node(or(its( predecessor(node,(r.(replace(the(value(of(n(with( the(value(of(r(and(then(delete(r.((why(r(cannot( have(more(than(2(children?)( 42