Fundamentals of Algorithms Flashcards

1
Q

Convert X = A * (B + C) / D to Reverse Polish Notation

A
  1. Use the stack/queue method (input queue, output queue and a operations stack)
    XABC+*D/=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When using a stack to evaluate a RPN statement, what do you need to do to how many elements in the stack?

A

You need to pop the previous 2 elements and use them in the equation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why is RPN used? (3 key ones)

A
  • eliminates need for brackets
  • makes it simpler to code
  • makes it simpler for the computation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Where is RPN used?

A

Interpreters based on a stack for example Postscript and bytecode

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

CHALLENGE: Convert This infix expression to RPN -
(A+(C+DF+TA)*B/C)

Practice more questions like this!

A

ACDF+TA+B*C/+

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 3 common algorithms for traversing trees?

A

Pre-order, post-order, in-order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How is a tree traversal different from a graph traversal?

A

it has no cycles/loops making it less complex

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the process for a pre-order tree?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the process for a In-order tree?

A

For a binary tree, it will be processed alphabetically

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the process for a post-order tree?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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’

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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’

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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’

A

PROCEDURE in_order_traversal(node)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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’

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What can an in-order traversal of a binary tree show?

A

it can be used to access the values of a binary search tree in ascending order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What can a reverse in-order traversal of a binary tree show?

A

to access the values of a binary search in descending order