Binary Search Trees A special case of a Binary Tree CS-2851 Dr. Mark L. Hornick
Binary Search Trees have some special properties Each element in the left subtree of an element is less than the root element of that subtree And each element in the right subtree is greater than the root element In a Set, no two elements are ever the same that is, no duplicate elements The left and right subtrees are thus themselves Binary Search Trees CS-2851 Dr. Mark L. Hornick
Not a Binary Search Tree Why? 50 80 30 100 20 40 60 CS-2851 Dr. Mark L. Hornick
Not a Binary Search Tree 50 80 30 100 20 40 60 “60” cannot be in the right branch of 80, since it is less than 80. CS-2851 Dr. Mark L. Hornick
A binary search tree need not be full, complete or a two-tree, but it could be any of those If a binary search tree is full or complete, its height is logarithmic (base 2) in n If a binary search tree is a chain, its height is linear in n. CS-2851 Dr. Mark L. Hornick
Another Linear Height example Even binary search trees that are not chains may have height that is linear in n. In the example at right, there are exactly two elements at level 1, level 2, etc. O(n)=n/2 CS-2851 Dr. Mark L. Hornick
Repeat: Each element in the left subtree is less than the root element of that subtree “less than” is obvious for integer elements What about other types of elements? We assume that the elements contained within the Entries of a BinarySearchTree are objects in a class that implements the Comparable interface: public interface Comparable<E> { int compareTo(E obj); } CS-2851 Dr. Mark L. Hornick
Adding to a Binary Search Tree Beginning at the root of the tree, repeat the following recursive pseudo-code to add target to the tree current = root // begin at the root add(current, target) { if target LESS THAN current add( left child, target) break; if target GREATER THAN current add( right child, target) break; If target == current // can’t have duplicates! break; } The inserted element always becomes a new leaf in the tree CS-2851 Dr. Mark L. Hornick
Add() time performance For adding an element, what is the worst case? What is the worst height? The worstTime (n) is linear in n if the tree is a chain What is the average height? The averageTime (n) is logarithmic in n if the tree is “bushy” CS-2851 Dr. Mark L. Hornick
Searching a Binary Search Tree Beginning at the root of the tree, recursively perform the following to determine if the tree contains the target current = root // begin at the root boolean contains( current, target) { if current EQUALS target return true; if target LESS THAN current AND left child not null return contains( left child, target); if target GREATER THAN current AND right child not null return contains( right child, target); } CS-2851 Dr. Mark L. Hornick
Search performance The averageTime(n) for a successful search: The average height of a complete binary search tree is logarithmic in n; so: averageTime(n) is O(log n). The worstTime(n) occurs if the tree is a chain. So worstTime(n) is ???? CS-2851 Dr. Mark L. Hornick