Here is the code given in the instructions: class AVL { Node root; private class
ID: 3774746 • Letter: H
Question
Here is the code given in the instructions:
class AVL { Node root; private class Node { int data; Node left, right; int height;
private Node(int D, Node L, Node R, int H) { data=D; left=L; right=R; height=H; // user has to set height } // of constructor Node } //of Node
static private void UpdateHeight(Node T) { if (T ==null) return; else T.height = Math.max(HEIGHT(T.left),HEIGHT(T.right)) + 1; } // UPdate Height
static private int HEIGHT(Node T) { if (T== null ) return(-1); else return (T.height); } // we need to more our right child up static private Node LeftRotate(Node T) { System.out.println("left rotate with data " + T.data); Node Tr; Tr=T.right; // right child T.right=Tr.left; // our right child IS NOW hhh Tr.left=T; // move T down to the left UpdateHeight(Tr.left); // update the height of T UpdateHeight(Tr); // update hte height of the new root return Tr;
} // of LeftRotate
// we move our immediate left node up. // in doing so, we are now immediat right of our left child static private Node RightRotate(Node T) { Node Tl; System.out.println("left rotate with data " + T.data); Tl=T.left; // left child T.left=Tl.right; // our left child is now what our left was pointing to Tl.right=T; // move T down to the right UpdateHeight(Tl.right); // update the height of T UpdateHeight(Tl); // update hte height of the new root return Tl;
} // of RightRotate
public AVL() { root=null; } // of constructor AVL
// method to allow external entity to insert an element into the tree public void insert(int D) { root=insert_internal(D, root); } // of insert
private Node insert_internal(int D, Node T) { if (T==null) return( new Node (D, null, null, 0)); if (T.data == D) return T; // the data is already in there if ( D < T. data ) // go left { if (T.left == null) { T.left = insert_internal(D,null); UpdateHeight(T); } else { //interior node
T.left = insert_internal(D, T.left); } } // of go left else // D goes to the right { if (T.right == null) { T.right = insert_internal(D,null); UpdateHeight(T); } else { //interior node
T.right = insert_internal(D, T.right); } } // of go right
// now we have to figure out if things are out of // balance
int diff= HEIGHT(T.right) - HEIGHT(T.left); System.out.println("difference is " + diff + " data is " + T.data);
if ( Math.abs(diff) <= 1) // we are good to go at this level { UpdateHeight(T); return(T); }
// only if diff is bigger than 1 if ( diff > 1)// right leaning { // look at right child and figure out how it is leaning } // of right leaning else // left leaning { // look at left child to see how it is leaning Node child = T.left; int cdiff;
cdiff = HEIGHT(child.right) - HEIGHT(child.left); System.out.println("cdiff is " + cdiff);
if ( cdiff < 0 ) // left left lean { T=RightRotate(T); } else {
System.out.println("SHAUN"); preorder_internal(T); T.left = LeftRotate(T.left); System.out.println("SHAUN"); preorder_internal(T); T=RightRotate(T); } } // of left leaning
// at this point we have rotated, so we need to update // the height of our current tree
UpdateHeight(T); return(T);
} // of insert_internal
public void preorder() { System.out.println("preorder"); preorder_internal(root); } // of preorder
private void preorder_internal(Node T) { if (T==null) return; else { System.out.println(T.data + " height " + T.height); preorder_internal(T.left); preorder_internal(T.right); return; } // of else } // or preorder_internal
}// of AVL
class avltester {
public static void main(String args[]) { AVL T; T=new AVL(); T.insert(5); T.preorder(); T.insert(3); T.preorder(); T.insert(4); T.preorder();
} // of main
} // of avltester class AVL { Node root; private class Node { int data; Node left, right; int height;
private Node(int D, Node L, Node R, int H) { data=D; left=L; right=R; height=H; // user has to set height } // of constructor Node } //of Node
static private void UpdateHeight(Node T) { if (T ==null) return; else T.height = Math.max(HEIGHT(T.left),HEIGHT(T.right)) + 1; } // UPdate Height
static private int HEIGHT(Node T) { if (T== null ) return(-1); else return (T.height); } // we need to more our right child up static private Node LeftRotate(Node T) { System.out.println("left rotate with data " + T.data); Node Tr; Tr=T.right; // right child T.right=Tr.left; // our right child IS NOW hhh Tr.left=T; // move T down to the left UpdateHeight(Tr.left); // update the height of T UpdateHeight(Tr); // update hte height of the new root return Tr;
} // of LeftRotate
// we move our immediate left node up. // in doing so, we are now immediat right of our left child static private Node RightRotate(Node T) { Node Tl; System.out.println("left rotate with data " + T.data); Tl=T.left; // left child T.left=Tl.right; // our left child is now what our left was pointing to Tl.right=T; // move T down to the right UpdateHeight(Tl.right); // update the height of T UpdateHeight(Tl); // update hte height of the new root return Tl;
} // of RightRotate
public AVL() { root=null; } // of constructor AVL
// method to allow external entity to insert an element into the tree public void insert(int D) { root=insert_internal(D, root); } // of insert
private Node insert_internal(int D, Node T) { if (T==null) return( new Node (D, null, null, 0)); if (T.data == D) return T; // the data is already in there if ( D < T. data ) // go left { if (T.left == null) { T.left = insert_internal(D,null); UpdateHeight(T); } else { //interior node
T.left = insert_internal(D, T.left); } } // of go left else // D goes to the right { if (T.right == null) { T.right = insert_internal(D,null); UpdateHeight(T); } else { //interior node
T.right = insert_internal(D, T.right); } } // of go right
// now we have to figure out if things are out of // balance
int diff= HEIGHT(T.right) - HEIGHT(T.left); System.out.println("difference is " + diff + " data is " + T.data);
if ( Math.abs(diff) <= 1) // we are good to go at this level { UpdateHeight(T); return(T); }
// only if diff is bigger than 1 if ( diff > 1)// right leaning { // look at right child and figure out how it is leaning } // of right leaning else // left leaning { // look at left child to see how it is leaning Node child = T.left; int cdiff;
cdiff = HEIGHT(child.right) - HEIGHT(child.left); System.out.println("cdiff is " + cdiff);
if ( cdiff < 0 ) // left left lean { T=RightRotate(T); } else {
System.out.println("SHAUN"); preorder_internal(T); T.left = LeftRotate(T.left); System.out.println("SHAUN"); preorder_internal(T); T=RightRotate(T); } } // of left leaning
// at this point we have rotated, so we need to update // the height of our current tree
UpdateHeight(T); return(T);
} // of insert_internal
public void preorder() { System.out.println("preorder"); preorder_internal(root); } // of preorder
private void preorder_internal(Node T) { if (T==null) return; else { System.out.println(T.data + " height " + T.height); preorder_internal(T.left); preorder_internal(T.right); return; } // of else } // or preorder_internal
}// of AVL
class avltester {
public static void main(String args[]) { AVL T; T=new AVL(); T.insert(5); T.preorder(); T.insert(3); T.preorder(); T.insert(4); T.preorder();
} // of main
} // of avltester
You are to create AVL tree code which implements the following methods insert (int D) delete (int D) preorderprint() postorderprint0 inorderprint() Whenever an insertion or deletion occurs, you are to rebalance the tree. You may use my code HERE as a starting point. There is no guarantee that my code is correct. It implements a left insertion. You will need to implement deletion using web resources, etc. (MAKE SURE YOU UNDERSTAND LEFT AND RIGHT ROTATION One of these will be on the final exam).