data-structures

Why are Binary Trees Important?

Why we do we study binary trees specifically? As in a general m-way search tree is not given as importance as binary trees in DataStructure textbooks. Does the use of a binary tree exceed m-way trees? ...

Please explain the algorithm for this problem from code jam 2009

This is problem from round 1B 2009 Problem C "Square Math". I know contest analysis is posted. But I am not getting on how to implement BFS when a node can be visited more than once. I could only implement using DFS. (because context is saved implicitly in recursive DFS). How to do that using BFS? ...

Tricky data conversion excercise

I have weather history data which I need to plot on a web page using (highly recommended) jquery flot. The app behind is in RoR. One of the data series describes overall weather state for a date such as 'heavy showers', 'light snow', etc. Now flot expects decimals in data input. Flot also allows to reformat data when it comes down to s...

Whats a better strategy for storing log data in a database?

Im building an application that requires extensive logging of actions of the users, payments, etc. Am I better off with a monolithic logs table, and just log EVERYTHING into that.... or is it better to have separate log tables for each type of action Im logging (log_payment, log_logins, log_acc_changes)? For example, currently Im loggi...

Is there an existing solution to the multithreaded data structure problem?

I've had the need for a multi-threaded data structure that supports these claims: Allows multiple concurrent readers and writers Is sorted Is easy to reason about Fulfilling multiple readers and one writer is a lot easier, but I really would wan't to allow multiple writers. I've been doing research into this area, and I'm aware of ...

What is the best way to organize a group of groups in Obj-c?

I have an unusual problem where I have a list of fruits (apple, orange, banana, grapes, etc etc). They can be organized into small groups for example: Group 1: apple, green_apple, pineapple Group 2: grapes, banana, strawberries, apple Group 3: orange, grapes, green_apple Each group is then associated with a price. So Group 1->9.99, Gr...

List of fundamental data structures - what am I missing?

I've been studying my fundamental data structures a bunch recently, trying to make sure I've got them down cold. By "fundamental", I mean the real basic ones. Fancy ones like Red-Black Trees and Bloom Filters are clearly worth knowing, but they're usually either enhancements of fundamental ones (Red-Black Trees are binary search trees w...

Choosing among alternatives in a Haskell algebraic datatype

When type X is defined as: data X = X { sVal :: String } | I { iVal :: Int } | B { bVal :: Bool } and I want the Int inside an X value, if there is one, otherwise zero. returnInt :: X -> Int How can I determine which type of X the argument to returnInt is? ...

Optimizing binary tree inserts to O(1) with hash map for write heavy trees

First of all I assume I've missed something major when thinking about this, but I still wanted to post about it to see if I really didn't miss anything, on with it... I have a pretty write heavy binary tree (about 50/50 between writes and reads), and on the way home today I was thinking about ways to optimize this, especially make the w...

Parsing line and selecting values corresponding to a key

there is a set of data which is arranged in a specific manner (as a tree), as is given below. basically a key=value pair, with some additional values at the end, which informs how many children does the branch have and some junk value. 11=1 123 2 11=1>1=45 234 1 11=1>1=45>9=16 345 1 11=1>1=45>9=16>2=34 222 1 11=1>1=45>9=16>2=34>7=0 2234...

Clojure data structure traversal/searching.

I'd like to be able to do something like this: (search data list? (fn [x] (and (list? x) (= 4 (first x)))) (fn [x] (and (set? x) (contains x 3)))) And have it recursively search a nested data structure data: first for the shallowest lists (might be in a set of sets, for example). then within those lists for the shallowest li...

Representing location data in Django Models

I'm having trouble deciding on how exactly to represent location data in a project I'm starting. The data is essentially a floor plan, with things existing at points. My first approach was to create a Point class which things will ForeignKey to. However, I quickly realized several problems. The first is that I need a way to represent...

Data Structure Interview Question

I was asked the following Question: How would you store the data given below(which data structure would u pick): A-1-2-3-4-5-6 | B-7-8-9-10-11 | C-12-14-15-16-17 My ans: Since this looks like a bunch of lists with its head nodes linked together. Use two node types one id the regular node type with the following definition: Struct...

Searching a number in a rotated sorted Array

Given a Sorted Array which can be rotated find an Element in it in minimum Time Complexity. eg : Array contents can be [8, 1, 2, 3, 4, 5]. Assume you search 8 in it. ...

Grouping related items in PHP

I have some rows in a database which represent line items in a sale. Each line item has five columns: id : Primary key. sale_id : The sale that this line-item is a part of (a foreign key to a Sales table). product_id : The product this item corresponds to (a foreign key to a Products table). group : The line-items group that this item ...

Static allocation of huge amounts of memory inside the main function

I have a program that has to declare a huge integer array of the size 1000000 in C (using GNU GCC compiler). I tried to declare the array in two different ways. The two possible codes are : #include <stdio.h> int arr[1000000]; int main() { return 0; } and #include <stdio.h> int main() { int arr[1000000]; return 0; } The l...

3d modeling for data structures

Hi, I'm looking for a 3D modeling/animation software. Honestly, I don't know if this is something achievable - but what I want to have is some kind of visual representation of various ideas. Speaking in future tense: if I were to read about of the boot process of an OS, I would visualize the various data structures building up; and I c...

Is there a way to produce a binary diff on two byte arrays in c# .net?

I am trying to compare two byte arrays in memory and produce a data structure to hold the differences so that with byte array B and the data structure holding the differences it is possible to recreate byte array A. The size of the byte arrays are always identical and are relatively small. The byte arrays represent bitmaps which typica...

Why doesn't the .Net framework have a priority queue class?

There are some threads on Stack Overflow dealing with implementing priority queues in .Net and C#. My issue is of a more basic nature: Why isn't there a priority queue out of the box in the .Net framework? Even the C++ Standard Library has one. ...

Appropriate data structure for table that uses ranges

I have a table that looks like this: <22 23-27 8-10 1.3 1.8 11-13 2.2 2.8 14-16 3.2 3.8 and it goes on. So I'd like to lookup a value like this: lookup(11,25) and get the response, in this case 2.8. What is the best data structure to use for this? I have the data in CSV format. I'm looking to program this in ...