data-structures

Why Binary Search Trees?

I was reading binary search tree and was thinking that why do we need BST at all? All the things as far as I know can also be achieve using simple sorted arrays. For e.g. - In order to build a BST having n elements, we requires n*O(log n) time i.e. O(nlog n) and lookup time is O(log n). But this thing can also be achieve using array. We ...

Can I define a map whose key is a structure?

and how can I do it in C++? ...

In Java, what's the most performant (time, memory) way to map longs to objects?

I'm storing a (possibly large) number of objects in memory that I want to refer to by long identifiers. Currently, I'm using a standard HashMap<Long, MyClass>. I was wondering if there might be a better way to do it, since intuitively, I'd think that wrapping long in a Long doesn't really make sense. Note that, as of now, this question ...

Counting repeated words in a file

hi, Goal: to find count of all words in a file. file contains 1000+ words My approach: use a HashMap() to store and count the number of times each word appears in the file. Question: Would a HashMap() be the best way or would it be better to use a Binary Tree for ensuring faster lookup as there is a large count of words in the file? ...

Is there a benefit to creating a very generic data model for a Rails 3 Project?

A Product can have lots of things said about it, I'll call them Properties. It can have a brief description. But that description can be in multiple languages. Likewise, it can have multiple Prices, some which are specific to Customers. Given the following data: Product: identifier: 123-ABC Price: value: $1.25 currency: ...

C# - How to return Dictionary or HashSet as read-only collections ?

Possible Duplicate: Is there a read-only generic dictionary available in .NET? Hi everyone, I realized that it was possible to return lists as read only collections: var list = new List<string>(); return list.AsReadOnly(); I wanted to write a similar piece of code for data structures like Dictionary or HashSet but I didn’...

Why is this while loop stuck in infinite loop?

I'm working on a hybrid data structure that is an ordered double-linked list where each node contains an array[] of SIZE. My difficulty is the add method. Using unit testing provided by the professor, a test string is broken up into individual characters and added to the list using a provided comparator. The default test is abcdefghij...

How to add node at some specific index in a linked list?

void insertLoc(int n, int i) inserts a node with info n after the ith location in the list. If the ith location does not exist in the list then the program should exit with an error message. Can anyone help me with the code please... #include<iostream> #include<process.h> using namespace std; struct node { int info; node *n...

How is CPython's set() implemented?

I've seen people say that set objects in python have O(1) membership-checking. How are they implemented internally to allow this? What sort of data structure does it use? What other implications does that implementation have? Every answer here was really enlightening, but I can only accept one, so I'll go with the closest answer to my o...

In a tree data structure, is a node a sibling of itself?

I am building a tree like data structure. What is the expected behavior if I have a method public Set getSiblingNodes(Node node); Should this method return a set including or excluding itself? Thanks! ...

Min heap is, but is a max heap module defined in python?

Possible Duplicate: What do I use for a max-heap implementation in Python? Python has a min heap implemented in the heapq module. However, if one would want a max heap, would one have to build from scratch? ...

Storing graphs in fully-normalized relational databases

I've nearly killed myself, multiple times, trying to find the perfect, flexible schema for storing many different types of objects with a wide variety of links between them in a relational database. But models such as EAV and things like polymorphic relationships just aren't true to relational databases. After learning about NoSQL graph...

CocoaTouch: a dictionary that hashes pointers, without NSCopying

I'm looking for a better way to do this: id key, value; NSMutableDictionary dict; [dict setObject:value forKey:[NSNumber numberWithInt:(int)key]] That is, use an NSDictionary to map from address to object. ...

How to make heapq evaluate the heap off of a specific attribute?

I wish to hold a heap of objects, not just numbers. They will have an integer attribute in them that the heap can sort by. The easiest way to use heaps in python is heapq, but how do I tell it to sort by a specific attribute when using heapq? ...

question, the best data structure and algorithm

I have a question about implementing a data structure (with algorithm) that has these features: There are too many places (like stores), and each place can store too many items, I want to store items in the places. The point is we need to have optimized insertion (or deletion) and optimized search feature. Search can be done based on p...

How to check if my AVL tree implementation is correct?

Hello, guys. I think I've created an AVL tree implementation, but as AVL Tree is quite a complex structure, I need to test it. So the question is - how can I test it? Have you got any ideas? Up to this moment I have the following tests: basic sanity check - checks that for every node height equals max. height of child nodes + 1...

Using jQuery to process a JSON object

My JSON object is constructed like this: var Source = { Object: [ //Array {Title: 'Test', Type: 'Pet', Category: 'Cat', Description: 'Fluffy', Count: 2 } ] }; I was able to figure out how to properly add to the 'Object' array, but I can't seem to figure out the jQuery syntax to query the object based on the property li...

IPv6 lookup data structure

A patricia trie is the well-know, recommended data structure for storing IPv4 allocations/assignments and performing lookup. Is this true for IPv6 adddresses too? Just a deeper/taller trie to accommodate the extra 96 bits? Is the trie still patricia, or a different radix trie? ...

Can a Dictionary be sorted by a different key?

I have a requirement to retrieve an item from a data structure by key. But I also have a requirement to traverse that data structure in sorted order, using a field other than the key. Something like this (pseudocode, for illustrative purposes only): var list = new SortedDictionary<TKey, TSortField, TItem>(); How would I do this? Is...

How to efficiently find k-nearest neighbours in high-dimensional data?

So I have about 16,000 75-dimensional data points, and for each point I want to find its k nearest neighbours (using euclidean distance, currently k=2 if this makes it easiser) My first thought was to use a kd-tree for this, but as it turns out they become rather inefficient as the number of dimension grows. In my sample implementation,...