Search results
faster searching algorithm: binary search. You might recall that binary search is similar to the process of finding a name in a phonebook. This algorithm’s speed can be leaps and bounds better than linear search, but not without a cost: binary search can only be used on data that is already sorted. The Binary Search Algorithm The basis of ...
We use binary search to look for an integer in a sorted array to exemplify it. We started in a previous lecture by discussing linear search and giving some background on the problem.
binarysearch(A,x): lo = 0 hi = A.size - 1. while lo < hi mid = (lo + hi) / 2 if A[mid] == x: return true if A[mid] < x: lo = mid + 1 if A[mid] > x: hi = mid - 1. return [lo] == x. ‣ Recursive algorithms can be implemented iteratively.
- Previously
- void inOrderTraversal(Node t){
- while(root != null
- if(root == null) return null; return root.data; }
- insert(13) insert(8) insert(31)
- Why might deletion be harder than insertion?
- Deletion – The Two Child Case
- findMax(node.left)
- Lazy Deletion
Dictionary ADT stores (key, value) pairs find, insert, delete Trees Terminology Binary Trees
if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); Sometimes order doesn’t matter Example: sum all elements Sometimes order matters Example: evaluate an expression tree ✓ ✓ ✓ ✓ ✓ E F G
&& root.key != key) { if(key < root.key) root = root.left; else(key > root.key) root = root.right; }
Worst case running time is O(n). - Happens if the tree is very lopsided (e.g. list)
(New) insertions happen only at leaves – easy! Again... worst case running time is O(n), which may happen if the tree is not balanced.
Removing an item may disrupt the tree structure! Deletion in BST Basic idea: find the node to be removed, then “fix” the tree so that it is still a binary search tree Three potential cases to fix: Node has no children (leaf) Node has one child Node has two children
Idea: Replace the deleted node with a value guaranteed to be between the two child subtrees Options: • successor minimum node from right subtree
Now delete the original node containing successor or predecessor
Lazy deletion can work well for a BST Simpler Can do “real deletions” later as a batch Some inserts can just “undelete” a tree node But Can waste space and slow down find operations Make some operations more complicated: e.g., findMin and findMax?
Jan 31, 2013 · 1 Introduction. One of the fundamental and recurring problems in computer science is to find elements in collections, such as elements in sets. An important al-gorithm for this problem is binary search. We use binary search for an in-teger in a sorted array to exemplify it.
- 326KB
- 15
Binary Search The binary search algorithm can only be applied on sorted data and works by finding the middle element in a list of data before deciding which side of the data the desired element is to be found in.
People also ask
What is binary search in Computer Science?
What is a binary search algorithm?
How do different search algorithms work?
How can a binary search algorithm be more efficient?
In In computer science, binary search, also known as half-interval search,[1] logarithmic search,[2] or binary chop,[3] is a search algorithm that finds a position of a target value within a sorted array.[4] . Binary search compares the target value to an element in the middle of the array.