Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How would you implement the following methods using an AVL Tree Structure? INSER

ID: 3701304 • Letter: H

Question

How would you implement the following methods using an AVL Tree Structure?

INSERT
FIND
REMOVEONE
REMOVEALL



//Implement the following methods

public void insert(int d) throws IOException {

//insert d into the tree

//if d is in the tree increment the count field associated with d

}

public int find(int d) throws IOException {

//if d is in the tree return the value of count associated with d

//otherwise return 0

}

public void removeOne(int d) throws IOException {

//remove one copy of d from the tree

//if the copy is the last copy remove d from the tree

//if d is not in the tree the method has no effect

}

public void removeAll(int d) throws IOException {

//remove d from the tree

//if d is not in the tree the method has no effect

}

Explanation / Answer

import java.util.Scanner;

class AVLNode

{   

AVLNode left, right;

int data;

int height;

/* Constructor */

public AVLNode()

{

left = null;

right = null;

data = 0;

height = 0;

}

/* Constructor */

public AVLNode(int n)

{

left = null;

right = null;

data = n;

height = 0;

}

}

class AVLTree{

private AVLNode root;

/* Constructor */

public AVLTree()

{

root = null;

}

public boolean isEmpty()

{

return root == null;

}

private int height(AVLNode t )

{

return t == null ? -1 : t.height;

}

private int max(int lhs, int rhs)

{

return lhs > rhs ? lhs : rhs;

}

public void insert(int data)

{

root = insert(data, root);

}

public boolean find(int val)

{

return find(root, val);

}

/* Function to insert data recursively */

private AVLNode insert(int x, AVLNode t)

{

if (t == null)

t = new AVLNode(x);

else if (x < t.data)

{

t.left = insert( x, t.left );

if( height( t.left ) - height( t.right ) == 2 )

if( x < t.left.data )

t = rotateWithLeftChild( t );

else

t = doubleWithLeftChild( t );

}

else if( x > t.data )

{

t.right = insert( x, t.right );

if( height( t.right ) - height( t.left ) == 2 )

if( x > t.right.data)

t = rotateWithRightChild( t );

else

t = doubleWithRightChild( t );

}

else

; // Duplicate; do nothing

t.height = max( height( t.left ), height( t.right ) ) + 1;

return t;

}

/* Rotate binary tree node with left child */

private AVLNode rotateWithLeftChild(AVLNode k2)

{

AVLNode k1 = k2.left;

k2.left = k1.right;

k1.right = k2;

k2.height = max( height( k2.left ), height( k2.right ) ) + 1;

k1.height = max( height( k1.left ), k2.height ) + 1;

return k1;

}

/* Rotate binary tree node with right child */

private AVLNode rotateWithRightChild(AVLNode k1)

{

AVLNode k2 = k1.right;

k1.right = k2.left;

k2.left = k1;

k1.height = max( height( k1.left ), height( k1.right ) ) + 1;

k2.height = max( height( k2.right ), k1.height ) + 1;

return k2;

}

/**

* Double rotate binary tree node: first left child

* with its right child; then node k3 with new left child */

private AVLNode doubleWithLeftChild(AVLNode k3)

{

k3.left = rotateWithRightChild( k3.left );

return rotateWithLeftChild( k3 );

}

/**

* Double rotate binary tree node: first right child

* with its left child; then node k1 with new right child */   

private AVLNode doubleWithRightChild(AVLNode k1)

{

k1.right = rotateWithLeftChild( k1.right );

return rotateWithRightChild( k1 );

}   

private boolean find(AVLNode r, int val)

{

boolean found = false;

while ((r != null) && !found)

{

int rval = r.data;

if (val < rval)

r = r.left;

else if (val > rval)

r = r.right;

else

{

found = true;

break;

}

found = find(r, val);

}

return found;

}

public void removeAll()

{

root = null;

}

}

/* Class AVL Tree Test */

class AVLTreeTest

{

public static void main(String[] args)

{   

Scanner scan = new Scanner(System.in);

/* Creating object of AVLTree */

AVLTree avlt = new AVLTree();

System.out.println("AVLTree Tree Test ");   

char ch;

/* Perform tree operations */

do   

{

System.out.println(" AVLTree Operations ");

System.out.println("1. insert ");

System.out.println("2. find");

System.out.println("3. remove node");

System.out.println("4. remove All");

  

int choice = scan.nextInt();   

switch (choice)

{

case 1 :

System.out.println("Enter integer element to insert");

avlt.insert( scan.nextInt() );

break;   

case 2 :

System.out.println("Enter integer element to search");

System.out.println("Search result : "+ avlt.find( scan.nextInt() ));

break;   

  

case 4 :

avlt.removeAll();

break;

default :

System.out.println("Wrong Entry ");

break;

}

  

System.out.println(" Do you want to continue (Type y or n) ");

ch = scan.next().charAt(0);   

} while (ch == 'Y'|| ch == 'y');

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote