Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

import java.util.ArrayList; import java.util.Collection; import java.util.List; public class AVL implements Tree { private int height; private int size; private BinaryNode root; private int RRotations; //

import java.util.ArrayList; import java.util.Collection; import java.util.List; public class AVL> implements Tree{ private int height; private int size; private BinaryNode root; private int RRotations; // this will be used to see if the amount of rotations was correct private int LRotations; // this will be used to see if the amount of rotations was correct public AVL(){ this.root = null; this.height = 0; this.size = 0; this.RRotations = 0; this.LRotations = 0; } public AVL(BinaryNode root){ this.root = root; this.height = root.height(); this.size = root.size(); this.RRotations = 0; this.LRotations = 0; } // Access fields public int getRRotations(){ return this.RRotations; } public int getLRotations(){ return this.LRotations; } public BinaryNode root() { return this.root; } public int height() { return this.height; } public int size() { return this.size; } public boolean isBalanced() { return root.isBalanced(); } public void updateHeight() { this.height = root.height(); } // Traversals that return lists public List preOrderList() { List list = new ArrayList<>(); preOrderTraversal(root, list); return list; } private void preOrderTraversal(BinaryNode node, List list) { if (node == null) { return; } list.add(node.data()); preOrderTraversal(node.left(), list); preOrderTraversal(node.right(), list); } public List inOrderList() { List list = new ArrayList<>(); inOrderTraversal(root, list); return list; } private void inOrderTraversal(BinaryNode node, List list) { if (node == null) { return; } inOrderTraversal(node.left(), list); list.add(node.data()); inOrderTraversal(node.right(), list); } public List postOrderList() { List list = new ArrayList<>(); postOrderTraversal(root, list); return list; } private void postOrderTraversal(BinaryNode node, List list) { if (node == null) { return; } postOrderTraversal(node.left(), list); postOrderTraversal(node.right(), list); list.add(node.data()); } /* TODO: rotateRight * x y * / \ / \ * y C ===> A x * / \ / \ * A B B C * You should never rotateRight if the left subtree is empty. * Make sure you increment the RRotations. */ public void rotateRight(BinaryNode node){ } /* TODO: rotateLeft * x y * / \ / \ * y C <== A x * / \ / \ * A B B C * You should never rotateLeft if the right subtree is empty. * Make sure you increment the LRotations. * Symmetrical to above. */ public void rotateLeft(BinaryNode node){ } /* TODO: possibleRotateRight * If the current node is unbalanced with the right tree height being smaller * than the left subtree height, rotate right. Otherwise, don't do anything. */ public void possibleRotateRight(BinaryNode node){ } /* TODO: possibleRotateLeft * If the current node is unbalanced with the left tree height being smaller * than the right subtree height, rotate left. Otherwise, don't do anything. */ public void possibleRotateLeft(BinaryNode node){ } /* TODO: mkBalanced * Given a node, balance it if the heights are unbalanced. * Hint: rotations!!! */ public void mkBalanced(BinaryNode node){ }  // Helpers for BST/AVL methods // TODO: extractRightMost - identical to BST public BinaryNode extractRightMost(BinaryNode curNode) { return null; } // AVL & BST Search & insert same // TODO: search - identical to BST public BinaryNode search(E elem) { return null; } /* TODO: insert - slightly different from BST but similar logic * Hint: mkBalanced will be your best friend here. */ public void insert(E elem) { } /* TODO: delete - slightly different from BST but similar logic * Hint: mkBalanced will be your best friend here. */ public BinaryNode delete(E elem) { return null; } // Stuff to help you debug if you want // Can ignore or use to see if it works. static > Tree mkAVL (Collection elems) { Tree result = new AVL<>(); for (E e : elems) result.insert(e); return result; } public TreePrinter.PrintableNode getLeft() { return this.root.hasLeft() ? this.root.left() : null; } public TreePrinter.PrintableNode getRight() { return this.root.hasRight() ? this.root.right() : null; } public String getText() { return (this.root != null) ? this.root.getText() : ""; } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students explore these related Databases questions