cs261 – recitation 5 fall 2013. outline assignment 3: memory and timing tests binary search...

12
CS261 – Recitation 5 Fall 2013

Upload: rebecca-harrington

Post on 02-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS261 – Recitation 5 Fall 2013. Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1

CS261 – Recitation 5

Fall 2013

Page 2: CS261 – Recitation 5 Fall 2013. Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1

2

Outline

• Assignment 3: Memory and Timing Tests• Binary Search Algorithm• Binary Search Tree• Add/Remove examples

Page 3: CS261 – Recitation 5 Fall 2013. Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1

3

Sequential search requires O(n) runtime

Page 4: CS261 – Recitation 5 Fall 2013. Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1

4

Both remove() operations take O(n) but DynArrBag remove() requires sliding of

elements

Page 5: CS261 – Recitation 5 Fall 2013. Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1

5

LL implementation requires extra data properties (e.g., prev/next pointers)

Page 6: CS261 – Recitation 5 Fall 2013. Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1

6

Binary Search: Ordered Array Algorithm

Page 7: CS261 – Recitation 5 Fall 2013. Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1

7

Binary Search Important Notes

Return Value:•If value is found, returns index•If value is not found, returns position where it can be inserted without violating ordering•return index can be larger than a legal indexRequirements:•Random access to the elements•Elements are already in sorted order

Page 8: CS261 – Recitation 5 Fall 2013. Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1

8

Worksheet #26

Binary search algorithm on ordered array and implementation of bag interfaceTime complexity (average case):

Page 9: CS261 – Recitation 5 Fall 2013. Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1

9

Tree Review• Trees are the third most used data

structure, after arrays and linked lists• A tree simulates a hierarchical tree

structure with a set of linked nodes.• A binary tree is a special type of tree.

Each node has at most two children. Children are either left or right.• A binary search tree is a binary tree in

which for each node, the values in all descendants to the left of the node are less than or equal to the value of the node, and the values in all descendants to the right are greater than or equal.

Page 10: CS261 – Recitation 5 Fall 2013. Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1

10

Tree Review

• Is it a tree?

• Is it a binary tree?

• Is it a full binary tree?

• Is it a complete binary tree?

• Is it a binary search tree?

Page 11: CS261 – Recitation 5 Fall 2013. Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1

11

Explain why the following are not legal trees.

Page 12: CS261 – Recitation 5 Fall 2013. Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1

12

Tree Mini Exercise1. Construct a binary search tree by adding the following numbers to the tree in the order that they are given:7, 3, 9, 5, 6, 2, 8

2. Remove 7 from the constructed tree