The Most Commonly-used Data Structures

advertisement
The Most Commonly-used
Data Structures
Terence Parr
USF
Introduction
Abstract data types
Data structures (implementations)
Combinations
Abstract Data Types
List
stack, queue, prioritized queue, …
Set (unordered, unique)
Dictionary (also called Map)
Graph (directed or undirected)
Tree
Binary tree
Choose via:
access pattern, properties, element relationships
Implementations chosen to optimize
time/space
List
Operations: add, get, insert, delete, find
queue: add to tail, get only from head
stack: add to “top”, delete from “top”
Typical implementation: array or linked
list
Examples: list of users, list of books
Set
Operations: add, delete, contains
Typical implementation: bit vector (if
elements are integers) or hash table
Examples: set of universities, set of
students
Dictionary
Operations: map x->y, get x, delete x
Typical implementation: hash table
Examples:
student -> userid
student -> list of classes
Graph
Collection of nodes connected by directed or
undirected edges with or without labels
Path==sequence of edges
Operations: add node, add edge x->y, delete
node, delete edge
Typical implementation: node has list of
edges that point to other nodes
Examples: network simulation, email trail
between employees (social network), finite
automata
Tree
A kind of directed graph with unique edge
path from node x to y
Children: emanating edges, Root: topmost
node, Leaves: nodes w/o children
Operations: add child, delete a child
Typical implementation: node has list of
children (again, a restricted graph)
Examples: organization chart, class hierarchy,
expression tree
Binary tree: at most 2 children per node
Implementations
Linked list
head, tail pointers
wrapper to hold value and “next”
Operations:
get O(1)
others O(n)
Hash table
Fast implementation of a dictionary; like an
associative memory; maps key to value
Idea: break up large search space into many
smaller spaces
“hash function” tells you which of the smaller
spaces (“buckets”) has element of interest
search linearly within smaller space
simple hash of int: hash(x)=x; hash of string: sum
of char values
Array of lists; each list is a bucket of
key/value pairs
 bucket index(key) = hash(key) % num_buckets
Hash table 2
Tree
Node has list of children; need root ptr
CEO
+
3
*
4
President
5
VP Sales
VP Eng.
Graph
States or nodes have list of edges to
other states
Mary
Jim
Chris
Tim
Jen
Download
Related flashcards
Create Flashcards