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.

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
}
}
Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)
import java.io.*;
import java.util.*;
public class AVLTree {
private RandomAccessFile f;
private long root;
private long free;
private int numFields;
private int fieldLengths[];
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 fields2[][]) {
left = l;
key = d;
right = r;
fields = fields2;
height = 0;
}
private Node(long
addr) throws IOException {
f.seek(addr);
key = f.readInt();
fields = new char[numFields][];
int temp = 0;
for (int i = 0; i < numFields; i++) {
for (int j = 0; j < fieldLengths[i]; j++) {
temp++;
}
fields[i] = new char[temp];
temp = 0;
}
for (int i = 0; i < numFields; i++) {
for (int j = 0; j < fieldLengths[i]; j++) {
fields[i][j] = f.readChar();
}
}
left = f.readLong();
right = f.readLong();
height = f.readInt();
}
private void
writeNode(long addr) throws IOException {
f.seek(addr);
f.writeInt(key);
for (int i = 0; i < numFields; i++) {
for (int j = 0; j < fieldLengths[i]; j++) {
if (fields[i][j] != '\0') {
f.writeChar(fields[i][j]);
}
else {
f.writeChar('\0');
}
}
}
f.writeLong(left);
f.writeLong(right);
f.writeInt(height);
}
}
public AVLTree(String fname, int
fieldLengths2[]) 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
File file = new
File(fname);
if (file.exists())
{
file.delete();
}
f = new
RandomAccessFile(file, "rw");
fieldLengths =
fieldLengths2;
numFields =
fieldLengths.length;
root = 0;
free = 0;
f.seek(0);
f.writeLong(root);
f.writeLong(free);
f.writeInt(numFields);
for (int i :
fieldLengths) {
f.writeInt(i);
}
}
public AVLTree(String fname) throws
IOException {
//reuse an existing tree
store in the file fname
File file = new
File(fname);
f = new
RandomAccessFile(file, "rw");
f.seek(0);
root =
f.readLong();
free =
f.readLong();
f.seek(16);
numFields =
f.readInt();
fieldLengths = new
int[numFields];
for (int i = 0; i <
numFields; i++) {
fieldLengths[i] = f.readInt();
}
}
public void insert(int k, char fields[][])
throws IOException {
root = insert(root, k,
fields);
}
@SuppressWarnings("Duplicates")
private long insert(long r, int k, char
fields[][]) throws IOException {
Node x;
if (r == 0) {
x = new Node(0, k, 0, fields);
long addr = getFree();
removeFromFree(addr);
x.writeNode(addr);
return addr;
}
x = new Node(r);
// Node being inserted
is less than the node currently in view
if (k < x.key)
{
x.left = insert(x.left, k, fields);
}
// Node being inserted
is greater than the node currently in view
else if (k > x.key)
{
x.right = insert(x.right, k, fields);
}
else {
return r;
}
x.height =
getHeight(x);
x.writeNode(r);
int heightDifference =
getHeightDifference(r);
if (heightDifference
< -1) {
if (getHeightDifference(x.right) > 0) {
x.right = rightRotate(x.right);
x.writeNode(r);
return leftRotate(r);
}
else {
return leftRotate(r);
}
}
else if
(heightDifference > 1) {
if (getHeightDifference(x.left) < 0) {
x.left = leftRotate(x.left);
x.writeNode(r);
return rightRotate(r);
}
else {
return rightRotate(r);
}
}
return r;
}
public void remove(int k) throws IOException
{
root = remove(root,
k);
}
@SuppressWarnings("Duplicates")
private long remove(long r, int k) throws
IOException {
if (r == 0) {
return 0;
}
long ret = r;
Node cur = new
Node(r);
if (cur.key == k)
{
if (cur.left == 0) {
ret = cur.right;
addToFree(r);
}
else if (cur.right == 0) {
ret = cur.left;
addToFree(r);
}
else {
cur.left = replace(cur.left, cur);
cur.height = getHeight(cur);
cur.writeNode(r);
}
}
else if (cur.key > k)
{
cur.left = remove(cur.left, k);
cur.height = getHeight(cur);
cur.writeNode(r);
}
else {
cur.right = remove(cur.right, k);
cur.height = getHeight(cur);
cur.writeNode(r);
}
int heightDifference =
getHeightDifference(r);
if (heightDifference
< -1) {
if (getHeightDifference(cur.right) > 0) {
cur.right = rightRotate(cur.right);
cur.writeNode(r);
return leftRotate(r);
}
else {
return leftRotate(r);
}
}
else if
(heightDifference > 1) {
if (getHeightDifference(cur.left) < 0) {
cur.left = leftRotate(cur.left);
cur.writeNode(r);
return rightRotate(r);
}
else {
return rightRotate(r);
}
}
return ret;
}
@SuppressWarnings("Duplicates")
private long replace(long r, Node change) throws
IOException {
Node cur = new
Node(r);
if (cur.right != 0)
{
cur.right = replace(cur.right, change);
cur.height = getHeight(cur);
cur.writeNode(r);
int heightDifference = getHeightDifference(r);
if (heightDifference < -1) {
if (getHeightDifference(cur.right) > 0) {
cur.right = rightRotate(cur.right);
cur.writeNode(r);
return leftRotate(r);
}
else {
return leftRotate(r);
}
}
else if (heightDifference > 1) {
if (getHeightDifference(cur.left) < 0) {
cur.left = leftRotate(cur.left);
cur.writeNode(r);
return rightRotate(r);
}
else {
return rightRotate(r);
}
}
return r;
}
else {
change.key = cur.key;
change.fields = cur.fields;
addToFree(r);
return cur.left;
}
}
private long leftRotate(long n) throws
IOException {
Node cur = new
Node(n);
long curRight =
cur.right;
Node r = new
Node(cur.right);
cur.right =
r.left;
r.left = n;
cur.height =
getHeight(cur);
cur.writeNode(n);
r.height =
getHeight(r);
r.writeNode(curRight);
return curRight;
}
private long rightRotate(long n) throws
IOException {
Node cur = new
Node(n);
long curLeft =
cur.left;
Node r = new
Node(cur.left);
cur.left =
r.right;
r.right = n;
cur.height =
getHeight(cur);
cur.writeNode(n);
r.height =
getHeight(r);
r.writeNode(curLeft);
return curLeft;
}
private int getHeightDifference(long addr)
throws IOException {
Node cur = new
Node(addr);
Node left = new
Node(cur.left);
Node right = new
Node(cur.right);
if (cur.left == 0)
{
left.height = -1;
}
if (cur.right == 0)
{
right.height = -1;
}
return left.height -
right.height;
}
private int getHeight(Node cur) throws
IOException {
Node left = new
Node(cur.left);
Node right = new
Node(cur.right);
if (cur.left == 0
&& cur.right == 0) {
return 0;
}
if (cur.left == 0)
{
return 1 + right.height;
}
if (cur.right == 0)
{
return 1 + left.height;
}
return 1 +
Math.max(left.height, right.height);
}
private long getFree() throws
IOException{
long addr;
f.seek(8);
if (f.readLong() == 0)
{
addr = f.length();
}
else {
f.seek(8);
addr = f.readLong();
}
return addr;
}
public LinkedList<Integer>
getFreeList() throws IOException {
LinkedList<Integer> out = new LinkedList<>();
long addr =
getFree();
if (addr != f.length())
{
while (addr <= f.length()) {
out.add((int) addr);
f.seek(addr);
addr = f.readLong();
if (addr == 0) {
break;
}
}
}
return out;
}
private void addToFree(long r) throws
IOException {
long free1 =
getFree();
if (free1 == f.length())
{
f.seek(8);
f.writeLong(r);
f.seek(r);
f.writeLong(0);
free = r;
}
else {
f.seek(8);
long temp = f.readLong();
f.seek(8);
f.writeLong(r);
f.seek(r);
f.writeLong(temp);
free = r;
}
}
private void removeFromFree(long r) throws
IOException {
if (r == f.length())
{
f.seek(8);
f.writeLong(0);
free = 0;
}
else {
f.seek(r);
long temp = f.readLong();
f.seek(8);
if (temp == 0) {
f.writeLong(0);
free = 0;
}
else {
f.writeLong(temp);
free = temp;
}
}
}
private String printFields(long addr) throws
IOException{
Node x = new
Node(addr);
StringBuilder sb = new
StringBuilder();
for (int i = 0; i <
numFields; i++) {
for (int j = 0; j < fieldLengths[i]; j++) {
sb.append(x.fields[i][j]);
}
sb.append(" ");
}
return
sb.toString();
}
public LinkedList<String> find(int k)
throws IOException {
LinkedList<String>
out = new LinkedList<>();
f.seek(20);
for (int i = 0; i <
numFields; i++) {
f.readInt();
}
while
(f.getFilePointer() < f.length()) {
Node cur = new Node(f.getFilePointer());
if (cur.key == k) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numFields; i++) {
for (int j = 0; j < fieldLengths[i]; j++) {
if (!(cur.fields[i][j] == '\0')) {
sb.append(cur.fields[i][j]);
}
}
out.add(sb.toString());
sb.delete(0, sb.length());
}
return out;
}
}
return null;
}
public void print() throws IOException
{
f.seek(0);
System.out.println(" ["
+ f.getFilePointer() + " Root: " + root + "] ");
f.seek(8);
System.out.println(" ["
+ f.getFilePointer() + " Free: " + free + "] ");
f.seek(16);
System.out.println(" ["
+ f.getFilePointer() + " numOtherFields " + numFields + "]
");
f.seek(root);
print(root);
System.out.println();
}
private void print(long r) throws IOException
{
if (r == 0) {
return;
}
Node x = new
Node(r);
print(x.left);
f.seek(r);
System.out.println(" ["
+ f.getFilePointer() + " " + x.key + " " + printFields(r) + " " +
x.left + " " + x.right + " " + x.height + "] ");
print(x.right);
}
public void close() throws IOException
{
f.seek(0);
f.writeLong(root);
f.seek(8);
f.writeLong(free);
f.close();
}
}
Implement an AVL tree stored in a random access file Each node contains an integer key,...
You should now be able to edit the IntTree class. Implement each of the functions labeled with You are not allowed to use any kind of loop in your solutions. You may not modify the Node class in any way You may not modify the function headers of any of the functions already present in the file. You may not add any fields to the IntTree class. You may not change or remove the line that reads “package hw2;”...
In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...
1. Write a function in Tree class which returns true if and only if the tree satisfies the binary search tree property. The function’s header line is public boolean isValidBST() And in the attached code, you just need to finish the function after the comment: “//Instructor hint: please write your code here:” Make sure you execute your code, and the result in the main function after calling your function should be same as the prompt message I write. Clearly you...
package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...
Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...
C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...
JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth * * Returns the number of nodes with depth == d * Precondition: none * * param: d the depth to search for * * hint: use a recursive helper function * * ToDo 4 */ public int numNodesAtDepth(int d) { return -1; } **********USEFUL CODE FROM THE ASSIGNMENT:*********** public class simpleBST<Key extends Comparable<Key>, Value> { private Node root; ...
Write a method that determines the key of the successor of the root node in a binary search tree. For any input binary search tree, find the key of successor of the root node.Note: Successor is the node with the next highest key, should work for any binary search tree - not just the given example input. #include <iostream> using namespace std; class Node { private: int key; string val; Node* left; Node* right; friend class BinarySearchTree; }; class BinarySearchTree {...
Here you'll write the inorder traversal function in the header file "bst.h". Notice that the public inorder function calls a private recursive function _ inorder to do the actual traversal. This public-private strategy is the correct way to implement recursive functions, where the public function kicks off the recursion and the private function does the actual work. The public function is written for you, your job is to implement the private _ inorder function. he main program has been written...
C++, implement the bst.cpp file without adding any additional methods or functions to it ======================================================================== // bst.cpp #include <iostream> #include "bst.h" using namespace std; BinarySearchTree::BinarySearchTree() { root = nullptr; } void BinarySearchTree::insert(int key, string val) { Node* new_node = new Node; new_node->key = key; new_node->val = val; new_node->left = nullptr; new_node->right = nullptr; if (root == nullptr) { root = new_node; } else { insertHelper(root, new_node); } } void BinarySearchTree::insertHelper(Node* parent, Node* new_node) { if (new_node->key < parent->key) { if...