17 - Binary Search Trees
Which of the following is true about a binary search tree? A any binary tree used as a search tree is a binary search tree B a binary search tree has nodes with search keys obeying the property that all keys in the left sub-tree of a node are less than the key value of the node and all keys in the right sub-tree of the node are greater than the key value of the node C a binary search tree uses sub-trees to resolve hash collisions D all of the above
B a binary search tree has nodes with search keys obeying the property that all keys in the left sub-tree of a node are less than the key value of the node and all keys in the right sub-tree of the node are greater than the key value of the node
Suppose a binary search tree is implemented with a node having fields for key, info, left sub-tree, and right sub-tree. Consider the following pseudo-code to search for a key K in binary search tree T, and return the info stored for that key, or return not_found if not found. Assume the return keyword not only returns a value for the function call but also exits from the function and returns to the caller of the function. Function lookup(K, T) begin if T is null then return(not_found) else if (T.key = K) then return(T.info) else if (K < T.key) then return(lookup(K, T.left)) else return(lookup(K, T.right)); end; Which of the following is true? A The lookup function will correctly locate the info associated with a search key, but will traverse through more nodes than are necessary to locate the item B The lookup function will correctly locate the info associated with a search key while traversing through the minimum number of nodes required to successfully lookup the item C This is not a correct lookup function
B The lookup function will correctly locate the info associated with a search key while traversing through the minimum number of nodes required to successfully lookup the item
"Comparable" object belongs to a class that implements the interface
Comparable
A binary search tree is a binary tree whose nodes contain __________ objects
Comparable
T/F When you add entries into an initially empty binary search tree add them in sorted order.
False
Where would the addition "Chad" appear in the following binary search tree? Jared / \ Brittany Megan / \ / \ Brett Doug Jim Whitney
Jared / \ Brittany Megan / \ / \ Brett Doug Jim Whitney / Chad *To add Chad to the binary search tree whose root is Jared: Observe that Chad is less than Jared. Add Chad to Jared's left subtree, whose root is Brittany. To add Chad to the binary search tree whose root is Brittany: Observe that Chad is greater than Brittany. Add Chad to Brittany's right subtree, whose root is Doug. To add Chad to the binary search tree whose root is Doug: Observe that Chad is less than Doug. Since Doug has no left subtree, make Chad the left child of Doug.
The efficiency of the operation "add" with respect to binary search trees
O(h)
The efficiency of the operation "getEntry" with respect to binary search trees
O(h)
The efficiency of the operation "remove" with respect to binary search trees
O(h)
In the worst case, searching a full binary search tree is an ________ operation
O(log n)
If n elements are inserted into a binary search tree resulting in a complete binary tree, what is the worst-case time complexity to look up an element?
O(log(n))
If n elements are inserted into a binary search tree, what is the worst-case time complexity to look up an element?
O(n)
We use the class Comparable's method __________ to compare Comparable objects.
compareTo *The basis for this comparison varies from class to class, depending on the data fields compareTo examines.
In a completely balanced tree, the subtrees of each node have
exactly the same height
In the term "O(h)", "h" stands for
height
To print out the data in a binary search tree in an order that is sorted by the search key, which traversal would be used?
in-order
For the following order: ... a < e < b ... a is called the _______ predecessor of e, and b is the _______ successor of e
inorder
This node is the right-most entry in a nodes left subtree
inorder predecessor
This node is the left-most entry in a nodes right subtree
inorder successor
If a tree only has one node, the root is a _______
leaf
Example of deriving a class "BinarySearchTree" from "BinaryTree"
package TreePackage; import java.util.Iterator; public class BinarySearchTree<T extends Comparable<? super T>> extends BinaryTree<T> implements SearchTreeInterface<T> { public BinarySearchTree() { super(); }
Example of overriding an inherited method to disable it and produce an exception
public void setTree(T rootData) // disable setTree (see Segment 25.6) { throw new UnsupportedOperationException(); } // end setTree public void setTree(T rootData, BinaryTreeInterface<T> leftTree, BinaryTreeInterface<T> rightTree) { throw new UnsupportedOperationException(); } // end setTree
Is the following binary search algorithm recursive or iterative? Algorithm bstSearch(binarySearchTree, desiredObject) // Searches a binary search tree for a given object. // Returns true if the object is found. if (binarySearchTree is empty) return false else if (desiredObject == object in the root of binarySearchTree) return true else if (desiredObject < object in the root of binarySearchTree) return bstSearch(left subtree of binarySearchTree, desiredObject) else return bstSearch(right subtree of binarySearchTree, desiredObject)
recursive
Every node in a binary search tree is the _______ of a binary search tree.
root
A binary search tree has these five basic database operations that it can perform on its entries
search (contains) retrieve (getEntry) add remove traverse
To make searching a binary search tree as efficient as possible, the tree must be as _________ as possible
short *The height of a tree directly affects the length of the longest path from the root to a leaf and hence affects the efficiency of a worst-case search. Thus, searching a binary search tree of height h is O(h).
Trees are said to be height balanced, or simply balanced, if
the subtrees of each node in the tree differ in height by no more than 1
For each node in a binary search tree:
The data in a node is greater than the data in the node's left subtree The data in a node is less than the data in the node's right subtree