Binary search is an efficient algorithm for finding a key in sorted data. It runs in O(log n) time.
2
votes
1answer
48 views
Returning a list of the values from the binary tree
I want to return a list of the values from the binary tree. Is there a shorter and more efficient way to write the method for numbers?
class BTNode(object):
"""A node in a binary tree."""
...
5
votes
2answers
131 views
Finding greatest value in array smaller than x
Code review requested to make this code simpler, cleaner, and better. Input array is sorted.
This program finds the greatest number smaller than x. So, in an array [10 20 30 40] and x = 25, the ...
0
votes
0answers
18 views
Improvements to my binary tree written in CoffeeScript
This is my attempt at a binary tree. I'm using it only for ordering a LDAP user list. My need is for storing multiple objects, so this tree is limited to leaves that are objects (see data = {[..]}.
...
5
votes
1answer
100 views
Is this implementation of binary search correct?
Is this implementation of binary search correct? Any special or edge cases that I missed out? Maybe it should be optimized for elements that are less than the first element of the array or greater ...
1
vote
2answers
46 views
Binary search on cstrings
This implements binary search on an array of cstrings. One thing I'm wondering about is it faster or slower to pass a variable as const i.e. len. Also strcmp() gets called a lot and since I can't find ...
0
votes
0answers
67 views
Can someone review my Generic Binary Search Tree implementation?
I use this interface for my BST node class:
public interface BinNode<E> {
public E getValue();
public void setValue(E value);
public BinNode<E> getLeftChild();
public ...
1
vote
0answers
92 views
Binary Search Tree insert method (map interface)
This is my implementation based on Map<K,V> interface. The BST is a linked binary tree with root reference, both internal and external nodes (MyBSNode) contains (key, value) entries
What do you ...
0
votes
0answers
89 views
Created a BST for Objective-C, Looking for some feedback
I put together a Custom Linked List class in hopes to be used for a Talent/Skill Tree for a game, that allows for inserting nodes with NSMutableDictionary for Skills and Requirements along with a ...
1
vote
1answer
76 views
First Occurrence of Number in Binary Search?
I am trying to write a function, where given a sorted array, and a number, it returns the FIRST index of that number.
Is this correct? Code is in Ruby, but even if you don't know Ruby, you can pretty ...
1
vote
2answers
104 views
Fast integer handling
Each data structure has it's own time complexities. The biggest one that jumps out at you is the hash table. The average times for Insertion, Deletion and Searching are all O(1). But it's really ...
1
vote
1answer
67 views
Binary Search Feedback
I have written a binary search algorithm, but it seems to be a bit different than other peoples that I've seen. I was hoping that the community could give me some feedback as to whether or not I'm ...
4
votes
4answers
356 views
Check if a Binary Tree <String> is aBinary Search Tree
I'm understanding the question, but I want to know whether or not my implementation is correct.
The question is:
Write a method that accepts as its argument a BinaryTree object and
returns true ...
1
vote
1answer
227 views
Creating an Array of Linked Lists from a BST
Question
Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (eg, if you have a tree with depth D, you’ll have D linked lists)
Here is my ...
1
vote
2answers
150 views
How can this code become cleaner and less error prone? Especially the binary search part
How could this code become cleaner?
I think that the way I handle the interfaces and binary search could be improved.
I am trying to understand how to structure such a code 9and usage of APIs) in a ...
2
votes
1answer
302 views
Returning Binary Search Tree Nodes in Zig Zag manner
I tried to implement an answer for the algorithm question given below. I implemented two different solutions. I couldn't decide which one is better. ( Maybe none )
Note: Node class and ZigZag ...
1
vote
0answers
122 views
Binary search algorithm for non-overlapping time spans and possible gaps
I have a data structure, containing time span nodes, with the following properties:
Nodes are sorted ascending
Time spans will not overlap, but may have gaps between them
Each node will have a start ...
1
vote
1answer
109 views
Errors with binary search in Haskell
I just learned about Maybe in Haskell, so I decided to try to use it with a binary search.
Here's the function:
binarySearch :: (Ord a, Int b) => [a] -> a -> b -> b -> Maybe b
...
0
votes
3answers
373 views
Given an 2D array which is row and column wise sorted. What is the efficient way to search for an element?
For example, given a 2D array (a matrix)
{{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
What are the solutions to find a number? say 6.
Here's the code I tried using Binary search method -
(code ...
2
votes
1answer
3k views
Implemenatation of Binary search Tree
Written a code to implement BST.
I would like a code review and any suggestions to make it better?
#include<iostream.h>
#include<conio.h>
#include<stack>
#include<queue>
...
3
votes
2answers
170 views
UVA 10474 - Where is the Marble Timelimit
I got a problem from UVA online judge Problem link. I have read it tons of times and as they said, I have to get the answer really fast.
I have used a binary search and STD::Sort, but I am still ...
0
votes
3answers
298 views
Optimizing function that searches for binary patterns [duplicate]
Possible Duplicate:
Optimizing function that searches for binary patterns
I have two functions that calculate whether or not a given coordinate (x, y) is a diagonal or anti-diagonal of ...
3
votes
2answers
234 views
Improving the efficiency and design of my number guessing game
I have just written a solution to the following problem:
we all know the classic "guessing game" with higher or lower prompts. lets do a role reversal; you create a program that will guess ...
1
vote
2answers
10k views
Deleting a node from Binary Search Tree
I have read about it at few places and tried to write my own version, would like to get it reviewed.
class Node {
private int value;
private Node left;
private Node right
// ...
0
votes
0answers
2k views
InOder successor and predecessor in Binary Search Tree
Do they look correct? I implemented them and was looking to review them
Node predecessor(Node node) {
if ((node.left == null) && (node.right==null)) {
return node;
}
if ...
6
votes
1answer
835 views
Implementations of prefix lookup tables in Python
I've got a fairly performance sensitive need for fast prefix lookup, and I've built two different implementations trying to optimize for my use case. The first is built off a Trie implementation, and ...
2
votes
2answers
2k views
Splay tree implementation
I am looking into splay trees and I implemented a version. It seems correct. Could someone please let me know if this indeed correct and how to improve it?
I will just show the insert item and splay. ...
2
votes
1answer
195 views
Is this binary search close to idiomatic OCaml?
let binsearch ~arr ~cmp x = (
let cmpx = cmp x in
(* Assuming arr is ordered according to cmp, then (bs min max) is an *)
(* index of a value v in arr such that ((cmp x value) = 0) ...
6
votes
4answers
701 views
Java binary search (and get index if not there) review
I want to do binary search and if the target is not there get the index of where it should have been.
This is the code I came up with. It seems correct. Is there any problem? Can it be improved?
...
3
votes
1answer
1k views
Binary tree traversal algorithms
I recently started learning binary tree. Following is the code for a typical node for a tree (no code review required for that):
typedef unsigned int uint;
template<typename T>
class ...
1
vote
2answers
2k views
Generic binary search
I have the following code for binary search
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace ...
1
vote
3answers
497 views
Binary search optimization: Kernighan & Ritchie 3-1
The problem I am trying to solve can be described as take binarysearch0 and rewrite it as binarysearch1 such that binarysearch1 uses a single test inside the loop instead of two. The code I used to do ...