Non-Linear Data Structures Flashcards
What is Big O notation?
Big O is a mathematical notation
Time/space complexity
that describes the limiting behavior of a function where the argument tends toward a particular value or infinity.
When do we use Big O notation?
To determine the scale-ability of an algorithm as input grows large.
What does Big O describe?
The performance on an algorithm
What does Big O have to do with data structures?
Certain operations can be more or less costly depending on what data structure is used (Array, Linked List, Queue, etc)
Name the different time complexities.
What is exponential time complexity?
The opposite of logarithmic time complexity.
The exponential curve grows faster as the input grows.
Which is time complexity more efficient?
log(n)
O(n)
While O(n) grows linearly with greater input,
O(log n) slows down at some point.
This makes O(log n) quadratic time more efficient that O(n) linear time.
At what point does the logarithmic algorithm slow down?
at some point, O(log n) will slow down.
When we reduce our work in half every step.
What is the Big-O for linear search vs. binary search?
linear search = O(n).
Iterate an array.
binary search = O(log n).
search a binary tree.
What are the five most common time complexity curves?
What are non-linear data structures?
- Binary Trees
- AVL Trees
- Heaps
- Tries
- Graphs
When would we have a quadratic Big-O value?
When we use nested loops that each performs an iteration over the entire set of values.
What is logarithmic time complexity?
When we throw out half our items and narrow our search.
o(log n) - binary search tree lookup
What is a tree?
A nonlinear data structure
stores elements in a hierarchy.
The “elements” are referred to as “nodes.”
Each “node” has data or values (could store objects).
The “lines” that connect “nodes” are called “edges.”
What are real-world applications of trees?
Tons of applications
databases
graphical user interfaces
websites
- Hierarchical data (files, folders) - File systems on your computer
- Databases (indexing) - use trees for indexing for fast database lookup.
- Autocompletion (matching previous queries) - Storing old search queries in a tree than trying to match old search queries to new entries.
- Compilers (translate code into new languages)
- Compression algorithms (JEPG, MP3)
What is special about a binary tree?
Allows for logarithmic time complexity
left subtree is smaller than root
right subtree is greater than root
What is the top node of a tree called?
The top node is a tree is called “the root.”
The root node has two children in a binary tree.
What are the children nodes called in a tree?
The children nodes of a root node are called “leaves.”
What is the run time complexity of various operations on binary trees?
Logarithmic time complexity
Lookup ( ) : O(log n)
addItem ( ) : O (log n)
deleteItem ( ): O (log n)
**if a tree isn’t set up properly the performance may degrade to O(n).
What values are we going to use for our binary search tree algorithm implementation?
How do we implement the Node { } class in our binary tree?
How do we implement the addItem( ) method in our binary search tree?
O ( log n ) operation!
How do we implement the lookup( ) method in our binary tree?
O(log n) time complexity
Traversal:
Root, leftChild, rightChild
How do we implement a recursive method for printing nodes in traversePreOrder ( )?
Kick off recursion with method overloading
End with base condition
How do we implement a recursive method for printing nodes in traversePostOrder ( )?
Kick off recursion with method overloading
End with base condition
How do we implement a recursive method for printing nodes in traverseInOrder ( )?
BT How do we implement traverseLevelOrder ( ) in our binary tree class?
O ( n ) operation!
Have to traverse every node in the tree.
BT How do we implement min ( ) in our non-binary tree?
O(n) operation!
Have to traverse all values in tree!
Post order traversal.
We start with the leaves.
We find the min value in the left and right subtrees.
compare with root
How do we implement min ( ) in our binary-search tree?
O(log n) operation!
We are only trying to find the left most leaf.
In every cycle, we are throwing out half of the nodes in the tree.
How do we implement equal ( ) method?
Pre-order traversal
root, leftChild, rightChild
First, compare the root nodes to check if equal.
Then, use recursion to look at their children nodes and compare if equal.
What are two properties of tree nodes?
Depth and height
Depth = start at root, move down
Height = start at leaf, move up
How do we calculate depth (distance) in our tree?
Counting number of edges from root to node (target)
Height of root is zero
Ex. 8 has a depth of 3
What is recursion?
implementing repetition
when a function calls the method itself
we have a cycle
Terminating recursion:
We will call this method forever!
need a base condition to terminate
Java Implementation:
Java uses a stack for recursion (storing values)
How is recursion executed?
During each recursive method call
return value is stored in a stack
base condition is reached
result of last recursive method is returned to the previous method calls
This continues using the stack
until finally, results of expressions will return from first method call
**Java knows how to get back to previous step using a stack!
How do we calculate height?
longest path from leaf to target node
Height of leaf node is zero
Ex. 20 (root) height is 3
Formula (height of tree)?
1 + max ( height(L), height(R) )
How do we implement the height ( ) method in our binary tree?
What is breadth-first tree traversal?
aka level order traversal
visit all nodes in the level before visiting the child nodes below the level.
Traversing level by level.
What is depth-first traversal?
What is pre-order traversal?
root, left subtree, right subtree
Start at root, go deep to all children and grandchildren
depth traversal!
What is in-order depth traversal?
depth first traversal
start deep at left leaf node, root, right node
values come out in ascending order
reverse for descending order
rightChild, root, leftChild
What is post-order traversal?
depth first traversal
leaves to root
start with left node, right node, branch root
Used in sorting data, heap properties, and mapping data connections.
Why?
start at the leaf nodes to calculate distances (height, depth) before moving to root nodes.
What powers non-linear traversal?
Recursion
calling a function again
used for tree traversal algorithms like HeapSort and BubbleUp.
How does the binary search algorithm work?
It begins in the middle of the data set.
In every set, it narrows the search by half.
with a million data values, we can find our value in only 19 comparisons.
How do we implement a method equal ( ) to validate if two trees are equal?
Popular interview question!
Write a method equal ( ) to validate if two trees are equal
How can we implement isBinarySearchTree ( ) ?
Popular Interview Question:
Write code to validate this binary tree
(Is this tree a binary search tree?)
Hint:
Pre-order traversal
check if each node is in the right range (vs. comparing to other nodes)
How do we implement getNodesAtDistance ( ) in our binary search tree?
Popular Interview Questions:
Write code to print all nodes at distance k from root.
Hint:
use recursion
Think of breaking the problem down into smaller parts.
Decriment the distance as we traverse nodes
eventually distance reaches zero, print nodes
Implement size ( ) method to calculate the size of a binary tree.
Implement max ( ) to return the maximum value in a binary tree using recursion.
If our binary search tree is not structured properly what does our Big-O drop to?
O(n)
Implement countLeaves ( ) to count number of leaves in a binary tree.
implement contains ( ) to check for the existence of a value in a binary tree using recursion.
Implement areSibling ( ) to check to see if two values are siblings in a binary tree.
Implement getAncestors ( ) to return the ancestors of a value in a List
Implement isBalanced ( ) method to check if a binary tree is balanced.
Implement isPerfect ( ) method to check if a binary tree is a perfect binary tree.
What are AVL trees?
Special types of binary trees that self balance themselves.
They do this by ensuring the right and left subtree difference isn’t greater than one on each insertion.
The difference in height between left and right sub trees still equals zero or less than one.
It it’s greater than one, they self-balance using rotations!
rotations come up in interviews.
How do AVL trees work?
If difference between left and right tree is more than one, self balance using rotations.
What does a perfect tree look like?
Every level is full with nodes
Reality?
As we add and subtract values some branches get longer or shorter
What happens to binary trees in reality?
They become unbalanced!
What is an unbalanced binary tree?
The right subtree is taller than the left subtree by more than one.
How do our search trees become unbalanced?
Insert sorted values in ascending or descending order!
Right skewed tree.
Sorted in descending order is a left tree.
What is a right skewed binary tree?
Most nodes have only right children
Like linked-lists
if lookup (value) is in last node, O(n) operation!
What is a left skewed tree?
Worst type of binary tree.
Most nodes have only left children
Like linked-lists
if lookup (value) is in last node, O(n) operation!
What problem do AVL trees solve?
The Big-O of a binary search tree dropping from O(log n) to O(n) due to improper structuring.
How?
Only a balanced tree performs at logarithmic time complexity.
We cannot have a long branch.
Right, left skewed trees are like linked lists lookup (value) = O(n) operation.
How?
Self-balancing mechanism
What is an AVL interview question?
Add integers in a binary tree
show where imbalanced
show how to rotate (draw on whiteboard)
What are the four types of rotations?
The type of rotation depends on what side of the tree is heavy
When do we perform a left rotation?
When the tree is heavy on the right
How?
height of left increases by one (becomes left child)
height of right decreases by one
Calculation:
balanceFactor = height (L) - height (R)
balanceFactor > 1 | left heavy (right rotate)
When do we perform a right rotation?
When tree is heavy on the left side
height of right increases by one (becomes right child)
height of left decreases by one
Calculation:
balanceFactor = height (L) - height (R)
balanceFactor < -1 | right heavy (left rotate)