Data Structures Flashcards
What are data structures?
These are collections of values, the relationships among them, and the functions or operations that can be applied to the data.
What is a (Singly) Linked List?
A data structure that contains a head, tail, and length property. It consists of nodes, and each node has a value and a pointer to another node, or null.
Describe the steps for the push method on a Singly Linked List in pseudocode.
- the function accepts a value
- create a new node using the passed value
- if no head property on list, set the:
- head and tail property of the list to the new node
- otherwise set the:
- next property on the tail node to the new node
- tail property on the list to the new node
- increment the list length property by one
- return the linked list.
Describe the steps for the pop method on a Singly Linked List in pseudocode.
- the function accepts no value
- if no nodes in the list, return undefined
- loop over the list, until reaching the tail
- set the tail of list to be the 2nd to last node
- decrement the length of list by 1
- return the value of removed node
Describe the steps for the shift method on a Singly Linked List in pseudocode.
- the function accepts no value
- if there are no nodes, return undefined
- store current head property of list in variable
- set head property of list to the current head’s next property
- decrement length by 1
- return the value of node removed
Describe the steps for the unshift method on a Singly Linked List in pseudocode.
- this function accepts a value
- creates a new node with the passed value
- if list has no head property, set the:
- head and tail to new node
- otherwise, set the
- new node’s next property to the current head
- set the head property to the new node
- increment list length by 1
- return the linked list
Describe the steps for the insert method on a Singly Linked List in pseudocode.
- this function should accept an index and a value
- check for valid index value, otherwise return null
- create new node with value
- if the index is 0, unshift new node to start of list
- if index is list length, push new node to end of list
- otherwise, get existing node at position of index - 1
- set next property of new node to next of existing node
- set next property of existing node to new node
- increment length
- return true
Describe the steps for the remove method on a Singly Linked List in pseudocode.
- this function should accept an index
- check for valid index value, otherwise return null
- if the index is 0, shift from start of list
- if index is list length - 1, pop from end of list
- otherwise, get existing node at position of index - 1
- set next property of existing node to be the next of the next node
- decrement list length
- return value of removed node
What are the Big O time complexities of Singly Linked Lists for:
- Insertion at beginning or end
- Insertion
What is a Queue? Give some examples what they are used for.
The concept of an ordered collection of data items, where the addition of new items (enqueue) takes place at one end, and the removal of existing items (dequeue) always takes place at the other end.
It is a FIFO (First In First Out) data structure.
They are used for the order of processing background tasks, uploading resources, printing/taks processing, and much more.
What is a Stack? Give some examples what they are used for.
The concept of an ordered collection of data items, where the addition of new items (pushing) and the removal of existing items (popping) always takes place at the same end.
It is a LIFO (Last In First Out) data structure.
They are used for function invocations, operations like undo/redo, for page routing (back and forth in history), and much more.
What is a Tree? What elements does it consist of?
A non-linear data structure that consists of nodes in a parent / child relationship.
- Root: the top node
- Edge: connection between one node and antother
- Parent: node that has an edge to a child node
- Child: a node directly connected to another, when moving away from the Root
- Siblings: group of nodes with the same parent
- Leaf: node with no children
Name the two main strategies for traversing a tree.
The strategies are:
-
Breadth First Search (BFS)
- search horizontally (siblings) first, then move vertically (children)
-
Depth First Search (DFS)
- search vertically (children) first, then move horizontally (siblings)
What is the difference between a Binary Tree and a Binary Search Tree?
Both have at most two children for each parent, but in a Binary Search Tree every node to the left of a parent is less than its value and every node to the right is greater.
By definition a Binary Search Tree has distinct keys and duplicates are not allowed.