1.4.2 Data Structures Flashcards
What are the main differences between arrays and
lists?
- Lists can store data non-contiguously whereas arrays store data in order.
- Lists can store data of more than one data type.
What is a linked list?
A dynamic data structure that is used to hold an ordered set of items that are not stored in contiguous locations.
What is a pre-order traversal?
Traversal algorithm in which you traverse the root node, followed by the left subtree then the right subtree.
What is in-order traversal?
Traversal algorithm in which you traverse the left subtree, the root node, then the right subtree.
What is post-order traversal?
Traversal algorithm in which you traverse the left subtree, the right subtree followed by the root node.
How to add data to a linked list?
- Check if the linked list is full
- Create a node and insert data into it
- Find between what 2 nodes the data should be inserted
- Make the previous node point to the current node
- Make the current node point to the next node
How to delete data from a linked list?
- Check if the linked list is empty
- Find the node to delete
- Set the pointer of the previous node to point to the next node (node ahead of the node to be deleted)
How to traverse a linked list?
- Check if the linked list is empty
- Start at the node the start pointer is pointing to
- Output the item
- Follow the pointer to the next item
- Repeat from 3 till end of list is reached
How to add data to a queue?
- Check if the queue is full
- Create a new node
- Insert it into the tail pointer position
- Increment the tail pointer`
How to remove data from a queue?
- Check if the queue is empty
- Output node pointed by the head pointer
- Increment the head pointer
How to add data to a stack?
- Check if the stack is full
- Create a new node
- Insert it into the pointer position
- Increment the pointer
How to remove data from a stack?
- Check if the stack is empty
- Output node in pointer position
- Decrement pointer
How to add data to a binary tree?
- Check if there’s free memory for a new node
- Create a new node and insert data into it
- If tree is empty
a. New node becomes the first item - If tree is not empty
a. Start at root node
b. If the new node is before current node, follow left pointer
c. If the new node is after current node, follow right pointer
d. Repeat from step 4b until leaf node is reached
e. If new node is placed before current node, set left pointer to be the new node
f. If new node is placed before current node, set right pointer to be the new node