Page 1 of 1

Struggling with this coding assignment. It has to be done with Java. I think (not sure, if you spot something please hel

Posted: Thu Jul 14, 2022 2:18 pm
by answerhappygod
Struggling with this coding assignment. It has to be donewith Java. I think (not sure, if you spot something please help)i'm done with the implementation, but now I have to create a Testfile. The objective is to code a Test file that goes into thebroster.txt file and create an AVL tree from the data then print itout in Pre-order transversal.
*Help with Test file* These are the classes I have so far. Ineed the Test file
AVLTreeMap class:
public class AVLTreeMap extends AbstractMap { private int n; private AVLTreeNode root; private AVLTreeNode NIL = new AVLTreeNode(); private AVLNodeComparator comp = newAVLNodeComparator();
public AVLTreeMap() { root = NIL; n = 0; }
public int size() { return n; }
public boolean isEmpty() { return n == 0; }
public Object get(Object key) { AVLTreeNode p = root; while (p != null) { if (comp.compare(key,p.getKey()) < 0) { p =p.getLeft(); } else if(comp.compare(key, p.getKey()) > 0) { p =p.getRight(); } else { returnp; } } return null; }
public Object put(Object key, Object value){ AVLTreeNode p = root; AVLTreeNode parent = NIL; while (p != null) { if (comp.compare(key,p.getKey()) < 0) { parent =p; p =p.getLeft(); } else if(comp.compare(key, p.getKey()) > 0) { parent =p; p =p.getRight(); } else { ObjectoldValue = p.getValue(); p.setValue(value.toString()); returnoldValue; } } AVLTreeNode newNode = newAVLTreeNode(); if (parent == NIL) { root = newNode; } else if (comp.compare(key,parent.getKey()) < 0) { parent.setLeft(newNode); } else { parent.setRight(newNode); } newNode.setH(); n++; return null; }
private void insertAVL(AVLTreeNode p, AVLTreeNodenode) { if (node.getKey().equals(p.getKey())){ p.setValue(node.getValue()); } else if (comp.compare(node.getKey(),p.getKey()) == 0) { if (p.getLeft() == NIL){ p.setLeft(node); node.setParent(p); p.setH(); n++; } else { insertAVL(p.getLeft(), node); if(p.calBF() < -1) { if (p.getLeft().calBF() < 0) { rotateLL(p); } else { rotateLR(p); } } } p.setH(); } else { if (p.getRight() == NIL){ p.setRight(node); node.setParent(p); p.setH(); n++; } else { insertAVL(p.getRight(), node); if(p.calBF() > 1) { if (p.getRight().calBF() > 0) { rotateRR(p); } else { rotateRL(p); } } p.setH(); } } }
public Object remove(Object key) { return null; }
private AVLTreeNode rotateLL(AVLTreeNode p){ AVLTreeNode temp; temp = (AVLTreeNode) p.getLeft(); p.setLeft(temp.getRight()); temp.setRight(p); return temp; }
private AVLTreeNode rotateLR(AVLTreeNode p){ AVLTreeNode temp; temp = (AVLTreeNode) p.getLeft(); p.setLeft(rotateRR(temp)); return rotateLL(p); }
private AVLTreeNode rotateRR(AVLTreeNode p){ AVLTreeNode temp; temp = (AVLTreeNode)p.getRight(); p.setRight(temp.getLeft()); temp.setLeft(p); temp = p; return temp; }
private AVLTreeNode rotateRL(AVLTreeNode p){ AVLTreeNode temp; temp = (AVLTreeNode)p.getRight(); p.setRight(rotateLL(temp)); return rotateRR(p); }
public String toString() { return genTreeString(root); }
public String genTreeString(AVLTreeNode node){ if (node == null) return ""; return node.getKey() +genTreeString(node.getLeft()) +genTreeString(node.getRight()); }
}
AVLTreeNode class:
public class AVLTreeNode extends MapEntry { private int height; private AVLTreeNode parent; private AVLTreeNode left; private AVLTreeNode right;
public AVLTreeNode() { // dummpy node super(); height = -1; parent = null; left = null; right = null; }
public AVLTreeNode(String key, String value) { // validnode 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())+ 1;} public int calBF(){return this.right.getH()-this.left.getH();}}
AVLNodeComparator class:
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(),bb.length()); for (int i=0; i<size; i++) { if (aa.charAt(i) <bb.charAt(i)) return -1; else if (aa.charAt(i) >bb.charAt(i)) return 1; } // first size characters are equal to eachother if (size == aa.length() && size ==bb.length()) return 0; else if (size == aa.length()) return -1; return 1; } }}AbstractMap class:
public abstract class AbstractMap {public abstract int size();public abstract boolean isEmpty();public abstract Object get(Object key);public abstract Object put(Object key, Object value);public abstract Object remove(Object key);}
MapEntry class:
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 String setValue(String value) { String old = this.value; this.value = value; return old; } @Override public int hashCode() { if (key == null) { return 0; } int h = 0; for (int i = 0; i < key.length();i++) { h = (h << 5) | (h>>> 27); h += (int) key.charAt(i); } return h; }}broster.txt file:
Gilbert 3 QB 27 6' 3" 230 lbs 2 SMUMayfield 6 QB 23 6' 1" 215lbs 2 OklahomaStanton 9 QB 34 6' 3" 226 lbs 12 MichiganStateChubb 24 RB 23 5' 11" 227 lbs 2 GeorgiaHilliard 25 RB 24 5' 11" 202lbs 2 TulaneMays 32 RB 24 5' 10" 230 lbs 2 UtahState