Binary Search Trees - Shadows Government

Week 8
(Binary Search Trees)
Definition
A binary search tree T is either empty or a binary tree whose subtrees are
binary search trees where each node of the left subtree is less than or equal to
the root and each node of the right subtree is greater than the root.
Examples 1. The first three of the following are binary search trees but the last two are not.
Searching a binary search tree for a given item
Assume the existence of the function lessthan : item x item  Boolean which determines if
one item is strictly less than another and the function eq : item x item  Boolean which
determines if two items are equal.
tsearch : item x bt(item)  Boolean
tsearch(x, newtree)
=
false
tsearch(x, T)
=
true
if eq(x, data(T)),
tsearch(x, left(T))
if lessthan(x, data(T)),
tsearch(x, right(T))
otherwise.
OR
tsearch(x, T) =
IF isnewtree(T)
THEN false
ELSE IF equal(x, data(T)) THEN true
ELSE IF lessthan(x, data(T)) THEN tsearch(x, left(T))
ELSE tsearch(x, right(T)).
Examples 2. Evaluate the following:
tsearch(6,
) = …………………………………………………………
38
tsearch(6,
) = ………………………………………………….
…………………………………………………………………………………………..
tsearch(6, make(make(, 6, ), 5, make(, 8, )))
……………………………………………………………………………………….
tsearch(6, make(make(, 6, ), 7, make(, 8, )))
= ……………………………………………………………………………………………
Examples 3. Constructing binary search trees from given lists.
Given a list of numbers [5, 8, 2, 12, 10, 14, 9] we can produce the following binary search
tree: (i) put the first element on the root, (ii) compare the next element with the root and put
it on the root of the left (right) subtree if it is less than or equal to (greater than) the root, (iii)
repeat the above process until every element in the list has been put onto the tree.
However the list [9, 12, 10, 5, 8, 2, 14] organised into a binary search tree produces:
39
Inserting an item into a binary search tree
We assume the existence of a function LE : item x item  Boolean, such that LE(x, y) =
true if x is less than or equal to y and false otherwise.
insert : item x bt(item)  bt(item)
insert(x, newtree) =
make(newtree, x, newtree)
insert(x, T)
make(insert(x, left(T)), data(T), right(T))
if LE(x, data(T))
make(left(T), data(T), insert(x, right(T)))
otherwise.
OR
insert(x, T)
=
= IF isnewtree(T) THEN make(newtree, x, newtree)
ELSE IF LE(x, data(T)) THEN make(insert(x, left(T)), data(T), right(T))
ELSE make(left(T), data(T), insert(x, right(T))).
Examples 4. Evaluate the following:
insert(5,
) ………………………………………………………………..
insert(5, make(make(, 3, ), 4, make(, 6, )))
= make(make(, 3, ), 4, insert(5, make(, 6, )))
= make(make(, 3, ), 4, make(insert(5, ), 6, ))
= make(make(, 3, ), 4, make(make(, 5, ), 6, ))) .
40
) = ………………………………………………………….
insert(11,
= ……………………………………………………………………………………..
= ………………………………………………………………………………
= ……………………………………………………………………………
= ……………………………………………………
Constructing a binary search tree from a given list.
With the help of the functions insert : item x bt(item)  bt(item) and
reverse: list(item)  list(item), we are now able to write the following function.
convert : list(item)  bt(item)
convert(L) = IF isnewl(L) THEN newtree
ELSE insert(hd(reverse(L)), convert(reverse(tl(reverse(L))))).
Examples 5. Illustrate the operation of this function by evaluating the following:
(i) convert([8, 9, 5]),
(ii) convert([8, 9, 5, 7, 1]).
41
Solution. (i)
convert([8, 9, 5])
= insert(hd([5, 9, 8]), convert(reverse(tl([5, 9, 8]))))
= insert(5, convert([8, 9]))
= insert(5, insert(9, convert([8])))
= insert(5, insert(9, insert(8, convert([ ]))))
= insert(5, insert(9, insert(8, )))
= insert(5, insert(9, make(, 8, )))
= insert(5, make (, 8, insert(9, )))
= insert(5, make(, 8, make(, 9, )))
= make(insert(5, ), 8, make(, 9, ))
= make(make (, 5, ), 8, make(, 9, )).
The pictorial representation of this binary search tree is
(ii)
convert([8, 9, 5, 7, 1])
= ……………………………………………………………………………….
= ………………………………………………………………………………..
= ………………………………………………………………………………..
= ……………………………………………………………………………..
= ……………………………………………………………………………
= ………………………………………………………………………………
= ……………………………………………………………………………….
= …………………………………………………………………………………
The pictorial representation of this binary search tree:
42