Algorithms Flashcards
What type of data structure does a depth-first traversal use?
1) Stack
2) When a node is visited place it on the stack
3) Once every node connected to that node has been visited, pop that node from the stack
4) In theory, should reach the bottom of the graph before coming back up to the start (LIFO)
What type of data structure does a breadth-first traversal use?
1)Queue
2)Place the first node onto the queue and every node connected to that onto the queue
3)That node has then been fully discovered, so dequeue it
4) Repeat this process until the whole thing is traversed
What is a Pre-Order Traversal used for?
Copying trees - can be performed on any tree
How do you perform a pre-order traversal?
Place a marker on the left side of each leaf
(Root, Left, Right)
What is an In-Order Traversal used for?
To search the contents of a binary tree and output them in the correct order
(Only used on binary trees)
How do you perform an In-Order Traversal?
Place a marker on the bottom of each leaf
What is a Post-Order Traversal used for?
Useful for converting Infix to Reverse Polish Notation
(Can be used on any tree)
How do you perform a Post Order traversal?
Place a marker on the right of each leaf
What is an algorithm?
A set of instructions which completes a task in a finite time and always terminates
What is a linear search?
A search to find an item in an unordered list. You search each item until you find the one you are searching for
What are the Pros/Cons of Linear Search?
Pros -
Easy to program
Cons -
Very inefficient so rarely used in the real world
What is the time complexity of a linear search?
O(N)
What is a binary search?
A search to find an item in an ordered list. Works by looking at the midpoint of a list and determining if the target is higher or lower
What is the time complexity of a binary search?
O(logN)
What is a bubble sort?
An algorithm that sorts a list of data by swapping the position of adjacent items to order them