QUESTIONS AND SOLUTIONS GRADED
A+.
■ What is the purpose of the Comparable interface in Java? Answer- It
allows objects to define their natural ordering by implementing the
compareTo() method.
■ What does the compareTo() method return? Answer- It returns a
negative integer if the current object is less than the other, a positive
integer if greater, and zero if they are equal.
■ Why can't we use a boolean return type for comparisons in the
Comparable interface? Answer- We need to distinguish between three
outcomes: less than, greater than, and equal.
■ What is the role of immutability in the context of binary trees?
Answer- Immutability prevents modification of the tree after its
construction, which can limit flexibility in adding or removing elements.
■ What is a potential drawback of using immutable trees? Answer-
They cannot be modified later, which may be necessary for dynamic
data structures.
,■ What is the relationship between binary search and binary search
trees? Answer- Binary search trees leverage the sorted order of elements
to perform efficient searches, similar to how binary search works on
arrays.
■ How does the BST order invariant relate to performance
improvements? Answer- It simplifies the code, makes it easier to reason
about, and can lead to significant improvements in runtime complexity.
■ What is the impact of the BST order invariant on the client interface?
Answer- It provides a simpler interface for clients by ensuring that the
tree maintains a specific order.
■ What is the significance of the root element in a BST during a search?
Answer- The search begins at the root, and its value determines whether
to continue searching in the left or right subtree.
■ What is an example of a type that can be stored in a BST? Answer-
Strings can be stored in a BST using their natural alphabetical order.
■ How does the BST order invariant aid in reasoning about the
structure? Answer- It provides a clear set of rules that govern the
relationships between elements in the tree.
, ■ What is the benefit of having a defined order for elements in a BST?
Answer- It allows for efficient searching, insertion, and deletion
operations based on the ordering of elements.
■ What is a potential challenge when implementing a BST for custom
types? Answer- Custom types must implement comparison logic to be
used effectively in a BST.
■ What is the expected time complexity for search operations in a
balanced BST? Answer- O(log n), where n is the number of elements in
the tree.
■ What is the worst-case time complexity for search operations in an
unbalanced BST? Answer- O(n), where n is the number of elements, if
the tree degenerates into a linked list.
■ What is the purpose of the in-order iterator in a BST? Answer- To
traverse the elements of the tree in sorted order.
■ What is a common use case for binary search trees? Answer- Storing
sorted data for efficient retrieval and dynamic updates.
■ How can the structure of a BST affect its performance? Answer- A
balanced structure leads to better performance, while an unbalanced
structure can degrade performance.