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

Your work should be readable as well as correct. Always Verify Time! Write code

ID: 3884771 • Letter: Y

Question

Your work should be readable as well as correct.

Always Verify Time!

Write code for O(n) worst-case algorithm that verifies that a tree is actually an AVL tree by detecting imbalance. If imbalance is true, then call the proper rotation function provided in the lecture slides to fix the imbalance condition.

1. Must read AVLNode data from a text file

2. Create a Book Object; and an AVL node object to be inserted into the AVL tree

3. At each insert, detect imbalance and fix the AVL tree

4. Report each imbalance detection and the node where it occurred; and output the message:

Output:

Imbalance occurred at inserting ISBN 12345; fixed in LeftRight Rotation

Imbalance occurred at inserting ISBN 87654; fixed in Left Rotation

Imbalance occurred at inserting ISBN 974321; fixed in RightLeft Rotation

class AVLNode

int key; (ISBN number)

Book value; //create a class representing a book with minimum attributes

int height;

AVLNode leftPtr;

AVLNode rightPtr;

}

You must verify the AVL balance condition at each insert and detect and fix imbalance, output result of each insertion. A null node is considered AVL property of height -1.

Java or C++ are the options.

Bonus (20%): Create a random binary tree and verify BST property and AVL balance condition. Do not hard code the tree inside the code.

Explanation / Answer

// Java program for insertion in AVL Tree

class Book {

  

int id;

}

class AVLNode {

Book value;

int key, height;

AVLNode leftPtrPtr, rightPtrPtr;

// A utility function to get height of the tree

int height(AVLNode N) {

if (N == null)

return 0;

return N.height;

}

// A utility function to get maximum of two integers

int max(int a, int b) {

return (a > b) ? a : b;

}

// A utility function to rightPtr rotate subtree rooted with y

// See the diagram given above.

AVLNode rightPtrRotate(AVLNode y) {

AVLNode x = y.leftPtrPtr;

AVLNode T2 = x.rightPtrPtr;

// Perform rotation

x.rightPtrPtr = y;

y.leftPtrPtr = T2;

// Update heights

y.height = max(height(y.leftPtrPtr), height(y.rightPtrPtr)) + 1;

x.height = max(height(x.leftPtrPtr), height(x.rightPtrPtr)) + 1;

// Return new root

return x;

}

// A utility function to leftPtr rotate subtree rooted with x

// See the diagram given above.

AVLNode leftPtrRotate(AVLNode x) {

AVLNode y = x.rightPtrPtr;

AVLNode T2 = y.leftPtrPtr;

// Perform rotation

y.leftPtrPtr = x;

x.rightPtrPtr = T2;

// Update heights

x.height = max(height(x.leftPtr), height(x.rightPtr)) + 1;

y.height = max(height(y.leftPtr), height(y.rightPtr)) + 1;

// Return new root

return y;

}

// Get Balance factor of node N

int getBalance(AVLNode N) {

if (N == null)

return 0;

return height(N.leftPtr) - height(N.rightPtr);

}

AVLNode insert(AVLNode node, int key) {

/* 1. Perform the normal BST insertion */

if (node == null)

return (new AVLNode(key));

if (key < node.key)

node.leftPtr = insert(node.leftPtr, key);

else if (key > node.key)

node.rightPtr = insert(node.rightPtr, key);

else // Duplicate keys not allowed

return node;

/* 2. Update height of this ancestor node */

node.height = 1 + max(height(node.leftPtr),

height(node.rightPtr));

/* 3. Get the balance factor of this ancestor

node to check whether this node became

unbalanced */

int balance = getBalance(node);

// If this node becomes unbalanced, then there

// are 4 cases leftPtr leftPtr Case

if (balance > 1 && key < node.leftPtr.key)

return rightPtrRotate(node);

// rightPtr rightPtr Case

if (balance < -1 && key > node.rightPtr.key)

return leftPtrRotate(node);

// leftPtr rightPtr Case

if (balance > 1 && key > node.leftPtr.key) {

node.leftPtr = leftPtrRotate(node.leftPtr);

return rightPtrRotate(node);

}

// rightPtr leftPtr Case

if (balance < -1 && key < node.rightPtr.key) {

node.rightPtr = rightPtrRotate(node.rightPtr);

return leftPtrRotate(node);

}

/* return the (unchanged) node pointer */

return node;

}

// A utility function to print preorder traversal

// of the tree.

// The function also prints height of every node

void preOrder(AVLNode node) {

if (node != null) {

System.out.print(node.key + " ");

preOrder(node.leftPtr);

preOrder(node.rightPtr);

}

}

public static void main(String[] args) {

AVLNode tree = new AVLNode();

/* Constructing tree given in the above figure */

tree.root = tree.insert(tree.root, 12345);

tree.root = tree.insert(tree.root, 87654);

tree.root = tree.insert(tree.root, 974321);

  

System.out.println("Imbalance occurred at inserting ISBN" +tree.preOrder(tree.root)+ " fixed in LeftRight Rotation");

System.out.println("Imbalance occurred at inserting ISBN" +tree.preOrder(tree.root)+ " fixed in Left Rotation");

System.out.println("Imbalance occurred at inserting ISBN" +tree.preOrder(tree.root)+ " fixed in RightLeft Rotation");

}

}