Question: How would I implement the AVLTree constructor with the following information?: import java.io.*; import java.util.*; public class AVLTree { /* Implements a ALV tree of
How would I implement the AVLTree constructor with the following information?:
import java.io.*;
import java.util.*;
public class AVLTree {
/*
Implements a ALV tree of ints stored in a random access file.
Duplicates are recorded by a count field associated with the int
*/
public static final int CREATE = 0;
public static final int REUSE = 1;
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 class Node {
private long left;
private long right;
private int data;
private int count;
private int height;
private Node(long L, int d, long r) {
//constructor for a new node
this.data = d;
this.count = 1;
this.height = -1;
this.left = L;
this.right = r;
}
private Node(long addr) throws IOException {
// constructor for a node that exists and is stored in the file
f.seek(addr);
this.data = f.readInt();
this.count = f.readInt();
this.height = f.readInt();
this.left = f.readLong();
this.right = f.readLong();
}
private void writeNode(long addr) throws IOException {
// writes the node to the file at location addr
f.seek(addr);
f.writeInt(data);
f.writeInt(count);
f.writeInt(height);
f.writeLong(left);
f.writeLong(right);
}
} //Implement AVLTree constructor
public AVLTree(String fname, int mode) throws IOException {
//if mode is CREATE a new empty file is created
//if mode is CREATE and a file with file name fname exists the file with
// fname must be deleted before the new empty file is created
//if mode is REUSE an existing file is used if it exists otherwise a new empty
// file is created
}
} --------------------------------------------------------------------------------------------- Here is an example of a Binary Search Tree constructor: public BinarySearchTree(String fname, int mode) throws IOException { File path = new File(fname); if (mode == CREATE && path.exists()) path.delete(); f = new RandomAccessFile(path, "rw"); if (mode == CREATE) { root = 0; free = 0; f.writeLong(root); f.writeLong(free); } else { f.seek(0); root = f.readLong(); free = f.readLong(); } } Would the AVL Tree implementation be any different???????
Thanks for the help!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
