Fundamentals of Algorithms Flashcards
Convert X = A * (B + C) / D to Reverse Polish Notation
- Use the stack/queue method (input queue, output queue and a operations stack)
XABC+*D/=
When using a stack to evaluate a RPN statement, what do you need to do to how many elements in the stack?
You need to pop the previous 2 elements and use them in the equation
Why is RPN used? (3 key ones)
- eliminates need for brackets
- makes it simpler to code
- makes it simpler for the computation
Where is RPN used?
Interpreters based on a stack for example Postscript and bytecode
CHALLENGE: Convert This infix expression to RPN -
(A+(C+DF+TA)*B/C)
Practice more questions like this!
ACDF+TA+B*C/+
What are the 3 common algorithms for traversing trees?
Pre-order, post-order, in-order
How is a tree traversal different from a graph traversal?
it has no cycles/loops making it less complex
What is the process for a pre-order tree?
Visit the root node and process it
Visit the node to it’s right and process it
THEN visit the node to it’s right and process it (if not then go left and process it)
Once the right side of the tree is processed, begin the same process on the left
What is the process for a In-order tree?
For a binary tree, it will be processed alphabetically
What is the process for a post-order tree?
Start from the bottom leftmost node on the right side of the root node and process it
Then process the bottoms of the tree and then go up
the root should come at the end of the traversal
Write a pseudocode for a pre-order traversal?
Tip: Use a procedure and use the attributes for node as ‘node.left’, ‘node.right’ and ‘node.data’
PROCEDURE pre_order_traversal(node)
PRINT(node.data)
IF node.left != Null THEN
pre_order_traversal(node.left)
ENDIF
IF node.right != Null THEN pre_order_traversal(node.right) ENDIF ENDPROCEDURE
Write a pseudocode for an in-order traversal?
Tip: Use a procedure and use the attributes for node as ‘node.left’, ‘node.right’ and ‘node.data’
PROCEDURE in_order_traversal(node)
IF node.left != Null THEN
in_order_traversal(node.left)
ENDIF
PRINT(node.data)
IF node.right != Null THEN in_order_traversal(node.right) ENDIF ENDPROCEDURE
Write a pseudocode for an in-order traversal?
Tip: Use a procedure and use the attributes for node as ‘node.left’, ‘node.right’ and ‘node.data’
PROCEDURE in_order_traversal(node)
Write a pseudocode for an post-order traversal?
Tip: Use a procedure and use the attributes for node as ‘node.left’, ‘node.right’ and ‘node.data’
PROCEDURE post_order_traversal(node)
IF node.left != Null THEN
post_order_traversal(node.left)
ENDIF
IF node.right != Null THEN
post_order_traversal(node.right)
ENDIF
PRINT(node.data)
ENDPROCEDURE
What can an in-order traversal of a binary tree show?
it can be used to access the values of a binary search tree in ascending order