Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the Java Program below that implements an AVL tree to include the ability to delete nodes from the AVL tree. public class AVLTree {

Modify the Java Program below that implements an AVL tree to include the ability to delete nodes from the AVL tree.

public class AVLTree> { private static final boolean OUTPUT = true; public static void main(String[] args) { int[] numbers = {89, 35, 92, 70, 57, 83, 45, 61, 88, 20}; AVLTree tree = new AVLTree(); System.out.println("Adding to AVL:"); for(int i=0; i) x).compareTo(n.element) < 0) { n.left = add(x, n.left); if (height(n.left)-height(n.right) == 2) { if(((Comparable) x).compareTo(n.left.element) < 0) { if (OUTPUT) System.out.println("Right rotation"); n = rightRotation(n); } else { if (OUTPUT) System.out.println("Left-Right rotation"); n = leftRightRotation(n); } } } else { n.right = add(x, n.right); if (height(n.right)-height(n.left) == 2) { if (((Comparable) x).compareTo(n.right.element) < 0) { if (OUTPUT) System.out.println("Right-Left rotation"); n = rightLeftRotation(n); } else { if (OUTPUT) System.out.println("Left rotation"); n = leftRotation(n); } } } n.height = Math.max(height(n.left), height(n.right)) + 1; return n; } public int size() { return size; } private int height(Node n) { if (n == null) { return 0; } else { return n.height; } } private Node rightRotation(Node n) { Node t = n.left; n.left = t.right; t.right = n; n.height = Math.max(height(n.left), height(n.right)) + 1; t.height = Math.max(height(t.left), height(t.right)) + 1; return t; } private Node leftRotation(Node n) { Node t = n.right; n.right = t.left; t.left = n; n.height = Math.max(height(n.left), height(n.right)) + 1; t.height = Math.max(height(t.left), height(t.right)) + 1; return t; } private Node leftRightRotation(Node n) { n.left = leftRotation(n.left); return rightRotation(n); } private Node rightLeftRotation(Node n) { n.right = rightRotation(n.right); return leftRotation(n); } public String inorder() { String s = inorder(root); return "[" + s.substring(0, s.length()-2) + "]"; } private String inorder(Node n) { if (n != null) { return inorder(n.left) + n.element + ", " + inorder(n.right); } else { return ""; } } public String preorder() { String s = preorder(root); return "[" + s.substring(0, s.length()-2) + "]"; } private String preorder(Node n) { if (n != null) { return n.element + " "+preorder(n.left) + " " + preorder(n.right); } else { return ""; } } @Override public String toString() { Node walker = root; String s = "["; java.util.Stack stack = new java.util.Stack(); while (walker != null) { s += walker.element + ", "; if (walker.right != null) { stack.push(walker.right); } walker = walker.left; if (walker == null && !stack.isEmpty()) { walker = stack.pop(); } } return s.substring(0, s.length()-2) + "]"; } }

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

Step: 3

blur-text-image

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

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions

Question

To solve p + 3q = 5z + tan( y - 3x)

Answered: 1 week ago

Question

1. Which position would you take?

Answered: 1 week ago