Java Reference
In-Depth Information
Display 15.40
A Binary Search Tree for Integers
(part 1 of 2)
1
/**
2
Class invariant: The tree satisfies the binary search tree storage rule.
3
*/
4
public
class
IntTree
5{
6
The only reason this inner
class is static is that it is
used in the
static
methods
insertInSubtree
,
isInSubtree
, and
showElementsInSubtree
private
static
class
IntTreeNode
7
{
8
private
int
data;
9
private
IntTreeNode leftLink;
10
private
IntTreeNode rightLink;
11
12
public
IntTreeNode(
int
newData, IntTreeNode newLeftLink,
13
IntTreeNode newRightLink)
14
{
15
data = newData;
16
leftLink = newLeftLink;
17
rightLink = newRightLink;
18
}
19
}
//End of IntTreeNode inner class
20
private
IntTreeNode root;
21
public
IntTree( )
This class should have more methods. This
is just a sample of possible methods
22
{
23
root =
null
;
24
}
25
public
void
add(
int
item)
26
{
27
root = insertInSubtree(item, root);
28
}
29
public
boolean
contains(
int
item)
30
{
31
return
isInSubtree(item, root);
32
}
33
public
void
showElements( )
34
{
35
showElementsInSubtree(root);
36
}
(continued)