You are on page 1of 2

Binary Search Trees

Part 1. (80%)
Write a program that will insert words from a text file into a binary search tree. The tree must be
sorted in alphabetical order (A-Z). Words in the text file are separated by a newline character.
Your program should support the following BST operations:
o insert
o in-order traversal (ie. printing the contents of the BST from leftmost to rightmost node)
o search
The program should record the time in microsecond precision it takes for each operation to
complete. Use this as a guide to calculating the elapsed time in microseconds.

You are given two text files, both containing the same set of 24,790 words but each in a different
order. Run your program on both files (separately). The program should
1.
2.
3.
4.
5.

insert all words into a binary search tree, sorted from A-Z
print all the words in order (traverse the tree in order)
search for the first word (in this case, it is "A")
search for the last word (in this case, it is "pe's")
display the completion times for processes 1-4

Example:
// words.txt
apple
dog
whales
circuit
ankle
feet
// your program
./me09 words.txt
ankleapple
circuit
dog
feet
whales
Insert: 0.5 seconds
In-order traversal: 0.001 seconds
Search for "ankle": 0.00001 seconds
Search for "whales": 0.00001 seconds

Part 2. (20%)
Submit your program along with a text file that briefly answers the following:
1. Compare the insertion time of words from both sets. Is there a significant difference between
the two? Discuss why this might be the case.
2. Compare the tree traversal time of both sets. Unlike the first scenario, these values should be
fairly similar. Why?
3. Compare the search times of the first and last words of both sets. Explain the behaviour of the
search function. Why does it take significantly longer to look for "zoos" in one set? Why does
the search for "A" take different times?

You might also like