Homework 4
Due Thursday November 1
• Implement an AVL tree stored in a random access file
• Each node contains an integer key, one or more fixed length character strings, a left child reference, a right child reference and a height
• The format of the file is shown on the next slide
• The methods you must implement are shown on the following slides.
• Duplicate keys cannot be entered in the tree
• Your implementation MUST NOT load the whole tree in memory. Each operation only makes copies of the nodes it needs for that operation. Modified nodes must be written back to the file.
• You must create own test driver. I will create my own test driver.
Root Free
numOtherFields
Length1
Length2
AddrContents
0 | 508 | |||||||
8 | 908 | |||||||
16 | 2 | |||||||
20 | 8 | |||||||
24 | 20 | |||||||
28 | 668 | |||||||
108 | 40 | Alonzo | Church | 828 | 588 | 2 | ||
188 | 200 | Alan | Turing | 0 | 0 | 0 | ||
268 | 1068 | |||||||
348 | 90 | Ada | Byron | 0 | 0 | 0 | ||
428 | 28 | |||||||
508 | 80 | Hannah | Arendt | 108 | 748 | 3 | ||
588 | 60 | Kurt | Godel | 988 | 0 | 1 | ||
668 | 0 | |||||||
748 | 100 | George | Eliot | 348 | 188 | 1 | ||
828 | 10 | Anton | Chekhov | 0 | 0 | 0 | ||
908 | 268 | |||||||
988 | 50 | Vladimir | Nabokov. | 0 | 0 | 0 | ||
1068 | 428 |
KeyField1Field2LeftRightHeight
import java.io.*; import java.util.*; public class AVLTree { /* Implements a ALV tree of ints (the keys) and fixed length character strings fields stored in a random access file. Duplicates keys are not allowed. There will be at least 1 character string field */ private RandomAccessFile f; private long root; //the address of the root node in the file private long free; //the address in the file of the first node in the free list private int numFields; //the number of fixed length character fields private int fieldLengths[]; //the length of each field private class Node { private int key; private char fields[][]; private long left; private long right; private int height; private Node(long l, int d, long r, char fields[][]) { //constructor for a new node } private Node(long addr) throws IOException{ //constructor for a node that exists and is stored in the file } private void writeNode(long addr) throws IOException { //writes the node to the file at location addr } } public AVLTree(String fname, int fieldLengths[]) throws IOException { //creates a new empty AVL tree stored in the file fname //the number of character string fields is fieldLengths.length //fieldLengths contains the length of each field } public AVLTree(String fname) throws IOException { //reuse an existing tree store in the file fname } public void insert(int k, char fields[][]) throws IOException { //PRE: the number and lengths of the fields matches the expected number and lengths //insert k and the fields into the tree //if k is in the tree do nothing } public void print() throws IOException { //Print the contents of the nodes in the tree is ascending order of the key } public LinkedList<String> find(int k) throws IOException { //if k is in the tree return a linked list of the fields associated with k //otherwise return null //The strings in ths list must NOT include the padding (i.e the null chars) return null; } public void remove(int k) throws IOException { //if k is in the tree removed the node with key k from the tree //otherwise do nothing } public void close() throws IOException { //update root and free in the file (if necessary) //close the random access file } }
• You will demonstrate your program to me either on your own machine or on a machine in the CS lab.
• I will give you a test driver for the demo but you should develop your own driver to do initial testing.
• After your demo email me your source code (AVLTree.java).
更多 java代写 案例请参考