2. public rotRight(AvlNode someNode) The right child of someNode moves into the position of someNode in the tree. Updates the height of someNode and returns updated AvINode.
Can i get help with coding this in java. Only this question , you can even create your own AvlTree.java
AvlTree.java is below
public class AvlTree {
private AvlNode root;
public AvlTree() {
root = null;
}
public void insert(Book book) {
root = insert(book, root);
}
//Provided insert
private AvlNode insert(Book book, AvlNode t) {
if (t == null) {
t = new AvlNode(book);
} else if (book.getId() < t.getBook().getId()) {
t.setLeft(insert(book, t.getLeft()));
if (height(t.getLeft()) - height(t.getRight()) == 2) {
if (book.getId() < t.getLeft().getBook().getId()) {
t = rotLeft(t);
} else {
t = doubleRotLeft(t);
}
}
} else if (book.getId() > t.getBook().getId()) {
t.setRight(insert(book, t.getRight()));
if (height(t.getRight()) - height(t.getLeft()) == 2) {
if (book.getId() > t.getBook().getId()) {
t = rotRight(t);
} else {
t = doubleRotRight(t);
}
}
} else {
//if both book ID's overlap do nothing and skip
}
t.setHeight(max(height(t.getLeft()), height(t.getRight())) + 1);
return t;
}
public void printTree() {
if (root == null) {
System.out.println("Empty tree");
} else {
printTree(root);
}
}
private void printTree(AvlNode t) {
if (t != null) {
printTree(t.getLeft());
System.out.println(t.getBook().getId());
printTree(t.getRight());
}
}
private int height(AvlNode t) {
return t == null ? -1 : t.getHeight();
}
private int max(int lhs, int rhs) {
return lhs > rhs ? lhs : rhs;
}
//For Question 1.
private AvlNode rotLeft(AvlNode someNode) {
return someNode;
}
//For Question 2
private AvlNode rotRight(AvlNode someNode) {
return someNode;
}
here's AvlNode.java
class AvlNode {
private Book book;
private int height;
private AvlNode left;
private AvlNode right;
public AvlNode(Book book) {
this.book = book;
this.height = 1;
this.left = null;
this.right = null;
}
public Book getBook() {
return book;
}
public int getHeight() {
return height;
}
public AvlNode getLeft() {
return left;
}
public AvlNode getRight() {
return right;
}
public void setBook(Book book) {
this.book = book;
}
public void setHeight(int height) {
this.height = height;
}
public void setLeft(AvlNode left) {
this.left = left;
}
public void setRight(AvlNode right) {
this.right = right;
}
public AvlNode(Book book, int height, AvlNode left, AvlNode right) {
this.book = book;
this.height = height;
this.left = left;
this.right = right;
}
}
Book.java
public class Book {
private String title;
private int id;
private String author;
private double price;
public Book() {
this.title = "";
this.id = 0;
this.author = "";
this.price = 0.0;
}
public Book(int id) {
this.title = "";
this.id = id;
this.author = "";
this.price = 0.0;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Book(String title, int id, String author, double price) {
this.title = title;
this.id = id;
this.author = author;
this.price = price;
}
@Override
public String toString() {
return "Book object\n{" + "title= " + title + ", id= " + id + ", author= " + author + ", price= " + price + '}';
}
}
![2. public rotRightAvINode someNode)
The right child of someNode moves into the position of someNode in the tree.
Updates the height of someNode and returns updated AvINode.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F13f42eda-4233-4463-985e-07142dfa1519%2F3ae88b90-3beb-4dae-8b50-2718df04a4bb%2Fx72s6up_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)