there was a reputed site which claimed that we cannot do binary search on linked list but I know for a fact that we can perform merge sort( which uses divide and conquer just like binary search) on a linked list so we must be able to perform binary search on a linked list as well right??
Purely in the abstract, a linked list does not support direct indexing. The only way to get to the third element is by starting at the first element, following the next link to the second element, then following it's next link to the third element.
If the implementation allows for indexing (e.g., java.util.LinkedList), it is possible to implement a binary search by calling get(index) when you need an element. If the underlying data structure is a simple linked list without an auxiliary lookup structure, then this will perform very poorly. As an example, the following is the code from the OpenJDK java.util.LinkedList.get method.
package java.util;
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
transient int size = 0;
transient Node<E> first;
transient Node<E> last;
public E get(int index) {
return node(index).item;
}
Node<E> node(int index) {
if (index < (size >> 2)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
}
When list.get(5) is called to get the sixth element in a list of more than 11 elements, it iterates over the first five elements before returning the sixth. The generic interface provides indexed access into the sequence but makes no guarantees on its performance.
Related
I'm designing a programming language and one feature I'd like to add is a cross between a trampoline function and a class. That is, a class which takes in a literal akin to a generic class taking in a type. I'm stuck on a name for these because I haven't encountered them in a language before, is there something which already means this concept or something close? Using trampoline class is an option, but if there's something that more accurately describes this or is already in use in another language I'd prefer to go with it to cut down on the amount of jargon required in the documentation.
Pseudo-code follows to illustrate this principle in case it is not clear from the above:
class Point<const int n> {
private float[n] _value;
Point() {
for (int i = 0; i < n; i++) {
this._value[i] = 0f;
}
}
Point(Point<o> other) {
for (int i = 0; i < min(n, o); i++) {
this._value[i] = 0f;
}
}
public static float operator [index] (optional float value = null) {
if (value != null) { this._value[index] = value; }
return (this._value[index]);
}
public static Point<max(o, p)> operator + (Point<const int o> p1, Point<const int p> p2) {
Point<min(o, p)> small = (p1.n < p2.n ? p1 : p2);
Point<min(o, p)> large = (p1.n < p2.n ? p2 : p1);
Point<max(o, p)> ret = new Point<max(o, p)>(large);
for (int i = 0; i < min(o, p); i++) { ret[i] += small[i] }
return (ret);
}
}
The term you are looking for is dependent types. It means that a type cannot only have type parameters (like generics), but a type can also be parameterized with arbitrary values (the dependent type parameters). For example, you can define the signature of a function that takes a number n and returns an array of length n.
Sadly, dependent type checking in general is undecidable. This is, because you have to calculate the range of possible values of the dependent type parameters while the type checking itself is executed. To actually type check the program, you have to check whether two pieces of code produce the same range of possible values. This is known as extensional function equality and this is the part that is known to be undecidable in general.
Now, it might be true that dependent type checking becomes decidable if only compile-time constants are used as dependent type parameters. However, I am not sure about that.
In the comments below, we figured out that the part that seems to be the dependent type parameter should actually not be used for the type checking. Instead, it can be seen as an implicit parameter. It is similar to implicit parameter passing in the Scala programming language.
The question is:
Given a list of String, find a specific string in the list and return
its index in the ordered list of String sorted by mergesort. There are
two cases:
The string is in the list, return the index it should be in, in the ordered list.
The String is NOT in the list, return the index it is supposed to be in, in the ordered list.
Here is my my code, I assume that the given list is already ordered.
For 2nd case, how do I use mergesort to find the supposed index? I would appreciate some clues.
I was thinking to get a copy of the original list first, sort it, and get the index of the string in the copy list. Here I got stuck... do I use mergesort again to get the index of non-existing string in the copy list?
public static int BSearch(List<String> s, String a) {
int size = s.size();
int half = size / 2;
int index = 0;
// base case?
if (half == 0) {
if (s.get(half) == a) {
return index;
} else {
return index + 1;
}
}
// with String a
if (s.contains(a)) {
// on the right
if (s.indexOf(s) > half) {
List<String> rightHalf = s.subList(half + 1, size);
index += half;
return BSearch(rightHalf, a);
} else {
// one the left
List<String> leftHalf = s.subList(0, half - 1);
index += half;
return BSearch(leftHalf, a);
}
}
return index;
}
When I run this code, the index is not updated. I wonder what is wrong here. I only get 0 or 1 when I test the code even with the string in the list.
Your code only returns 0 or 1 because you don't keep track of your index for each recursive call, instead of resetting to 0 each time. Also, to find where the non-existent element should be, consider the list {0,2,3,5,6}. If we were to run a binary search to look for 4 here, it should stop at the index where element 5 is. Hope that's enough to get you started!
I'm new to coding.
How do I return a false if there is a string being added that's already in the arraylist?
For example, if you have a list of dog names in the class and you add new dog names in the list, but don't add it when the same dog name was already in the list?
The Solution:
You could use a for statement to iterate through your array list:
public static bool checkArray(string dogName)
{
for int i=0; i<arrayName.Length; i++) // basic for loop to go through whole array
{
if (arrayName[i] == dogName) //checks if array value at index i is the dog's name
{
return true; //if it is, return true
}
}
return false; //gone through whole array, not found so return false
}
This means you can call your method via
string Name = "myDogsName";
bool isAlreadyPresent = checkArray(Name);
Note
This is written in C#, and so other coding languages will slightly
differ in their syntax.
isAlreadyPresent will then contain a bool value if the dog is
present or not
I have written this (for learning purposes) in (possibly) an
inefficient way, but should allow you to understand what is happening
at each stage.
the i++
The i++ may confuse new programmers, but effectively it is the same as writing
i = i + 1;
This also works for i--;
i = i - 1;
Or even i*=2;
i = i * 2;
I am trying to count the number of nodes in a Binary Search Tree and was wondering what the most efficient means was. These are the options that I have found:
store int count in the BST Class
store int children in each node of the tree which stores the number of children under it
write a method that counts the number of Nodes in the BST
if using option 3, I've written:
int InOrder {
Node *cur = root;
int count = 0;
Stack *s = null;
bool done = false;
while(!done) {
if(cur != NULL) {
s.push(cur);
cur = cur->left;
}
else {
if(!s.IsEmpty()) {
cur = s.pop();
count++;
cur = cur->right;
}
else {
done = true;
}
}
}
return count;
}
but from looking at it, it seems like it would get stuck in an infinite loop between cur = cur->left; and cur = cur->right;
So which option is the most efficient and if it is option 3, then will this method work?
I think the first option is the quickest and it only requires O(1) space to achieve this. However whenever you insert/delete an item, you need to keep updating this value.
It will take O(1) time to get the number of all the nodes.
The second option would make this program way too complicated since deleting/inserting a node somewhere would have to update all of its ancestors. Either you add a parent pointer so you can adequately update each one of the ancestors, or you need to go through all the nodes in the tree and update the numbers again. Anyway I think this would be the worst option of all three.
The third option is good if you don't call this many times since the first option is a lot quicker, O(1), than this option. This will take O(n) since you need to go through every single node to check the count.
In terms of your code, I think it's easier to write in a recursive way like below:
int getCount(Node* n)
{
if (!n)
return 0;
return 1 + getCount(n->left) + getCount(n->right);
}
Hope this helps!
I am trying to make a script that dynamically generates world chunks by making a height map then filling out the terrain blocks from there. My problem is creating a 2 dimensional array of objects.
public class Chunk
{
public Block[,] blocks;
Generate(){
//code that makes a height map as a 2 dimensional array as hightmap[x,y]=z
//convert heightmap to blocks
for (int hmX = 0; hmX < size; hmX++)
{
for (int hmY = 0; hmY < size; hmY++)
{
blocks[hmX, hmY] = new Block(hmX, hmY, heightmap.Heights[hmX, hmY], 1);
}
}
}
}
this is giving me the error:
NullReferenceException was unhandled, Object reference not set to an
instance of an object.
You just need to add new before the loop:
Block[,] blocks = new Block[size,size];
Or rather, within the generate function (all else the same):
blocks = new Block[size,size];
Otherwise you'll be shadowing the original 'blocks' variable.