lecture6

# lecture6 - Searching:BinaryTrees Dr.YingwuZhu...

This preview shows pages 1–13. Sign up to view the full content.

Searching: Binary Trees Dr. Yingwu Zhu

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Review of Linear Search Collection of data items to be searched  is organized in a list        x 1 , x 2 , … x n Assume  ==   and  <  operators defined for  the type Linear search begins with item 1 continue through the list until target found or  reach end of list
Linear Search Vector based search function template <typename t> const t &item, { found = false; loc = 0; for ( ; ; ) { if (found || loc == v.size()) return; if (item == v[loc]) found = true; else loc++; } }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Linear Search Singly-linked list based search function template <typename t> void LinearSearch (NodePointer first, const t &item, bool &found, int { NodePointer locptr=first; for{loc=0; !found & locptr!=NULL; locptr=locptr->next) { if (item == locptr->data) found = true; else loc++; } }
Linear Search template <typename t> void LinearSearch (const vector<t>& v , { found = false; loc = 0; for (vector<t>::iterator it=v.begin(); !found & it != v.end(); it++) { if (*it == item) found = true; else loc++; } } Iterator-based linear search

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Binary Search Two requirements?
Binary Search Two requirements The data items are in ascending order (can  they be in decreasing order??  ) Direct access of each data item for  efficiency (why linked-list is not good!)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Binary Search Binary search function for vector template <typename t> const t &item, found = false; loc = 0; int first = 0; last = v.size() - 1; for ( ; ; ){ if (found || first > last) return; loc = (first + last) / 2; if (item < v[loc]) last = loc - 1; else if (item > v[loc]) first = loc + 1; else found = true; // item found } }
Binary Search vs. Linear Search Usually outperforms Linear search  O(logn) vs. O(n) Disadvantages Sorted list of data items Direct access of storage structure,  not   good for linked-list Good news: It is  possible to use a linked  structure which can be searched in a  binary-like manner

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Binary Search Tree Consider the following ordered list of integers 1. Examine middle element 2. Examine left, right sublist (maintain pointers) 3. (Recursively) examine left, right sublists 80 66 62 49 35 28 13
Binary Search Tree Redraw the previous structure so that it has  a treelike shape – a binary tree

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Trees A data structure which consists of  a finite set of elements called nodes  or vertices a finite set of directed arcs  which connect the  nodes If the tree is nonempty one of the nodes (the root ) has no incoming arc every other node can be reached by following a  unique sequence of  consecutive arcs  (or  paths )
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/01/2011 for the course CSSE 250 taught by Professor Dr.yingwuzhu during the Fall '11 term at UH Clear Lake.

### Page1 / 54

lecture6 - Searching:BinaryTrees Dr.YingwuZhu...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online