binary-search

How to extend a binary search iterator to consume multiple targets

I have a function, binary_range_search, that is called like so: my $brs_iterator = binary_range_search( target => $range, # eg. [1, 200] search => $ranges # eg. [ {start => 1, end => 1000}, ); # {start => 500, end => 1500} ] brs_iterator->() will ...

Is it necessary to implement a BST with both keys and values?

Is it necessary to implement a BST with both keys and values? I can implement a BST that has method calls such as the following, in which it will make the comparison at each node of whether the traversal should go to the left node or right node based upon the V value: public class BST<V> { public void Insert(V value) { /...

Flash browser app ActionScript: how to extract a subset of objects from a sorted array *efficiently*?

I have a browser-deployed Flash app (not an AIR app with access to SQLConnection) and it fetches JSON results from a remote server via HTTPService. I need to extract subsets from the returned resultset, an array of objects, efficiently. Mutltiple calls through the cloud to the back-end won't do. It all has to happen client-side. Is t...

Optimum way to compare strings in Javascript?

I am trying to optimize a function which does binary search of strings in Javascript. Binary search requires you to know whether the key is == the pivot or < the pivot. But this requires two string comparisons in Javascript, unlike in C like languages which have the strcmp() function that returns three values (-1, 0, +1) for (less than...

Is insertion time complexity of sorted-list implementation of priority queue O(n)?

From wikipedia: Sorted list implementation: Like a checkout line at the supermarket, but where important people get to "cut" in front of less important people. (O(n) insertion time, O(1) get-next time, O(n*log(n)) to build) I think if searching the insert position with binary search algorithm,the insertion time complexity...

No guarantees for Arrays.BinarySearch?

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html Sun does not mention any complexity for their binary-search implemention. Is this a mistake? I know it should be O(logn), but it makes me nervous when they don't state this explicitly. They do for some of their algoritms, like Arrays.sort. Does any of you have any knowledge ...

Construct a binary tree such that the Post-order traversal should give the sorted result

Hi, I know that In-order traversal(VISIT LEFT, VISIT ROOT, VISIT RIGHT) on a binary search tree gives me a sorted result. But I need to do a Post-order traversal (VISIT LEFT, VISIT RIGHT, VISIT ROOT) on a binary tree and the result should give me sorted values. In order to achieve that, how should I construct my binary tree? ...

Extension of Binary search algo to find the first and last index of the key value to be searched in an array.

The problem is to extend the binary search algorithm to find all occurrences of a target value in a sorted array in the most efficient way. Concretely speaking, the input of the algorithm are (1) a sorted array of integers, where some numbers may appear more than once, and (2) a target integer to be searched. The output of the algorithm...

Are structs necessary in binary search trees

I've looked at some code for BSTs and I can see that each node is a struct. Is this necessary? ...

How to find the last element of array in binary search

In binary Search algorithm, the upper-bound element is array.length-1, then how can I find the last element of an array? If the lower-bound and upper-bound for element of an array of length 8 is 6 and 7 respectively, then my mid element coming out as: mid=(6+7)/2 i.e. 6 in java ...

Find kth smallest element in a binary search tree in Optimum way

Hi, I need to find the kth smallest element in the binary search tree without using any static/global variable. How to achieve it efficiently? The solution that I have in my mind is doing the operation in O(n), the worst case since I am planning to do an inorder traversal of the entire tree. But deep down I feel that I am not using the ...

Java Binary search

Hi there, Trying to perform a binary search on a sorted array of Book objects. Its not working well, it returns the correct results for some of the objects, but not all. I went through the loop on paper and it seems that a number can get missed out due to rounding #.5 upwards. Any ideas how to make this work? Book found = null; /...

C++: binary search compile error

Hi: I have the following lines of code: if(std::binary_search(face_verts.begin(), face_verts.end(), left_right_vert[0]) && std::binary_search(face_verts.begin(), face_verts.end(), left_right_vert[1])) And when I compile my code, I get the following errors: In file included from /usr/include/c++/4.4/algorithm:62, ...

binary search question

For binary search, what is the average number of comparisons needed to find a record in a file? ...

Binary Search Help

Hi, for a project I need to implement a binary search. This binary search allows duplicates. I have to get all the index values that match my target. I've thought about doing it this way if a duplicate is found to be in the middle: Target = G Say there is this following sorted array: B, D, E, F, G, G, G, G, G, G, Q, R S, S, Z I get th...

Binary Search Tree Balancing

I had a quesiton here, but it didn't save. I'm having trouble balancing a fully unbalanced tree (nodes 1-15 along the right side). I'm having trouble because I get stack overflow. > // balancing public void balance(node n) { if(n != null) { System.out.println(height(n)-levels); if (height(n.RCN) != h...

binary_search not working for a vector<string>

#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) { string temp; vector<string> encrypt, decrypt; int i,n, co=0; cin >> n; for(i=0;i<n;i++) { cin >> temp; encrypt.push_back(temp); } for(i=0;i<n;i++) { cin >> temp...

What's the case for duplications in BST?

How to solve the problem with duplication in Binary Search Tree? ...

Binary Search Tree in Java

I want to make a generic BST, that can be made up of any data type, but i'm not sure how I could add things to the tree, if my BST is generic. All of my needed code is below. I want my BST made up of Locations, and sorted by the x variable. Any help is appreciated. Major thanks for looking. public void add(E element) { if (root == ...

number of comparisons of binary search

What is the total number of comparisons necessary to locate all the n sorted distinct integers in an array using binary search? I think the number is n log2 n (2 is the base), but I am not sure. What do you think? ...