Questions tagged [binary-search]

1

votes
1

answer
74

Views

Is it possible to construct a binary search tree from a Max-Heap in O(n) steps?

I think it is not possible because if it actually is, people would build max-heap and then use it to construct BST which I think is not the case. Please give an answer with a proof.
Vimal Patel
0

votes
1

answer
22

Views

How to use structs in binary search?

I'm trying to learn c (its my first language) and i have some problems with this task. (It is no homework!) Task: Implement the binary search algorithm on an array of structs of the following kind (sorry for my English). struct { unsigned int number; char* food; int price; } pk; The given array is...
Marie.L
1

votes
4

answer
58

Views

Why is it necessary to color the root black after every insertion into a Left-Leaning Red-Black Tree?

In Sedgewick's Left-Leaning Red-Black trees (presented in his paper or his Algorithms book), one modification over the standard BST is to color the root node black after each insertion, see root.color = BLACK in insert(Key, Value). I understand that semantically this is necessary, because the root n...
Malichan
0

votes
0

answer
9

Views

Segmentation fault (core dumped) - Threaded Binary Search Tree

I keep getting the following error : Segmentation fault (core dumped) . I found out the line of code that is causing the problem ( marked with a comment inside of the program) . Please tell me why this error is happening and how to fix it. I've tried to dry run my code (on paper ) and see no logical...
Harshit Pandey
1

votes
2

answer
1.2k

Views

Coding an iterator to traverse binary tree

The question is, given a BST, find out whether there are two numbers that add up to a given number k. No extra memory should be used. Now if it were a sorted array, I could have simply kept two pointers, one at the beginning, one at the end. At each step I would compute the sum of the two numbers po...
SexyBeast
1

votes
1

answer
4.3k

Views

Making a delete function for a binary search tree in C++

Edited*: I'm working on the delete function for a binary search tree. I'm just working on the first case right now. I think this is correct, but I'm wondering if it can be done recursively, or more efficiently. Any help is appreciated. Assume BSTSearch searches for a node, isLeaf returns true if the...
CoffeeIsProgramming
1

votes
2

answer
1.7k

Views

Multiple Searches in a single text File

So far I have made a program that can search for a single string in a text file. It does give the indexes of the string I searched for. However, how can I search for multiple string in a single text file? So far if I input "Alice" using the Scanner my output would be: Got a match at line 17 Got a ma...
user1883386
1

votes
4

answer
390

Views

Binary Search using Java on a UTF-8 encoded text file where line size is not fixed

I have a tab separated UTF-8 file, where the records are sorted on one field. But, the line size is not fixed, so cannot jump into a particular position directly. How can I perform binary search on this? Example: line 1: Alfred Brendel /m/011hww /m/0crsgs6,/m/0crvt9h,/m/0cs5n_1,/m/0cr...
Esha Ghosh
-1

votes
1

answer
39

Views

Binary search code fails efficiency check

I have been tasked to complete a technical assessment for a position involving a simple C++ coding exercise. The problem was to check if a number exists in a sorted array, where: ints[] is the array to be sorted size is the size of the array k is the number to be checked The requirement was to imple...
Darius Hente
1

votes
1

answer
336

Views

BST is not abstract and does not override abstract method height() in TreeGT

I am receiving the error: BST is not abstract and does not override abstract method height() in TreeGT. I need some help with understanding what it means, and how I can get resolve it. In the file TreeGT.java I have the following code: public interface TreeGT { public boolean insert(E item); public...
1

votes
1

answer
290

Views

C++ delete a node from BST [closed]

I am having trouble with my delete function. I don't know what seems to be the problem. Please help me fix this. thank you very much. node* tree_minimum(node *x){ while(x->left!=NULL){ x=x->left; } return x; } node* tree_successor(node *x){ if(x->right!=NULL) return tree_minimum(x->right); node *y;...
1

votes
1

answer
305

Views

using java default binarysearch on biginteger

I have used the binarySearch on array of BigInteger, I tested with a few numbers, seems working fine. So I assume I don't need to implement/override any methods (e.g. Comparator) for it to work properly? I'm using the following method: static int binarySearch(Object[] a, Object key) //Searches the...
Miranda
1

votes
1

answer
1.3k

Views

Python — Morse Code Translation through a binary tree

I'm writing a program that will create a binary tree of the Morse Code alphabet (as well as a period and an apostrophe), and which will then read a line of Morse Code and translate it into English. (Yes, I know that a look-up table would be easier, but I need to sort out my binary trees). I think a...
user2309856
1

votes
2

answer
2k

Views

Binary Search with character arrays?

I'm having a little bit of trouble understanding how to do a binary search with strings. What I have as input is a sentence in the command line prompt which is argv[1], (argv[0] being the name of the executable I output). This is how it looks: ./a.out "This is my sentence." And included in my file i...
user1871869
1

votes
1

answer
209

Views

Change the inequality in binary search code

shouldn't be if(a[mid] < t)return BS(mid+1,high); else return BS(low,mid); the same as if(a[mid] > t)return BS(low,mid-1); else return BS(mid,high); But the second one doesn't work, why? Edit: I mean by doesn't work, that the code doesn't reach the base case.
Mahmoud Maged
1

votes
4

answer
1.7k

Views

Remove duplicates in a linked list

I have seen , written and tested a few logics to remove duplicates in linked list. e.g. Using two loops (O(n2)) , or sorting and removing duplicates though it doesn't preserve order. I am just wondering is it valid or legible if we pull out the elements of a linked list and start creating a binary s...
Diwakar Sharma
1

votes
1

answer
2.2k

Views

k successive calls to tree successor in bst

Prove that K-successive calls to tree successor takes O(k+h) time. Since each node is visited atmost twice the maximum bound on number of nodes visited must be 2k. The time complexity must be O(k). I dont get where is the factor of O(h) coming. Is it because of nodes which are visited but are not th...
user2179293
1

votes
3

answer
1.5k

Views

How to find the sum of the values stored in a binary tree of integers weighted by the depth of each value?

Explanation: For the given binary tree +---+ | 9 | +---+ / \ +---+ +---+ | 7 | | 6 | +---+ +---+ / \ \ +---+ +---+ +---+ | 3 | | 2 | | 4 | +---+ +---+ +---+ / \ +---+ +---+ | 5 | | 2 | +---+ +...
Nishant
1

votes
2

answer
131

Views

BST time complexity: Is there any difference between float value and int value?

Given a BST of float numbers and a BST for int numbers. I'm just wondering that is there any time complexity difference of insertion, deletion, etc. between different data type based BST?
DoReMi
1

votes
3

answer
417

Views

C++ Algorithm to Filter Irrelevant Coordinate Data

I'm currently working on a hobby project in which I have several thousand stars in a 2D fictional universe. I need to render these stars to the screen, but clearly I don't want to have to operate on all of them -- only the ones that are visible at any given time. For proof of concept, I wrote a brut...
SamuelMS
1

votes
2

answer
121

Views

Search for secondary data in BST

I have built a BST that holds a name as it's primary data, along with the weight associated with that name (as in when the info is inserted it goes in as Tom - 150, but is sorted in the tree by Tom). I need to be able to determine who has the lowest weight and I am not sure how to go about it. Bel...
Tyler
1

votes
2

answer
418

Views

Find the swapped nodes in binary search tree

Two of the nodes of a Binary Search Tree are swapped. Input Tree: 10 / \ 5 8 / \ 2 20 In the above tree, nodes 20 and 8 must be swapped to fix the tree. Output tr...
Trying
1

votes
2

answer
158

Views

while loop executing infinitely without any recursive call even after execution flow reaches end of function containing while

below is a function i am using for inorder traversal of a binary search tree . Everything works well till the loop while(!st.empty()) gets terminated but after that the execution flow again goes to while(!st.empty()) hence loop turns into an infinite loop structure of node class bst{ private: bst *...
Rahul
1

votes
2

answer
40

Views

segmentation error searching through binary tree

I am receiving a segmentation fault while searching through a binary tree for a match. It does not give me a segmentation fault if a match is found but if it does not find anything it isn't finishing properly. Could someone point me in the right direction? What am i doing wrong. void search() { cha...
user2644819
1

votes
1

answer
3.8k

Views

Binary Search Tree Python, Implementing Delete

I am trying to implement a binary search tree in python, but I can't find a solution for delete. If the item is in a leaf, that is simple, but what if the item I want to delete has 2 children which also have other children and so on ? How can in find its successor, so that I can replace it ? Are the...
buster
1

votes
1

answer
110

Views

What is the best way to guess the value of x+y? [closed]

The problem is simple when we only want to guess one number. For example, we would like to guess x and we know that the highest possible value is n. We can do binary search, of which complexity is O(log n). However, I have found the variation of this problem: Given 0
FiveFiftyFive
1

votes
1

answer
528

Views

Remove an element from unbalanced binary search tree

I have been wanting to write remove() method for my Binary Search Tree (which happens to be an array representation). But before writing it, I must consider all cases. Omitting all cases (since they are easy) except when the node has two children, in all the explanations I have read so far, most of...
Yokhen
1

votes
1

answer
1.1k

Views

Discrete binary search

Can someone please explain discrete binary search with an example? I read about it on the above link, and got a basic idea about what it is, but I still don't understand the code part and how it is practically implemented.
user3004790
1

votes
2

answer
1.2k

Views

Time complexity for rebuilding binary search tree / quadtree / octtree?

Let's say I have a binary search tree with N nodes. Or an quad tree, or octtree, if these make any difference. Then let's say I run an algorithm which changes all the keys in the tree. It seems to be complicated to move stuff around. Perhaps there actually is a good algorithm for this, but let's say...
bombax
1

votes
1

answer
590

Views

C++ iterative insert into binary search tree BST

My function works as long as item is smaller and therefore inserted to the left, but when it should go right, nothing seems to happen. It doesn't crash or anything, it just doesn't do the insert. I don't get how this could happen because the only different in the code in either case is the words le...
LoserName
1

votes
3

answer
133

Views

Binary search in Java - learning it “my way”

So I'm trying to teach myself how to implement a binary search in Java, as the topic might have given away, but am having some trouble. See, I tend to be a little stubborn, and I'd rather not just copy some implementation off the internet. In order to teach myself this, I created a very (VERY) rough...
ViRALiC
1

votes
1

answer
86

Views

Binary Search Tree Iterate non-keys

I have a bit of a conundrum. I need to print all the values in my BST that are NOT keys. So since the tree is not ordered according to these values, I can't do as I normally have with BSTs in the past. I simply need to look at EVERY node on the tree, compare the non-key value with the value I hav...
user3066571
1

votes
2

answer
209

Views

Insertion Sort with Binary Serach

I cannot find what's wrong with my insertion sort. I need to implement binary search into my sort and it will not work. public void insertionSort(String[] data){ for (int i=1; i
Gucci Product Wearer
1

votes
1

answer
102

Views

My Binary Tree's delete function occasionally results in duplicate entries

I created a small Binary Tree "library", but tDelete isn't functioning properly in certain circumstances. I tested it on simpler trees and it worked fine, but in this particular scenario, it causes a duplicate node to be added to the tree. It seems to be because the recursive call to tDelete can't f...
Carcigenicate
1

votes
3

answer
1.1k

Views

Template with struct

I am designing a Binary Search Tree which allows user to enter value of any data-type, not just int.To achieve this,i am trying to use template with a struct. I defined my struct as follows template struct node { struct node *left; T info; struct node *right; }*root; Now i am trying use this in a c...
Pradeep
1

votes
3

answer
2.1k

Views

Binary search and invariant relation

I'm reading this post and trying to figure out how could we determine invariant relation for binary search. To be specific, in the two examples he gave, why these two invariant relation is different? What make the different? The part A[start] < target < A[end] is obvious, but the question is where t...
fuiiii
1

votes
1

answer
371

Views

Binary Search Tree recursive insertion causing stack overflows, iterative insertion not working

I have this project to do for class where I have to add 10000 elements (in order) to a binary search tree -- among other things, but that's not what's currently broken. My recursive insertion works fine until about 3500 elements, then it breaks and causes a stack overflow. I then tried to create an...
Tyler V
1

votes
1

answer
8.2k

Views

Binary Search Tree - Copying a tree

I'm messing around with sorting data structures in C++ so I'm writing a bunch of functions to make a binary search tree class. I'm a bit lost when trying to create a copy constructor/function to copy a tree. void BST::copyBST(Node* treeOriginal, Node* treeNew){ if (treeOriginal == NULL) //base case...
1

votes
0

answer
37

Views

Binary Search Tree - Memory Leak at insertion

I am trying to create a binary search tree and insert a new node in an iterative way. It is all working well except I am getting a memory leak in this function. Valgrind says 7 blocks (I am adding 7 nodes) are missing. I couldn't see where my leak is. I would appreciate another look at my code....
aomerk
1

votes
1

answer
262

Views

data structure for storing angle intervals

I want to create a data structure that can store angle intervals, for example (a,b) where both a and b represent the polar angle of the the end points of a segment A,B against a point Q This data structure must support two operations. Insert new intervals and also tell me if the entire view point of...
jsguy

View additional questions