100 likes | 374 Views
Data Structures. Data Structures. How long an algorithm takes often depends on how the data is organized Different data structures will give different running times It’s important to use the appropriate data structure for your task. Linear Structures. Arrays A ccess any element in O(1) time
E N D
Data Structures How long an algorithm takes often depends on how the data is organized Different data structures will give different running times It’s important to use the appropriate data structure for your task
Linear Structures • Arrays • Access any element in O(1) time • Inserting and Deleting an element can cost O(n) time • The underlying structure for others, like Strings • Linked list • Singly or doubly linked • Node with pointer to next one • Access first in list in O(1) time • Access general element in O(n) time • Insert and Delete in O(1) time
Linear Structures • Stack • FILO • Queue • FIFO • Priority Queue • Each element given priority, and elements are deleted in priority order These are all implemented using some other structure, like an array.
Graphs Collection of nodes (or vertices) and edges May be directed or undirected May or may not have a loop May or may not be weighted Are usually connected We talk about paths in the graph and simple paths
Trees A type of graph: A connected, acyclic graph Refer to its root and leaves Refer to parents, children, ancestors, descendants, and siblings Have height, have subtrees |E| = |V| -1
Binary Tree Tree with at most 2 children for each node Usually draw root at the top Can be complete, or full
Sets Unordered collection No duplicates allowed (elements are distinct) Should support membership, union, and intersection
Dictionary Way to look up key, value pairs Must support adding and deleting elements Usually thought of as either “associative arrays” or a hash table (HashMap in Java) Carol 123 Junior 424 Hope 829 Bob 701
What data structure? Receive calls and answer them in the order they were received Send backlog orders to customers based on the priority of the order Represent the layout of city streets, compute the route from point A to point B