2.3.1 - Algorithms (Data Structures) Flashcards
What would be output using a breadth-first traversal on the following tree?
Italy, France, Spain, Austria, Germany, Norway
What would be output using a depth-first traversal on the following tree?
Austria, Germany, France, Norway, Spain, Italy
Root node is always last in depth-first (post-order)
Describe the steps involved in a breadth-first traversal
Uses a queue data structure to perform the traversal
- Visit root node
- Visit all direct children
- Visit all children of first child
- Repeat three points for each child visited
Describe the steps involved in a depth-first traversal
Uses a stack data structure to perform the traversal
- Traverse the left subtree
- Traverse the right subtree
- Visit the root node
- Repeat three points for each child visited
In what situation would a depth-first traversal be chosen over a breadth-first traversal?
Depth
- Requires less memory than breadth first search.
- Quicker than breadth if you are looking at deep parts of tree
Breadth
- Is more efficient when the data searched for is closer to the root.
Keywords to describe a binary tree
- Ordered Data Structure
- Child Node
- Parent Node
- Root Node
- Leaf Node
- Sub Tree
- Edge / Branch
Draw this Binary Search Tree
What is the purpose of the root and free pointers in a binary search tree?
Root Pointer - Points to where the tree starts (root node)
Free Pointer - Points to where the next piece of data would be added in the array
Key words to describe a tree (multi-branch)
- No restriction on the number of child nodes per parent
- Un-Ordered Data Structure
- Child Node
- Parent Node
- Leaf Node
- Sub Tree
- Edge / Branch
Write the pseudocode to push an item to a stack
Write the pseudocode to pop an item to a stack
Keywords to describe a queue
- Dynamic Data Structure
- Enqueue
- Dequeue
- First in First Out
- Peek
- Front Pointer
- Rear Pointer
- Circular / Non-Circular
Write the pseudocode to dequeue an item to a queue
Write the pseudocode to enqueue an item to a queue
Describe the purpose of the front and rear pointer in a queue
Front - Where the data will be removed from (dequeued)
Rear - Where the data will be added to (enqueued)