Question
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
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 followingslides.
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.
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 char fields[][];
private long left;
private long right;
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 find(int k) throws IOException {
// otherwise return null
// if k is in the tree return a linked list of the fields associated with k
// 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
}
}
Addr Contents Root Free numOtherFields 508 908 16 20 24 28 108 188 268 348 428 508 588 668 748 828 908 988 1068 Length 1 Length 2 20 668 40Alonzo 200 Alan 1068 90 28 80 60 0 100 George 10 268 50 428 Church 828 Turing 0 0 Ada Byron Hannah Arendt 108 748 3 Kurt Godel 988 0 Eliot 348 188 Anton Chekhov 0 0 Vladimir Nabokov. 0 0 0 Key Field 1 Field 2 Left Right Height Addr Contents Root Free numOtherFields 508 908 16 20 24 28 108 188 268 348 428 508 588 668 748 828 908 988 1068 Length 1 Length 2 20 668 40Alonzo 200 Alan 1068 90 28 80 60 0 100 George 10 268 50 428 Church 828 Turing 0 0 Ada Byron Hannah Arendt 108 748 3 Kurt Godel 988 0 Eliot 348 188 Anton Chekhov 0 0 Vladimir Nabokov. 0 0 0 Key Field 1 Field 2 Left Right HeightStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started