Test 2 Review (Chapter 20 - Binary Search Trees)
method of operation of Binary search trees
-addElement -removeElement -removeAllOccurences -removeMin -removeMax -findMin -findMax
What is the difference between a binary tree and a binary search tree?
A binary search tree has the added ordering property that the left child of any node is less than the node, and the node is less than or equal to its right child.
Binary Search Tree
A binary tree with the added property that the left child is always less than the parent, which is always less than or equal to the right child.
rightleft rotation
A double rotation strategy for re balancing a tree when the long path is in the left subtree of the right child of the root.
leftright rotation
A double rotation strategy for re balancing a tree when the long path is in the right subtree of the left child of the root.
What imbalance is fixed by a leftright rotation?
A leftright rotation will fix the imbalance if the long path is in the right subtree of the left child of the root.
What imbalance is fixed by a single right rotation?
A single right rotation will fix the imbalance if the long path is in the left subtree of the left child of the root.
Assuming that the tree is balanced, what is the time complexity (order) of the addElement operation?
If the tree is balanced, finding the insertion point for the new element will take at worst log n steps, and since inserting the element is simply a matter of setting the value of one reference, the operation is O(log n).
RemoveFirst and first were O(1) operations for our earlier implementation of an ordered list. Why are they less efficient for our BinarySearchTreeOrderedList?
In our earlier linked implementation of an ordered list, we had a reference that kept track of the first element in the list, which made it quite simple to remove it or return it. With a binary search tree, we have to traverse to get to the leftmost element before knowing that we have the first element in the ordered list.
Why does the BinarySearchTreeOrderedList class have to define the iterator method? Why can't it just rely on the iterator method of its parent class, as it does for size and isEmpty?
Remember that the iterators for a binary tree are all followed by which traversal order to use. That is why the iterator method for the BinarySearchTreeOrderedList class calls the iteratorInOrder method of the BinaryTree class.
Our removeElement operation uses the inorder successor as the replacement for a node with two children. What would be another reasonable choice for the replacement?
The best choice is the inorder successor because we are placing equal values to the right.
hy are we able to specify addElement and removeElement operations for a binary search tree but unable to do so for a binary tree?
With the added ordering property of a binary search tree, we are now able to define what the state of the tree should be after an add or remove. We were unable to define that state for a binary tree.
Without the balance assumption, what is the time complexity (order) of the addElement operation?
Without the balance assumption, the worst case would be a degenerate tree, which is effectively a linked list. Therefore, the addElement operation would be O(n).
What is the time complexity of the addElement operation after modifying to implement an AVL tree?
complexity 2*log n or O(log n).
The removeAllOccurrences operation uses both the containss and removeElement operations. What is the resulting time complexity (order) for this operation?
complexity of k2log n or O(log n).