lecture6 - Searching:BinaryTrees Dr.YingwuZhu...

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

View Full Document Right Arrow Icon
    Searching: Binary Trees Dr. Yingwu Zhu
Background image of page 1

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

View Full DocumentRight Arrow Icon
    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
Background image of page 2
    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++; } }
Background image of page 3

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

View Full DocumentRight Arrow Icon
    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++; } }
Background image of page 4
    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
Background image of page 5

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

View Full DocumentRight Arrow Icon
    Binary Search Two requirements?
Background image of page 6
    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!)
Background image of page 7

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

View Full DocumentRight Arrow Icon
    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 } }
Background image of page 8
    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
Background image of page 9

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

View Full DocumentRight Arrow Icon
    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
Background image of page 10
    Binary Search Tree Redraw the previous structure so that it has  a treelike shape – a binary tree
Background image of page 11

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

View Full DocumentRight Arrow Icon
    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 )
Background image of page 12
Image of page 13
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 Right Arrow Icon
Ask a homework question - tutors are online