[Solved]Task Implementation Avltreemap Class Partial Implementation Class Given Public Class Avltr Q37106169
Task: Implementation of the AVLTreeMap class.
The partial implementation of the class is given below:
public class AVLTreeMap extends AbstractMap { private int n; private AVLTreeNode root; private AVLTreeNode NIL = new AVLTreeNode(); private AVLNodeComparator comp = new AVLNodeComparator(); public AVLTreeMap() { /* your implementation */ } public int size() {return n;} public boolean isEmpty() {return n == 0;} public Object get(Object key) { /* your implementation */ } public Object put(Object key, Object value) { /* your implementation */ } private void insertAVL(AVLTreeNode p, AVLTreeNode node) { /* your implementation */ } private AVLTreeNode rotateLL(AVLTreeNode p) { /* your implementation */ } private AVLTreeNode rotateLR(AVLTreeNode p) { /* your implementation */ } private AVLTreeNode rotateRR(AVLTreeNode p) { /* your implementation */ } private AVLTreeNode rotateRL(AVLTreeNode p) { /* your implementation */ } public Object remove(Object key) { return null; // remove is not required for this assignment } public String toString() { // return the key string sequence of this AVL tree nodes in the order of pre-order traversal }}
Requirements:
- Please make sure your program compiles, otherwise yoursubmission will not be graded and you will receive zero.
- Point deduction rule:
- Compile warning: 3 points each.
- Minor error: such as not meeting the assignment input/outputrequirement, 5 points each.
- Major error: examples include, infinite loop, runtime errors,any runtime exception, 15 points each.
- Any code not compliant to the assignment requirement (e.g., notusing AbstractMap class) will not be graded and you will receivezero.
- Complete the AVLTreeMap class as we discussed in thelectures.
- You have to complete all the necessary methods declared in thetemplate.
- You may add your own private method if necessary.
- The remove method is not rquired.
- Your implementation has to follow the specification given. Anyother implementation will not receive any credit.
- Test: write a test program that inserts all Cleveland Indians’players in roster.txt . Then print the AVL tree nodes inthe order of pre-order traversal. A sample output is shown below:Totally 30 playersHand–Cimber–Bieber–Bauer–Allen–Bauers–Carrasco–Edwards–Clevinger–Goody–Haase–Perez–Lindor–Kluber–Kipnis–Moroff–Martin–Luplow–Mercado–Olson–Naquin–Otero–Ramirez–Plutko–Plawecki–Santana–Salazar–Wittgren–Stamets–Zimmer–
Classes that support the assignment are asfollow:
MapEntry.java
public class MapEntry {
private String key;
private String value;
public MapEntry() {
this(null, null);
}
public MapEntry (String k, String v) {
this.key = k;
this.value = v;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
public void setKey(String key) {
this.key = key;
}
public Object setValue(String value) {
String old = this.value;
this.value = value;
return old;
}
public int HashCode() {
if (key == null)
return 0;
int h = 0;
for (int i = 0; i < key.length(); i++) {
h = (h << 5) | (h >>>= 7);
h+= (int)key.charAt(i);
}
return h;
}
}
AbstractMap.java
public abstract class AbstractMap {
public abstract Object get(Object key);
public abstract Object put(Object key, Object value);
public abstract Object remove(Object key);
public abstract boolean isEmpty();
public abstract int size();
public abstract Iterable keySet();
}
AVLTreeNode.java
public class AVLTreeNode extends MapEntry{
private int height;
private AVLTreeNode parent;
private AVLTreeNode left;
private AVLTreeNode right;
public AVLTreeNode(){
super();//NIL
height=-1;
parent=null;left=null;right=null;}
public AVLTreeNode(String key, String value){
super(key,value);
height=0; parent=null;
left=null;right=null;
}
private int max(int l,int r){
if(l<r) return r;
return l;
}
public AVLTreeNode getleft(){return left;}
public AVLTreeNode getRight(){return right;}
public AVLTreeNode getParent(){return parent;}
public int getH(){return height;}
public void setLeft(AVLTreeNode l){left=l;}
public void setRight(AVLTreeNode r){right=r;}
public void setParent(AVLTreeNode p){parent=p;}
public void setH(){height=max(left.getH(),right.getH());}
public int calBF(){
return this.right.getH()-this.left.getH();
}
AVLNodeComparator
import java.util.Comparator;
public class AVLNodeComparator implements Comparator {
public int compare(Object a,Object b){
String aa=((AVLTreeNode)a).getKey();
String bb=((AVLTreeNode)b).getKey();
if(a==null&&b==null)return 0;
else if(a==null)return -1;
else if(b==null)return 1;
else{
int size=Math.min(aa.length());
for(int i=0;i<size;i++)
if(aa.charAt(i)<bb.charAt(i))
return -1;
else return 1;
if(size==aa.length()&&size==bb.length())
return 0;
else if(size==aa.length()) return -1;
return 1;
}}}
Expert Answer
Answer to Task: Implementation of the AVLTreeMap class. The partial implementation of the class is given below: public class AVLTr… . . .
OR