PyTrie Documentation. Release 0.3. George Sakkis

Similar documents
pygtrie Release Jul 03, 2017

Friday Four Square! 4:15PM, Outside Gates

Python - 2. Jim Eng

Recursion. Presentation Subtitle. Brad Miller David Ranum 1 12/19/2005. Luther College. Binary Search Trees. 1 Department of Computer Science

Iterators & Generators

Binary Search Tree (2A) Young Won Lim 5/17/18

CIS192 Python Programming

matchbox Documentation

Dictionaries. Looking up English words in the dictionary. Python sequences and collections. Properties of sequences and collections

CSE : Python Programming. Decorators. Announcements. The decorator pattern. The decorator pattern. The decorator pattern

Programming II (CS300)

CIS192 Python Programming

jxmlease Documentation

Binary Search Tree (3A) Young Won Lim 6/2/18

CSI33 Data Structures

redish Documentation Release Ask Solem

CIS192 Python Programming

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Maps; Binary Search Trees

Final Examination CSE 100 UCSD (Practice)

CSI33 Data Structures

IHIH Documentation. Release Romain Dartigues

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

Binary Trees, Binary Search Trees

Outline. LList: A Linked Implementation of a List ADT Iterators Links vs. Arrays In-class work. 1 Chapter 4: Linked Structures and Iterators

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

freeze Documentation Release 0.7.0alpha Jean-Louis Fuchs

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Princeton University COS 333: Advanced Programming Techniques A Subset of Python 2.7

CS61A Lecture 36. Soumya Basu UC Berkeley April 15, 2013

ECE697AA Lecture 20. Forwarding Tables

Binary Search Tree (3A) Young Won Lim 6/6/18

Binary Search Tree (3A) Young Won Lim 6/4/18

Binary Search Trees. See Section 11.1 of the text.

Chapter 12 Digital Search Structures

COMP4128 Programming Challenges

Basilisk Documentation

Programming Languages and Techniques (CIS120)

Descriptor HowTo Guide Release 3.3.3

TREES. Trees - Introduction

Binary Search Tree. Revised based on textbook author s notes.

CS Programming Languages: Python

CSE100. Advanced Data Structures. Lecture 12. (Based on Paul Kube course materials)

CS 171: Introduction to Computer Science II. Binary Search Trees

Data Structures in Java

Structure and Interpretation of Computer Programs

Dictionaries. Priority Queues

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Object-Oriented Python

Computer Science Foundation Exam

Data Structures and Algorithms. Course slides: Radix Search, Radix sort, Bucket sort, Patricia tree

TREES AND ORDERS OF GROWTH 7

Introduction. Router Architectures. Introduction. Introduction. Recent advances in routing architecture including

Lecture #12: Mutable Data. map rlist Illustrated (III) map rlist Illustrated. Using Mutability For Construction: map rlist Revisited

Counting Words Using Hashing

Sequences and iteration in Python

Crit-bit Trees. Adam Langley (Version )

Lecture Notes on Tries

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

ECE Spring 2018 Problem Set #0 Due: 1/30/18

Walk through previous lectures

Module 8: Binary trees

MUTABLE LISTS AND DICTIONARIES 4

Data Structures and Methods. Johan Bollen Old Dominion University Department of Computer Science

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Mock Documentation. Release Michael Foord

django-crucrudile Documentation

Python Mock Tutorial Documentation

databuild Documentation

speller.c dictionary contains valid words, one per line 1. calls load on the dictionary file

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Script language: Python Data structures

Structure and Interpretation of Computer Programs

Trees. Truong Tuan Anh CSE-HCMUT

Module 9: Binary trees

Red-black trees (19.5), B-trees (19.8), trees

PIC 16: Midterm. Part 1. In this part, you should not worry about why your class has the name that it does.

Lecture 6: A step back

The WSGI Reference Library

Data Structures and Algorithms for Engineers

Data Structures. Lists, Tuples, Sets, Dictionaries

COS 226 Algorithms and Data Structures Fall Final Solutions. 10. Remark: this is essentially the same question from the midterm.

Trees in java.util. A set is an object that stores unique elements In Java, two implementations are available:

Lecture #12: Mutable Data. Last modified: Sun Feb 19 17:15: CS61A: Lecture #12 1

redis-lua Documentation

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Algorithms in Systems Engineering ISE 172. Lecture 5. Dr. Ted Ralphs

Figure 1: A complete binary tree.

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

rumps Documentation Release Jared Suttles

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Structure and Interpretation of Computer Programs

Advanced Python. Executive Summary, Session 1

CS/COE 1501 cs.pitt.edu/~bill/1501/ Searching

CSE 12 Abstract Syntax Trees

pybdg Documentation Release 1.0.dev2 Outernet Inc

Introduction. Introduction. Router Architectures. Introduction. Recent advances in routing architecture including

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Babu Madhav Institute of Information Technology, UTU 2015

Transcription:

PyTrie Documentation Release 0.3 George Sakkis Jun 05, 2017

Contents 1 Usage 3 2 Reference documentation 5 2.1 Classes.................................................. 5 2.2 Trie methods............................................... 6 2.3 Extended mapping API methods..................................... 6 2.4 Original mapping API methods..................................... 7 2.5 Internals................................................. 7 Python Module Index 9 i

ii

PyTrie Documentation, Release 0.3 pytrie is a pure Python implementation of the trie (prefix tree) data structure. A trie is a tree data structure that is used to store a mapping where the keys are sequences, usually strings over an alphabet. In addition to implementing the mapping interface, tries facilitate finding the items for a given prefix, and vice versa, finding the items whose keys are prefixes of a given key K. As a common special case, finding the longest-prefix item is also supported. Algorithmically, tries are more efficient than binary search trees (BSTs) both in lookup time and memory when they contain many keys sharing relatively few prefixes. Unlike hash tables, trie keys don t need to be hashable. In the current implementation, a key can be any finite iterable with hashable elements. Contents 1

PyTrie Documentation, Release 0.3 2 Contents

CHAPTER 1 Usage >>> from pytrie import SortedStringTrie as Trie >>> t = Trie(an=0, ant=1, all=2, allot=3, alloy=4, aloe=5, are=6, be=7) >>> t SortedStringTrie({'all': 2, 'allot': 3, 'alloy': 4, 'aloe': 5, 'an': 0, 'ant': 1, 'are ': 6, 'be': 7}) >>> t.keys(prefix='al') ['all', 'allot', 'alloy', 'aloe'] >>> t.items(prefix='an') [('an', 0), ('ant', 1)] >>> t.longest_prefix('antonym') 'ant' >>> t.longest_prefix_item('allstar') ('all', 2) >>> t.longest_prefix_value('area', default='n/a') 6 >>> t.longest_prefix('alsa') Traceback (most recent call last):... KeyError >>> t.longest_prefix_value('alsa', default=-1) -1 >>> list(t.iter_prefixes('allotment')) ['all', 'allot'] >>> list(t.iter_prefix_items('antonym')) [('an', 0), ('ant', 1)] 3

PyTrie Documentation, Release 0.3 4 Chapter 1. Usage

CHAPTER 2 Reference documentation Classes class pytrie.trie(*args, **kwargs) Bases: _abcoll.mutablemapping Base trie class. As with regular dicts, keys are not necessarily returned sorted. Use SortedTrie if sorting is required. KeyFactory alias of tuple NodeFactory alias of Node init (*args, **kwargs) Create a new trie. Parameters are the same with dict(). classmethod fromkeys(iterable, value=none) Create a new trie with keys from iterable and values set to value. Parameters are the same with dict.fromkeys(). class pytrie.stringtrie(*args, **kwargs) Bases: pytrie.trie A more appropriate for string keys Trie. class pytrie.sortedtrie(*args, **kwargs) Bases: pytrie.trie A Trie that returns its keys (and associated values/items) sorted. class pytrie.sortedstringtrie(*args, **kwargs) Bases: pytrie.sortedtrie, pytrie.stringtrie 5

PyTrie Documentation, Release 0.3 A Trie that is both a StringTrie and a SortedTrie Trie methods The following methods are specific to tries; they are not part of the mapping API. Trie.longest_prefix(key[, default]) Return the longest key in this trie that is a prefix of key. If the trie doesn t contain any prefix of key: if default is given, return it otherwise raise KeyError Trie.longest_prefix_value(key[, default]) Return the value associated with the longest key in this trie that is a prefix of key. If the trie doesn t contain any prefix of key: if default is given, return it otherwise raise KeyError Trie.longest_prefix_item(key[, default]) Return the item ((key,value) tuple) associated with the longest key in this trie that is a prefix of key. If the trie doesn t contain any prefix of key: if default is given, return it otherwise raise KeyError Trie.iter_prefixes(key) Return an iterator over the keys of this trie that are prefixes of key. Trie.iter_prefix_values(key) Return an iterator over the values of this trie that are associated with keys that are prefixes of key. Trie.iter_prefix_items(key) Return an iterator over the items ((key,value) tuples) of this trie that are associated with keys that are prefixes of key. Extended mapping API methods The following methods extend the respective mapping API methods with an optional prefix parameter. If not None, only keys (or associated values/items) that start with prefix are returned. Trie.keys(prefix=None) Return a list of this trie s keys. Parameters prefix If not None, return only the keys prefixed by prefix. Trie.values(prefix=None) Return a list of this trie s values. Parameters prefix If not None, return only the values associated with keys prefixed by prefix. Trie.items(prefix=None) Return a list of this trie s items ((key,value) tuples). 6 Chapter 2. Reference documentation

PyTrie Documentation, Release 0.3 Parameters prefix If not None, return only the items associated with keys prefixed by prefix. Trie.iterkeys(prefix=None) Return an iterator over this trie s keys. Parameters prefix If not None, yield only the keys prefixed by prefix. Trie.itervalues(prefix=None) Return an iterator over this trie s values. Parameters prefix If not None, yield only the values associated with keys prefixed by prefix. Trie.iteritems(prefix=None) Return an iterator over this trie s items ((key,value) tuples). Parameters prefix If not None, yield only the items associated with keys prefixed by prefix. Original mapping API methods The following methods have the standard mapping signature and semantics. Trie. len () Trie. nonzero () Trie. iter () Trie. contains (key) Trie. getitem (key) Trie. setitem (key, value) Trie. delitem (key) Trie. cmp (other) Trie. repr () Trie.clear() Trie.copy() Trie.has_key(key) Internals Tries are implemented as trees of Node instances. You don t need to worry about them unless unless you want to extend or replace Node with a new node factory and bind it to Trie.NodeFactory. class pytrie.node Bases: object Trie node class. Subclasses may extend it to replace ChildrenFactory with a different mapping class (e.g. sorteddict) Variables value The value of the key corresponding to this node or NULL if there is no such key. children A {key-part : child-node} mapping. 2.4. Original mapping API methods 7

PyTrie Documentation, Release 0.3 ChildrenFactory alias of dict numkeys() Return the number of keys in the subtree rooted at this node. 8 Chapter 2. Reference documentation

Python Module Index p pytrie, 3 9

PyTrie Documentation, Release 0.3 10 Python Module Index

Index Symbols cmp () (pytrie.trie method), 7 contains () (pytrie.trie method), 7 delitem () (pytrie.trie method), 7 getitem () (pytrie.trie method), 7 init () (pytrie.trie method), 5 iter () (pytrie.trie method), 7 len () (pytrie.trie method), 7 nonzero () (pytrie.trie method), 7 repr () (pytrie.trie method), 7 setitem () (pytrie.trie method), 7 C ChildrenFactory (pytrie.node attribute), 7 clear() (pytrie.trie method), 7 copy() (pytrie.trie method), 7 F fromkeys() (pytrie.trie class method), 5 H has_key() (pytrie.trie method), 7 I items() (pytrie.trie method), 6 iter_prefix_items() (pytrie.trie method), 6 iter_prefix_values() (pytrie.trie method), 6 iter_prefixes() (pytrie.trie method), 6 iteritems() (pytrie.trie method), 7 iterkeys() (pytrie.trie method), 7 itervalues() (pytrie.trie method), 7 K KeyFactory (pytrie.trie attribute), 5 keys() (pytrie.trie method), 6 L longest_prefix() (pytrie.trie method), 6 longest_prefix_item() (pytrie.trie method), 6 longest_prefix_value() (pytrie.trie method), 6 N Node (class in pytrie), 7 NodeFactory (pytrie.trie attribute), 5 numkeys() (pytrie.node method), 8 P pytrie (module), 1 S SortedStringTrie (class in pytrie), 5 SortedTrie (class in pytrie), 5 StringTrie (class in pytrie), 5 T Trie (class in pytrie), 5 V values() (pytrie.trie method), 6 11