(W11) Data Structures Flashcards
(14 cards)
What is the special property of an AVL tree
It’s a self balancing binary tree
What is the definition of balanced in an AVL tree & how do you calculate it?
Balance = height(left) - height(right)
importantly, a tree is balanced if, for each node, at most, the height of the right and left subtree differ by at most 1
AVL Tree circumstance:
Balance Factor = 2, left child has a balance of 0 or 1
Left-Left case (only 1 operation)
AVL tree circumstance:
Balance Factor = 2, left child has a balance of -1
Left-right case
Perform left on the child, right on the parent
AVL tree circumstance:
Balance Factor = -2, right child has a balance of 0 or -1
Right-right case
AVL tree circumstance:
Balance Factor = -2, right child has a balance of 1
Right-left case
Perform right on the child, left on the parent
Time Complexity of AVL operations?
O(log n) for all operations. this is because the BST tree is balanced. rotations are O(1)
except traversal O(n)
What are the types of nodes in a 2-3 Tree?
2-nodes: single key, two children
3-nodes: 2 keys, three children
How to insert into a 2-3 Tree
- Do a search, find the leaf node
○ If the leaf is a 2-node, turn it into a three node, and add the key
○ If it’s a 3-node, temp make it a 4 node - If there is a 4 node, then promote the middle key. Do this recursively (including if you need to add a new root node)
Do you ever increase the height of a 2-3 tree from the base?
No, if the height of the tree ever increases, it will be the result of adding a node to the parent
What are the different complexities of operations in a 2-3 tree?
O(log n) for insert/search/delete
What is the practical purpose of a Red-Black-Tree?
It’s a faster implementation of 2-3 tree. Lower constants
What are the 4 properties of a red-black tree?
The colour of a non-root node is determined by the colour of its
single incoming edge. The root is by definition black.
All red links lean left, i.e., all red links go to a left child.
No node is adjacent to two red edges.
The tree has perfect black balance: the number of black edges
between any node and each of its descendant leaves are the same.
AVL trees vs RBTs regarding complexity?
In terms of worst-case complexity - they are both equivalent
AVL trees are more strictly balanced - so this makes them better for lookups (in terms of concrete efficiency)
RBT’s are better for insertion (less rotations that need to be done)