Practical Eight

Binary Search Trees

due : 12am (midnight) 13 May 1997


Part One

Implement a Binary Search Tree (BST) using the object-oriented paradigm. Include in your class methods to perform insertion of elements, deletion of elements and searching of the BST. For this exercise, assume that the elements contain a single integer.

Example :
class Element { public: int anInteger ...

Part Two

Time your search algorithm to determine its comparative efficiency.

In order to test this, insert 500 random numbers (in the range 1-500) into the BST. Then search for the number 123. Repeat this creation and search procedure 100 times and calculate the average time taken to perform the search.

Note: You will have to store the time before starting the 100 runs and subtract this from the time after the 100 runs have been completed. Then divide by 100.

Part Three

Generalise your data structure so that it only contains the methods Add, Remove and Search. Then split up the BST data structure into a base class (containing Add, Remove and Search) and derived class.

Derive a linked list from the same base class. Then generalise your program so that it can store Elements into either a BST or a linked list. Include the ability to time either data structure. Your program must execute the test (as indicated in Part Two) on both data structures (using the same program code) and indicate which of the two is more efficient.

Note :

If you answer all parts of the question, submit only one set of program files (for part three).
If you answer only part two, submit only the program files for part two.
If you answer only part one, submit only the program files for part one.