Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS261 – Recitation 5 Fall 2013. Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1."— Presentation transcript:

1 CS261 – Recitation 5 Fall 2013

2 Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1

3 2 Sequential search requires O(n) runtime

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

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

6 Binary Search: Ordered Array Algorithm 5

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 index Requirements: Random access to the elements Elements are already in sorted order 6

8 Worksheet #26 Binary search algorithm on ordered array and implementation of bag interface Time complexity (average case): 7

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. 8

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? 9

11 Explain why the following are not legal trees. 10

12 Tree Mini Exercise 1. 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 11


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

Similar presentations


Ads by Google