Java Code Problem

JAVA CODE PROBLEM

I have almost completed this code I just need some help compelting it, I have written where I need help inside the java code

import java.lang.Math;

public class AVLTreeNode<E> extends BinTreeNode<E> implements AVLTreePos<E> {
private int height; // height of node
public int height() {
/**
Return height of the node
*/
return height;
}
public void setHeight(int ht) {
/**
Set height to be the argument ht
*/
height = ht;
}
public AVLTreeNode(E e, BinTreePos<E> par, BinTreePos<E> lt, BinTreePos<E> rt) {
super(e,par,lt,rt);
height = 0;
}

ORDER A PLAGIARISM-FREE PAPER HERE

 

public String toString() {
return toStringRec(1); Java Code Problem
}

private String toStringRec(int depth) {
String spaces = new String(new char[2*depth]).replace(‘\0’, ‘ ‘);
String s = spaces + “Element: ” + element().toString() + “; Height: ” + height + “\n”;
String leftstring = ( left() == null ) ? “” :
((AVLTreeNode<E>) left()).toStringRec(depth+1);
String rightstring = ( right() == null ) ? “” :
((AVLTreeNode<E>)right()).toStringRec(depth+1);
if ( left() != null )
s += spaces + “Left:\n” + leftstring;
if ( right() != null )
s += spaces + “Right:\n” + rightstring;
return s;
}
}

public interface AVLTreePos<E> extends BinTreePos<E> {
int height();
void setHeight(int h);
default void updateHeight() {
int leftHeight = (left() == null) ? -1 : ((AVLTreePos<E>)
left()).height();
int rightHeight = (right() == null) ? -1 :
((AVLTreePos<E>)right()).height();
int newHeight = Math.max(leftHeight,rightHeight)+1;
if ( height() != newHeight ) {
setHeight(newHeight);
if ( ! isRoot() )
((AVLTreePos<E>)parent()).updateHeight();
}
}
static int getBalance() {
int leftHeight = (left() == null) ? -1 : ((AVLTreePos<E>)
left()).height();
int rightHeight = (right() == null) ? -1 :
((AVLTreePos<E>)right()).height();
/**
Calculate the balance factor and return it
*/
int balanceFactor = leftHeight – rightHeight;
return balanceFactor;
}
}
import java.lang.Comparable;

public class AVLTree<E extends Comparable<E>> extends BinSearchTree<E> {
@Override
public BinTreePos<E> makeNode(E e, BinTreePos<E> par, BinTreePos<E> lt, BinTreePos<E> rt) {
return new AVLTreeNode<E>(e,par,lt,rt); Java Code Problem
}

public void rotateRight(AVLTreeNode<E> n) {
AVLTreeNode<E> nleft = (AVLTreeNode<E>)(n.left());
AVLTreeNode<E> lrChild = (AVLTreeNode<E>)nleft.right();

if ( n.isRoot() ) {
setRoot(nleft);
nleft.setParent(null);
} else {
AVLTreeNode<E> par = (AVLTreeNode<E>)n.parent();
if ( n == par.left() ) {
par.setLeft(nleft);
} else if ( n == par.right() ) {
par.setRight(nleft);
}
nleft.setParent(par);
}
nleft.setRight(n);
n.setParent(nleft);
n.setLeft(lrChild);
if ( lrChild != null ) {
lrChild.setParent(n);
lrChild.updateHeight();
}
n.updateHeight();
}

public void rotateLeft(AVLTreeNode<E> n) {
AVLTreeNode<E> nright = (AVLTreeNode<E>)(n.right());
AVLTreeNode<E> rlChild = (AVLTreeNode<E>)nright.left();

if ( n.isRoot() ) {
setRoot(nright);
nright.setParent(null);
} else {
AVLTreeNode<E> par = (AVLTreeNode<E>)(n.parent());
if ( n == par.right() ) {
par.setRight(nright);
} else if ( n == par.left() ) {
par.setLeft(nright);
}
nright.setParent(par);
}
nright.setLeft(n);
n.setParent(nright);
n.setRight(rlChild);
if ( rlChild != null ) {
rlChild.setParent(n);
rlChild.updateHeight();
}
n.updateHeight();
}

public void rebalance(BinTreePos<E> pos) {
if ( pos == null )
return;
AVLTreeNode<E> avlpos = (AVLTreeNode<E>)pos;
avlpos.updateHeight();
/**
Rebalance the AVLTree
*/
<YOUR CODE HERE>

}
}

@Override
public void add(E e) {
if ( isEmpty() ) {
setRoot(makeNode(e,null,null,null));
}
else {
AVLTreePos<E> pos = (AVLTreePos<E>)findPos(e);
int comparison = e.compareTo(pos.element());
if ( comparison == 0 )
return; // found e; nothing to add
/**
make a node with element e, and insert the new node to appropriate position
*/
<YOUR CODE HERE>
pos.updateHeight();
/**
rebalance the AVLTree
*/
<YOUR CODE HERE>
}
}

@Override
public String toString() {
return “AVLRoot: ” +
(( root() == null ) ? “null” : “\n” + root().toString());
}

public static void main(String[] args) {
String[] birds = { “heron”, “eagle”, “woodpecker”, “kookaburra”, “cardinal”,
“swallow”, “puffin”, “ostritch”, “flamingo”, “goose”,
“duck”, “budgerigar”, “magpie”, “loon”, “toucan”, “ibis”, “vulture”,
“yellowthroat”, “quail”, “gull”, “raven”, “jay”, “albatross” };
AVLTree<String> avlt = new AVLTree<String>();
for ( String bird : birds ) {
System.out.println(“Adding “+bird); Java Code Problem
avlt.add(bird);
System.out.println(avlt);
}
System.out.println();
System.out.println(“Alphabetical list of birds:”);
for ( String bird : avlt )
System.out.println(bird); Java Code Problem

× How can I help you?