1 / 21

Class 16 - Searching

Class 16 - Searching. Linear search Binary search Binary search trees. The problem. Given a set of data items {k 1 , k 2 , ..., k n } - called keys - determine whether another key k occurs in the set.

james-eaton
Download Presentation

Class 16 - Searching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class 16 - Searching • Linear search • Binary search • Binary search trees

  2. The problem • Given a set of data items {k1, k2, ..., kn} - called keys - determine whether another key k occurs in the set. • (Variation: Start with pairs (k1,v1), ..., (kn,vn). Then given k, determine whether k = ki for some i; return vi.)

  3. “Data structures” • We will consider two costs: • The cost of inserting the keys ki in some data structure (array, list, whatever) • The cost of searching for k • Cost depends primarily on how data are stored. • Best data structure can depend upon ratio of searches to insertions, how searches and insertions are distributed.

  4. Data structures (cont.) • We will look at three data structures: • Linear storage in array - unsorted • Linear storage in array - sorted • Binary tree

  5. Linear storage, unsorted Store data in array keys[0...count], unsorted 1. To insert key k: keys[count] = k; count++; 2. To find key k: for (i=0; i<count; i++) if (keys[i] == k) return i; Cost of inserting new key: O(1) Cost of searching for key: O(n)

  6. Linear storage, sorted Store data in array keys[0...count], sorted 1. To insert key k: insert in correct order, as in insertion sort 2. To find key k: same as above Cost of inserting new key: O(n) Cost of searching for key: O(n)

  7. Binary search Cost of searching in sorted array can be dramatically reduced by “divide and conquer”. boolean bsearch (int k, int[] keys, int i, int j) // find whether k occurs in keys[i..j]] Can cut size of subarray in half with one comparison:

  8. Binary search (cont.) i m j • Cases: • k == keys[m]: done • k < keys[m]: k occurs in keys[i..m-1] (if at all) • k > keys[m]: k occurs in keys[m+1..j] (if at all) keys m = (j+i)/2

  9. Binary search (cont.) boolean bsearch(int k, int[] keys, int i, int j) { // find whether k occurs in keys[i..j] int m = (i+j)/2; if (i > j) return false; if (k == keys[m]) return true; if (k < keys[m]) return bsearch(k, keys, i, m-1); return bsearch(k, keys, m+1, j); }

  10. Efficiency of binary search # of comparisons = 2 * # of times array can be divided in half: log2 n E.g. If n = 1,000,000, worst-case cost of linear search = 1,000,000; worst-case cost of binary search = 40.

  11. Binary trees Can obtain log n insertions and searches (on average) by storing data in binary tree structure. Def. A binary tree is a structure consisting of a number (the label ) and two binary trees, the left and right subtrees. Both are optional.

  12. Binary trees (cont.) 15 10 19 9 12 16 11 14 Def. Size of tree t = number of nodes (i.e. numbers) in t.

  13. Binary search trees • Def. A binary search tree (BST) is a binary tree with the following property: its label is greater than any of the labels in its left subtree and less than any labels in its right subtree. Furthermore, its left and right subtrees are binary search trees. • Example: tree on previous slide

  14. Balanced BST’s • A binary search tree is balanced if its left and right subtrees are of approximately equal size, and are both balanced. • Def. The height of a tree is the length of the longest path from the top to the bottom. • Observation If a BST of size n is balanced, its height  log2 n.

  15. Searching in BST k’ To find k in BST T = Cases: • k = k’: Done • k < k’: Search in T1 (if it exists) • k > k’: Search in T2 (if it exists) Time (in worst case) is proportional to height of T. If T is balanced, time is order log2 n. T1 T2

  16. Inserting in BST k’ To insert k in BST T = Cases: • k = k’: Done (do nothing) • k < k’: Insert in T1. If T1 absent, add new left subtree containing just k • k > k’: Insert in T2. If T2 absent, add new right subtree containing just k If T is balanced, time is order log2 n. T1 T2

  17. Implementing BST’s • Represent trees by objects containing an integer and two references to other trees. (Recall that a reference to an object can be null, meaning it doesn’t really point to anything.) class BinarySearchTree { int val; BinarySearchTree left, right; BinarySearchTree (int v, BinarySearchTree l, BinarySearchTree r) { val = v; left = l; right = r; } }

  18. Implementing BST’s (cont.) • Could make the search and insert operations instance methods, but, as for lists, we will instead make them class methods of a separate class, BST. class BST { static BinarySearchTree makeBST (int k) { return new BinarySearchTree(k, null, null); }

  19. Implementing BST’s (cont.) static boolean search (int k, BinarySearchTree t) { if (t == null) return false; else if (k == t.val) return true; else if (k < t.val) return search(k, t.left); else return search(k, t.right); }

  20. Implementing BST’s (cont.) static void insert (int k, BinarySearchTree t) { // t must be non-null if (k == t.val) return; if (k < t.val) if (t.left == null) t.left = makeBST(k); else insert(k, t.left); else if (t.right == null) t.right = makeBST(k); else insert(k, t.right); }

  21. Implementing BST’s (cont.) Here is an example of using these operations: public static void main (String[] args) { BinarySearchTree T = BST.makeBST(7); BST.insert(3,T); BST.insert(5,T); BST.insert(13,T); BST.insert(11,T); BST.insert(9,T); BST.insert(1,T); System.out.print(BST.search(3, T)); // true System.out.print(BST.search(4, T)); // false }

More Related