ASSIGNMENT #6: Binary Search Tree



Score: 10 points


This time no graphics, no GUI, just a data structure and some timing tests:

Please program YOUR OWN BINARY SEARCH TREE class. The tree should be able to contain integer numbers (int). Please program the following methods:

o       public boolean insert (int n); // inserts the integer into the tree. If n already exists in the tree, return ‘false’, ‘true’ otherwise.

o       public myTreeNode find(n); // returns a reference to the node containing n, returns ‘null’ if the tree does not contain n.

o       public delete(n); // deletes the node containing n

o       public traverseIn(); // traverses the tree in IN-order and prints the values

 

The (local) class myTreeNode should contain the fields:

o       Int value

o       myTreeNode leftChild, rightChild

o       myTreeNode parent

Please note that the node structure containing a parent-reference is different from the structure in the textbook! It makes it easier to program the delete method (you can use the find method to find the deletion candidate), yet adds a little overhead to the add() structure. Also, each node uses a little more memory.

After you finished the data structure task, please perform the following tests:

A)    Insert 20 random values between 0..10 (yes, between 0 and 10!), count how often ‘insert’ returned ‘null’, print that value and verify the tree content calling ‘traverseIn()’

B)    Write a loop that generates 1000,2000,3000,…10000 random integer values. Each time, insert all values into a NEW tree, i.e. create trees with 1000,2000,3000,…10000 entries. Record and print the times needed for the insertion (…currentTimeMillis()). Which function should be approximated by the time values?  …If the performance times are too small to be measured on your machine, just call each tree build multiple times (inner loop 1..10, for example), but time outside of that loop, of course.

 

That’s all. Good luck!

The assignment is due Sunday, april 4 (sorry for the late posting)