4.4 - Fundamentals of Algorithms Flashcards
(11 cards)
What is a tree?
A tree is a connected, undirected graph with no cycles.
What are the 3 types of tree-traversal algorithms?
In-order
Pre-order
Post-order
What is a recursively-defined procedure?
What must it have to be recursive?
What can it be used for?
Recursion (or a recursive procedure) is one that calls itself. It is used when a problem can be broken down, and the same algorithm is repeatedly used on each sub-problem.
It MUST have an end condition, and any recursive algorithm can be written using iteration instead (and vice versa).
It can be used for:
- Sorting agorithms
- Parsing XML/HTML
- Working with files
- In directory structure
What is pre-order tree traversal used for?
- Copying a tree
What is in-order tree traversal used for?
- Binary earch trees
- Outputing tree nodes in order (print method)
- Polish notation
What is post-order tree traversal used for?
- Reverse polish notation
What is the issue with infix notation
- There can be ambiguity over the order of operations e.g.
6 + 4 x 2
1. (6 + 4) x 2
OR
2. 6 + (4 x 2)
What is pre-fix notation?
Mathematical notation where the operator is at the start of the operands
What is post-fix notation?
Mathematical notation where the operator is at the end of the operands
How do we implement post-fx notation using a stack?
- Read the numbers onto the stack
- When you reach an operator, pop that last 2 numbers off, apply the operator to them
- Push the result back onto the stack
What is advantage of using post-fix (reverse polish) over pre-fix (polish) notation?
In reverse polish, the operators appear in the order required for completion, therefore are easier to implement using a stack.