Tag Archives: Data Structure

Walking Recursive Data Structures Using Java 8 Streams

via Walking Recursive Data Structures Using Java 8 Streams | Konrad Garus.

The Streams API is a real gem in Java 8, and I keep finding more or less unexpected uses for them. I recently wrote about using them as ForkJoinPool facade. Here’s another interesting example: Walking recursive data structures.

A static kdtree data structure

via mikolalysenko/static-kdtree · GitHub.

kd-trees are a compact data structure for answering orthogonal range and nearest neighbor queries on higher dimensional point data in linear time. While they are not as efficient at answering orthogonal range queries as range trees – especially in low dimensions – kdtrees consume exponentially less space, support k-nearest neighbor queries and are relatively cheap to construct. This makes them useful in small to medium dimensions for achieving a modest speed up over a linear scan.

Note that kd-trees are not the best data structure in all circumstances. If you want to do range searching, here is a chart to help you select one which is appropriate for a given dimension:

Efficiency with Algorithms, Performance with Data Structures

Click to Read: The canonical location for presentations and code fromCppCon 2014.

Persistent data structures for Python

Pyrsistent introduction

Pyrsistent is a number of persistent collections (by some referred to as functional data structures). Persistent in the sense that they are immutable.

All methods on a data structure that would normally mutate it instead return a new copy of the structure containing the requested updates. The original structure is left untouched.

This will simplify the reasoning about what a program does since no hidden side effects ever can take place to these data structures. You can rest assured that the object you hold a reference to will remain the same throughout its lifetime and need not worry that somewhere five stack levels below you in the darkest corner of your application someone has decided to remove that element that you expected to be there.

The following code snippet illustrates the difference between the built in, regular, list and the vector which is part of this library

>>> from pyrsistent import v
>>> l = [1, 2, 3]
>>> l.append(4)
>>> print l
[1, 2, 3, 4]
>>> p1 = v(1, 2, 3)
>>> p2 = p1.append(4)
>>> p3 = p2.set(1, 5)
>>> print p1
pvector([1, 2, 3])
>>> print p2
pvector([1, 2, 3, 4])
>>> print p3
pvector([1, 5, 3, 4])

The collection types currently implemented are PVector (similar to a python list), PMap (similar to a python dict), PSet (similar to a python set), PBag (similar to collections.Counter), PList (a classic singly linked list) and PDeque (similar to collections.deque). There is also an immutable object type built on the named tuple as well as freeze and thaw functions to convert between pythons standard collections and pyrsistent collections.

Pyrsistent is influenced by persistent data structures such as those found in the standard library of Clojure. The data structures are designed to share common elements through path copying () It aims at taking these concepts and make them as pythonic as possible so that they can be easily integrated into any python program without hassle.


Pyrsistent is developed and tested on Python 2.7, 3.2, 3.4 and PyPy (Python 2.7 compatible). It will most likely work on all other versions >= 3.2 but no guarantees are given. 🙂


Pyrsistent is developed with performance in mind. Still, while some operations are nearly on par with their built in, mutable, counterparts in terms of speed, other operations are considerably slower. In the cases where attempts at optimizations have been done, speed has generally been valued over space.

Pyrsistent comes with two API compatible flavors of PVector (on which PMap and PSet are based), one pure Python implementation and one implemented as a C extension. The latter generally being 2 – 20 times faster than the former. The C extension will be used automatically when possible.

The pure python implementation is fully PyPy compatible. Running it under PyPy speeds operations up considerably if the structures are used heavily (if JITed), for some cases the performance is almost on par with the built in counterparts.


pip install pyrsistent


Available at http://pyrsistent.readthedocs.org/

Brief presentation available at http://slides.com/tobiasgustafsson/immutability-and-python/


Tobias Gustafsson https://github.com/tobgu

Christopher Armstrong https://github.com/radix


If you experience problems please log them on GitHub. If you want to contribute code, please fork the code and submit a pull request.

Part 2: Basic Syntax Analysis

Part 2: Basic Syntax Analysis

Last week we looked at transforming a source program into a set of tokens that form the beginning of a more complex data structure, the ‘Abstract Syntax Tree’ or AST. This week we’ll take a look at absorbing these tokens to create such a data structure.

Collection Data Structures In Swift

Collection Data Structures In Swift

Imagine you have an application that needs to work with a lot of data. Where do you put that data? How do you keep it organized and handle it efficiently?

If your program only manages one number, you store it in one variable. If it has two numbers then you’d use two variables.

What if it has 1000 numbers, 10,000 strings or the ultimate library of memes? (Wouldn’t it be nice to be able to find a perfect meme in an instant?) In that case, you’ll need one of the fundamental collection data structures. Fortunately for you, that’s the topic of this tutorial.

Here’s how the tutorial will flow:

  • You’ll review what a data structure is and then you’ll learn about Big-O notation. It’s the standard tool for describing the performance of different data structures.
  • Next up: practicing by measuring the performance of arrays, dictionaries and sets — the most basic data structures available in Cocoa development. This will also double as a rudimentary introduction to performance testing.
  • As you proceed, you’ll compare the performance of mature Cocoa structures with newer, Swift-only counterparts.
  • Finally, you’ll briefly review some related types offered by Cocoa. These are data structures that you might be surprised to learn are already at your fingertips.