Binary Search Introduction to Trees Binary searching introduction
Binary Search Introduction to Trees Binary searching & introduction to trees
Last time: recursion n In the last lecture, we learned about recursion & divide-and-conquer n n n Apply these techniques to storing data so that it is n n n Ordered Easy to find Hash tables and list-type structures don’t do both n n n Split the problem into smaller parts Solve each of the smaller parts separately: easier to code & understand! Lists & arrays: ordered, but lookup is slow Hash tables: lookup is fast, but not ordered We want a structure that can do both! CMPS 12 B, UC Santa Cruz Binary searching & introduction to trees 2
Quickly finding a particular item… n n Problem: in a class of n students, who has the mth best grade? Use a hash table? n n n Use a (sorted) linked list? n n n Easy to add and lookup new students Table has no order: very difficult to answer this question without sorting it first Easy to find: count m links from the start Difficult to insert: must search along the list to find the correct insertion point Use an array? n Same kinds of advantages and disadvantages as linked list CMPS 12 B, UC Santa Cruz Binary searching & introduction to trees 3
What if we only have the value? n Rather than find the mth best grade, find the student whose grade is 77 n n n Can’t just count m items any more! Must scan the list / array until we find the correct student A better way: play 20 questions! n n n Guess a random number between 1 and 1 million in only 20 guesses! How can we do this? How does this apply to searching? CMPS 12 B, UC Santa Cruz Binary searching & introduction to trees 4
20 Questions n n Take a sorted array of values While (item not found) n n n “Guess” the item in the middle of the array See if the desired item is above or below the guess Narrow down the search area by half This works in log 2(N) tries on an array with N values Much faster than simply scanning CMPS 12 B, UC Santa Cruz Binary searching & introduction to trees 5
Binary search n n int bsearch (int values[], int find. This) { int range = values. length; int base = 0; int mid; n Problem split in half at each step while (range > 1) { range = (range+1)/2; n Main difference: ignore the half mid = base+range; where the value isn’t if (find. This > values[mid]) { Recursion doesn’t usually save base = mid; } else if (find. This==values[mid]){ time break; n Easier to program, though } Binary search saves time! } if (values[mid]==find. This) { n Rule out half of the remaining return (mid); values at each step } else { n Like recursion where we ignore return (-1); half of the problem each time we } } recurse “ 20 questions” is called binary search Similar to recursion CMPS 12 B, UC Santa Cruz Binary searching & introduction to trees 6
Binary search is great, but… n Binary search works well with arrays n n n Binary search doesn’t work well with linked lists n n n Easy to find element n in constant time Difficult to insert things into the middle of the array Can’t find element n in constant time: long lists => long time to find elements Easy to insert and delete things in the middle Modify linked lists to make searching easier? n Keep references into the middle of the list (1/4, 1/2, 3/4, or similar)? n n n Good idea, but doesn’t scale that well Must recreate shortcuts when things are inserted or deleted Create a new structure that uses links but is still easy to do binary search on? CMPS 12 B, UC Santa Cruz Binary searching & introduction to trees 7
Solution: trees n n A tree is a linked data structure where nodes may have more than one “next” Terms n n n n “next” of a node is its child “prev” of a node is its parent Base of the tree is the root Nodes along path to root are ancestors Nodes “below” this one are descendants Nodes with no children are leaf nodes Binary tree: tree in which each node has at most two children CMPS 12 B, UC Santa Cruz root A B C parent leaf D E child Class BTNode { Object item; BTNode left; BTNode right; BTNode parent; } Binary searching & introduction to trees 8
Why use trees? n Advantages of linked lists n n n Advantages of arrays n n n Easy to do binary search Easy to keep sorted Advantages of hash tables n n Insert or delete anywhere with ease Grow to any size Lookup can be done quickly if the tree is sorted Disadvantages? n n Overhead: three references per node in the tree It’s easy to have trees grow the wrong way… CMPS 12 B, UC Santa Cruz Binary searching & introduction to trees 9
- Slides: 9