Data Structures
Stacks, queues, trees, matrices, heaps, graphs
12 flashcards · Computer Science · by lakesare
LIFO/FIFO stands for?
Last/First In First Out
What is a stack?
A stack is an ordered collection of items that follows the LIFO principle.
Name 2 sides of a stack.
top and base
What is a queue?
A queue is an ordered collection of items that follows the FIFO principle.
What's a priority queue?
Priority Queue is like a regular queue or stack, but where additionally each element has a priority associated with it. Element with high priority is served before an element with low priority.
Name 3 ways to store trees in relational database.
1. parent-child, where each child stores parentId. { name: apple, parentId: 5 } 2. materialized path, where each element stores the full path to the very root. { name: apple, path: '5,4,1' } 3. nested set model, where you execute a preorder tree traversal and store lft and rgt indexes. { name: apple, lft: 5, rgt: 6 } Links: trees in Rails, trees in relational dbs
What's a linked list?
Linked list stores ordered collection of elements; but unlike arrays, in linked lists the elements are not placed contiguously in memory. Each element consists of a node that stores the element itself and also a reference (also known as a pointer or link) that points to the next element.
What's a matrix?
Array of arrays in any n of dimensions. This is a 2-dimensional matrix: [ [12, 7, 21], [45, -2, 14] ] This is a 3-dimensional matrix: [ [ [7, 2, 3], [5, 3, 5] ], [ [8, 9, 3], [1, 2, 2] ] ]
The defining characteristic of a tree is absence of loops, children never point back to parents. What data structure would that be instead?
A graph.
Which of these structures are trees and why?
No, cycle A→A No, cycle B→C→E→D→B Yes No, more than one root No, undirected cycle 1-2-4-3
What's a leaf in a tree?
Node that doesn't have any children.
What's a heap?
Any tree, where child <= parent.