Assignment 9, Due at 11:59pm, Friday Dec 05. (Absolute deadline!)
Objectives
- Implement a Splay Tree's function.
- This is a chance to delve deeper into implementing a "real" data
structure.
- Getting more comfortable with pointers.
What to do
You are to write a C++ program that does the following.
- It keeps reading user's inputs, line by line. Each input line the user
types is supposed to be in one of the following forms:
newtree
insert [list of integer keys]
remove [key]
find [key]
insert [key]
print
exit
where
-
exit
tells your program to quit
-
newtree
creates a new Splay Tree and clear
the old tree.
-
[key]
is any integer such as 5
or -3
-
[list of integer keys]
is a list of integers separated by spaces, such as 5 -3 4 1 20
.
-
insert
inserts the list of keys (integers) into a
Splay tree. If the key already exists in the tree then it is
not inserted.
-
remove
removes a node with the key from the Splay tree.
If the key does not exist in the tree then we report so.
-
find
returns whether the key exists in the tree
-
print
prints the tree in pretty format
- I have written most of the above program, leaving out two functions:
splay()
and remove()
. You can download the source
codes by typing (from your timberlake
account):
wget http://www.cse.buffalo.edu/~hungngo/classes/2014/Fall/250/assignments/A9.tar.gz
gunzip A9.tar.gz
tar -xvf A9.tar
cd A9
make
An executable named splaydriver
will be produced.
I've already implemented the assignment. You can download the binary and run it
under timberlake
wget http://www.cse.buffalo.edu/~hungngo/classes/2014/Fall/250/assignments/hqn-splaydriver
chmod 700 hqn-splaydriver
Here is a sample run of splaydriver
:
$ hqn-splaydriver
UB Splay Tree Driver. Version 0.7
hqn
Please report bugs to hungngo@buffalo.edu
> insert 50 40 30 20 10
50 just inserted
40 just inserted
30 just inserted
20 just inserted
10 just inserted
> print
[10]
/ \
x [20]
/ \
x [30]
/ \
x [40]
/ \
x [50]
> find 50
50 FOUND
> print
_[50]_
/ \
_[20]_ x
/ \
[10] [40]
/ \
[30] x
> find 45
45 NOT FOUND
> print
[40]_
/ \
[20] [50]
/ \
[10] [30]
> insert 32
32 just inserted
> print
_[32]_
/ \
[30] [40]
/ \ / \
[20] x x [50]
/ \
[10] x
> insert 25
25 just inserted
> print
__[25]__
/ \
[20] [32]
/ \ / \
[10] x [30] [40]
/ \
x [50]
> remove 25
25 removed!
> print
_[32]_
/ \
[30] [40]
/ \ / \
[20] x x [50]
/ \
[10] x
> remove 31
** ERROR **
Key 31 NOT removed
> print
_[30]_
/ \
[20] [32]
/ \ / \
[10] x x [40]
/ \
x [50]
> remove 32
32 removed!
> print
_[30]_
/ \
[20] [40]
/ \ / \
[10] x x [50]
> remove 20
20 removed!
> print
[30]
/ \
[10] [40]
/ \
x [50]
> remove 50
50 removed!
> print
[40]
/ \
[30] x
/ \
[10] x
> remove 40
40 removed!
> print
[30]
/ \
[10] x
> remove 10
10 removed!
> print
[30]
> remove 30
30 removed!
> print
> exit
-
Finally, the only file you can modify is
SplayTree.cpp
. You are to supply the body of
the two functions that have TODO: TBD
in the body. I strongly
suggest you read the rest of the code base carefully before starting.
In particular, make sure you understand what leftRotate()
and
rightRotate()
do. Do not modify any other functions' bodies.
Do not add any more data members nor function members.
- When you submit your assignment, you will only submit one
file:
SplayTree.cpp
. We will copy this file to a directory
containing all other files and compile and grade.
Don't modify any other files.
How to submit
submit_cse250 SplayTree.cpp
Note that the above line only works if you logged in to your CSE account and
the file is there. All previous things can be done at home, as long as
you remember to upload the final file to your CSE account and run
the submit script from there.
Grading
- 0 point if your
SplayTree.cpp
does not compile with the
other files intact.
- 10 points if your
SplayTree.cpp
file does compile. In other
words, if you don't do anything, just re-submit the file, you'll get 10 points.
- 30 points if
remove()
works correctly (with or without splaying).
Remember to delete
the pointer to the node that's removed, else you'll get a memory leak.
- 30 points if
splay()
works correctly. Since insert
and find
calls splay, we can test if your splay function works correctly by looking at the tree printed after insert
or find
.
- 30 points if
remove()
works correctly with splaying. In particular, the correct node is splayed up to the root after a removal. And, if we try to remove a node that is not found, then the node right before we hit the NULL
pointer is splayed. (Read the code base!) Please run my implementation to see how the tree is supposed to look like after a removal.