Review Problems. What is the Big O? i <- N j <- 1 loop exitif(i <= 0) loop exitif(j > M) j <- j + 1 endloop i < i - 1 endloop.

Slides:



Advertisements
Similar presentations
Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree.
Advertisements

Linked List: Traversal Insertion Deletion. Linked List Traversal LB.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Building a Linked List in Java. Linked List In the Procedural Paradigm a linked list consisted of: –A pointer to the head of the list –Nodes (in dynamic.
CS252: Systems Programming Ninghui Li Program Interview Questions.
Reference Details (copying, comparing). References You were introduced to the concept of pointers earlier this term. The pointer capabilities in pseudocode.
Lower bound for sorting, radix sort COMP171 Fall 2005.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
Sorting Algorithms Bubble Sort Merge Sort Quick Sort Randomized Quick Sort.
Preliminaries Multiway trees have nodes with greater than two children. Multiway trees of order k have nodes with most k children Trees –For all.
Advanced Recursion Design by Contract Data Abstraction.
Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules.
Binary Trees – Part I CS 367 – Introduction to Data Structures.
Generic Classes Use Cases Inheritance. Generic Classes.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Announcements NP-Complete Problems Decidable vs. Undecidable Problems Review.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Pointers OVERVIEW.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
Analysis of Bubble Sort and Loop Invariant
Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.
Announcements Review problems Review Outline. Online Survey
Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.
Review Records Dynamic Memory and Pointers Introduction to Linked Lists.
Introduction to Classes and Objects. Real Life When a design engineer needs an electrical motor he doesn’t need to worry about –How a foundry will cast.
Internal Sorting File Sorting Part 2.
Binary Search Trees (BST)
Graphs Arrays Iteration Combining Data Structures.
Internal and External Sorting External Searching
Linked Lists. Array List Issues Painful insert/remove at start/middle.
1 Lecture 18: Selection Sort, Quicksort & Merge Sort Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
HeapSort 25 March HeapSort Heaps or priority queues provide a means of sorting: 1.Construct a heap, 2.Add each item to it (maintaining the heap.
Introduction to Recursion. Recursion Defined A procedure or function which calls itself. Powerful mechanism for repetition. Makes algorithms more compact.
Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.
CS Algorithm Development Modularity. Objectives Do “high-level design” Practice abstraction by decomposing problems into their sub-tasks Define.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.
Recursion Damian Gordon. Recursion Recursion: Factorial Recursion is a feature of modularisation, when a module can call itself to complete a solution.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Linked List :: Basic Concepts
Searching – Linear and Binary Searches
Sorting.
Data Structure Dr. Mohamed Khafagy.
Problems with dynamic arrays
Lecture 14 Traversals of Linked Lists Preorder BST Traversal Postorder BST Traversal Miscellaneous BST Traversals Depth First vs Breadth First Array Traversals.
Linked Lists.
Simple Dynamic Data (Linked Lists)
Data Structures Balanced Trees CSCI
Computer Science 2 Outline/Learning Objectives for the day:
Lecture 29 Heaps Chapter 12 of textbook Concept of heaps Binary heaps
Lecture 16 Bubble Sort Merge Sort.
Computer Science 2: Trees
Introduction to Recursion
Linked List Functions.
Linked Lists.
Binary Search Trees CS 580U Fall 17.
Presentation transcript:

Review Problems

What is the Big O? i <- N j <- 1 loop exitif(i <= 0) loop exitif(j > M) j <- j + 1 endloop i < i - 1 endloop

Review problems Circle and Identify the 3 parts of recursion: Function Fact returnsa Num(N iot in Num) if(N = 0) then Fact returns 1 else Fact returns N * Fact(N - 1) endif endfunction // Fact

Review problems Circle and Identify the 3 parts of recursion: Function Fact returnsa Num(N iot in Num) if(N = 0) then Fact returns 1 else Fact returns N * Fact(N - 1) endif endfunction // Fact Check for termination Call self Move one step closer

Review Problems Recall that a leaf is a node in a binary tree with no children. Write a module that when passed a pointer to a binary tree will return the number of leaves. The module should use recursion

Leaves Function Leaves returnsa Num (current iot Ptr toa TNode) if(current = NIL) then Leaves returns 0 elseif(current^.left = NIL AND current^.right = NIL) then Leaves returns 1 else Leaves returns Leaves(current^.right) + Leaves(current^.left) endif endfunction // Leaves

Review Problems How many “time chunks” will be required to run this algorithm on 2 processors? S1 S4 S2 S5 S8 S3 S6 S9 S7 IIIIII

Review Problems S1 S4 S2 S5 S8 S3 S6 S9 S7 III

Review Problems Write a module to convert an unsorted linked list to a sorted linked list. Use data structure conversion as opposed to a sort algorithm such as Bubble Sort or Merge Sort

Solution Recall that for this type problem you will typically need three modules: –A standard linked list AddInOrder module –A modified linked list traversal module modified to keep track of pointer to new list –A startup module

Review problems Algorithm Pain a,b,c iot Char a <- ‘b’ b <- ‘c’ c <- ‘a’ Agony(c,a,’b’) print(a,c,b) b <- funky(a,c) print(a,b,c) endalgorithm Procedure Agony(a iot in/out Char, b iot out Char, c iot in Char) t iot Char if(c = ‘c’) then c <- ‘d’ t <- b b <- a a <- t else b <- a a <- ‘b’ endif endprocedure Function funky returnsa Char (x,y isoftype in Char) if(x = y) then funky returns ‘a’ else finky returns ‘b’ endif endfunction

Review problems Write a vector class It should be generic and support (at least) the following methods in the public section AddToEnd AddAt(nth) Remove(nth) Size Get(nth)

class Vector(DT) public Procedure AddToEnd(din iot in DT) // PPP Procedure AddAt(nth iot in Num, din iot in DT) // PPP Procedure Remove(nth iot in Num) // PPP Function Size returnsa Num() // PPP Function Get returnsa DT(nth iot in Num) // PPP Procedure Initialize() // PPP

protected Node definesa record data iot DT next iot Ptr toa Node endrecord head isoftype Ptr toa Node count isoftype Num Procedure AddToEnd(din iot in DT) AddToEndHelper(head, din) endprocedure // AddToEnd

Procedure AddToEndHelper (cur iot in/out Ptr toa Node, din iot in DT) // PPP if(cur = NIL) then cur = new(Node) cur^.data <- din cur^.next <- NIL count <- count + 1 else AddToEndHelper(cur^.next, din) endif endprocedure // AddToEndHelper Procedure AddAt(nth iot in Num, din iot in DT) AddAtHelper(head, nth, din, 1) endprocedure // AddAt

Procedure AddAtHelper(cur iot in/out Ptr toa Node, nth iot in Num, din iot in DT,kount iot in Num) // PPP temp iot Ptr toa Node if(cur = NIL OR nth = kount) then temp <- new(Node) temp^.data <- din temp^.next <- cur cur <- temp count <- count + 1 else AddAtHelper(cur^.next, nth, din, kount + 1) endif endprocedure // AddAtHelper

Procedure Remove(nth iot in Num) RemoveHelper(head, nth, 1) endprocedure // Remove Procedure RemoveHelper(cur iot in/out Ptr toa Node, nth iot in Num, kount iot in Num) // PPP if(cur <> NIL) then if(nth = kount) then cur <- cur^.next count <- count - 1 else RemoveHelper(cur^.next, nth, kount + 1) endif endprocedure // RemoveHelper

Function Size returnsa Num() Size returns count endfunction // Size Function Get returnsa DT(nth iot in Num) Get returns GetHelper(head, nth, kount) endfunction Function GetHelper returnsa DT (cur iot in Ptr toa Node, nth, kount iot in Num) // Precon: User must not request item > Size ** // PP if(nth = kount) then GetHelper returns cur^.data else GetHelper returns GetHelper (cur^.next, nth, kount + 1) endif endfunction // GetHelper

Procedure Initialize() head <- NIL count <- 0 endprocedure // Initialize endclass // Vector

Review problems Use the generic Vector class you just wrote to write a baseball roster program. It should manage baseball player records consisting of –Name –Position It should support the following operations –Add a player –Remove a player –Add a player at position N –Print a roster (only if there are 9 players otherwise print an error message) Assume that the record is named Player Assume that you have modules called –Procedure GetPlayer(data isoftype out Player) –Procedure PrintPlayer(data isoftype in Player)

Procedure Menu(Choice iot out Num) print(“1-Add a player”) print(“2-Remove a player”) print(“3-Add a player at position N”) print(“4-Print a roster”) print(“5-Quit”) read(Choice) endprocedure // Menu Player definesa record Name iot String Position iot String endrecord // Player Procedure GetPlayer(data isoftype out Player) Procedure PrintPlayer(data isoftype in Player) TEAMSIZE is 9

Algorithm Roster uses Vector(DT) Team isoftype Vector(Player) // Make the Vector!!! Choice iot Num Loop Menu(Choice) exitif(Choice = 5) if(Choice = 1) then Add(Team) elseif(Choice = 2) then Remove(Team) elseif(Choice = 3) then AddAt(Team) elseif(Choice = 4) then PrntRoster(Team) endif endalgorithm // Roster

Procedure Add(Team iot in/out Vector(Player)) temp iot Player GetPlayer(temp) Team.AddToEnd(temp) endprocedure // Add procedure Remove(Team iot in/out Vector(Player)) i iot Num print(“Line number to remove?”) read(i) Team.Remove(i) endprocedure // Remove Procedure AddAt(Team iot in/out Vector(Player)) i iot Num temp iot Player print(“Enter at what line number”) GetPlayer(temp) Team.AddAt(i, temp) endprocedure // AddAt

Procedure PrntRoster(Team iot in/out Vector(Player))) i iot Num if(Team.Size() <> TEAMSIZE) print(“Wrong size team”) else i < 1 Loop exitif(i > TEAMSIZE) PrintPlayer(Team.Get(i)) i <- i + 1 endloop endif endprocedure // PrntRoster