ADDS exam prep Flashcards
What is a directed graph
Every point has a line segment that connects to every other point. n(n-1) line-segments
What is an undirected graph
Only one line segment connection between two points.
(n(n-1))*1/2
Define Adjacency list
List that represents a graph as an array of linked lists
How many edges can a tree have?
can have (n-1) edges
What is a tree?
- > Must have all nodes connected
- >Cannot contain cycles
What is the height of a tree?
Longest path from node to leaf
What is the depth of a tree?
Longest path from root to the leaf
What is a binary tree?
A tree with at most two child nodes
What is an ordered, balanced tree?
The tree is organised such that a nodes left child will be less than the parent.
What is the complexity for searching a ordered balanced tree?
O(log n)
What is the worst case complexity of a binary tree search?
complexity n - If they are in one line.
o
\
o
\
o
\
ect
Tree traversal - What is Pre-order?
Give output to image
(node, left, right)
Tree traversal - What is post-order
(left, right, node)
Tree traversal - What is in-order
(left, node, right)
Write post fix for the following seqence
5231+*-4/
Give the output for pre-order, post-order and in-order
pre-order (node, left, right)
/ - 5 * 2 + 3 1 4
Post order - (left right node)
5 2 3 1 + * - 4 /
in-order (left node right)
5 - 2 * 3 + 1 / 4
What are the properties of Binary Search Tree
- Nodes are comparable
- The left subtree of a node contains only values that are less than the node’s own value
- The right subtree of a node contains nly values that are greater thanhe node’s own value.
Binary search tree - Searching
Example, how would you search for the following
Binary search tree - Min and max value
How would you find the max value of a binary tree?
What is the worst case running time?
Binary search tree - How do you insert a number?
For example add the following numbers
If the value is less than the node it is put on the left and if the value is greater than the node it is put on the right.
Binary search tree - Deletion
case 1: Deleated node has no child nodes
BST - Deletion
case 2: The node to be deleted has one child
What do you do?
You
BST - Deletion
Case three - The node to be deleted has two child nodes
Delete 7 node
So you look at the node that you need to delete
1) Replace the node that you are going to delete with the max value from it’s left subtree. In this case it’s 5. So the 7 node has now become a 5.
2) You delete the 5 leaf. Thus the BST still holds. The larger values are on the right and the smaller values are on the left.
What is the performance of a BST?
What is the worst case of a BST search?
o(n) if the are all within a line
AVL - What is the balance factor?
give equation
height(left subtree) - height(right subtree) |
What are the three properties of an AVL tree?
Calculating the balance factor examples
AVL - Single rotation
What type of rotation is needed here?
Complete the rotation
It’s left left heavy so we need to rotate to the right.
AVL - Single rotation
What type of rotation is needed here?
Complete the rotation
Right right heavy which means that you need to do a left rotation.
AVL - double rotation
What type of rotation is needed here?
Complete the rotation