A binary search tree is a special case of a binary tree

We can only create a binary search tree of objects that are comparable in some fashion.
Here is a diagram of a binary search tree of integers.

Here is a diagram of a binary search tree of Strings.

This diagram has more detail

This is not a binary search tree

Romeo could be the left child
Comparable, so that you can compare the values
in order to place them in the correct position in the binary search tree
public class BinarySearchTree
{
private Node root;
public BinarySearchTree() { . . . }
public void add(Comparable obj) { . . . }
. . .
class Node
{
public Comparable data;
public Node left;
public Node right;
public void addNode(Node newNode) { . . . }
. . .
}
}
null node reference, look at its data
value
data value of that node is larger than the one you want to
insert,data value is smaller,
continue the process with the right subtree BinarySearchTree tree = new BinarySearchTree();
tree.add("Juliet");
tree.add("Tom");
tree.add("Diana");
tree.add("Harry");
tree.add("Romeo"); 

Which tree is the result of inserting F R E D into an empty binary search tree
F / \ D R \ E
F
\
R
/
E
/
D
F / \ E R / D
add method in the BinarySearchTree Class
public void add(Comparable obj)
{
Node newNode = new Node();
newNode.data = obj;
newNode.left = null;
newNode.right = null;
if (root == null) root = newNode;
else root.addNode(newNode);
}
addNode method of the Node class
class Node
{
. . .
public void addNode(Node newNode)
{
int comp = newNode.data.compareTo(data);
if (comp < 0)
{
if (left == null) { left = newNode; }
else { left.addNode(newNode); }
}
else if (comp > 0)
{
if (right == null) { right = newNode; }
else { right.addNode(newNode); }
}
}
. . .
}
Assume 1000 random integers were inserted into a BST. What is the maximum comparisons needed to find whether the integer 42 is among them?


Consider this BST:
C
/ \
A G
/ \
D K
What is the result of removing C?
G
/ \
A K
/
D
A
\
G
/ \
D K
D
/ \
A G
\
K
You need to keep a set of lots of objects of class X. You don't care about traversal in sorted order, but you want maximum efficiency. What should you choose, and why?