This is in java and cannot use java api classes. In this question, you have to w
ID: 3792752 • Letter: T
Question
This is in java and cannot use java api classes. In this question, you have to write a program t construct an AVL tree and insert /remove nodes into/from it. Your program should have a menu driven interface for the AVL tree. You need to have options for ‘insert’, ‘remove’, ‘levelOrder’ and ‘exit’ only for the AVL tree. Other than that not many restriction just has to be able to balance. At the driver menu need to have the 5
1. insert a element into the tree.
2. remove item from tree
3.levelOrder which will balance the tree
4. print all elements as you traversed the balanced tree.
5. exit - exit driver
Explanation / Answer
class Node
{
int data;
Node left, right;
public Node(int item)
{
data = item;
left = right = null;
}
}
class BinaryTree
{
Node root;
public BinaryTree()
{
root = null;
}
void printLevelOrder()
{
int h = height(root);
int i;
for (i=1; i<=h; i++)
printGivenLevel(root, i);
}
/* Print nodes at the given level */
void printGivenLevel (Node root ,int level)
{
if (root == null)
return;
if (level == 1)
System.out.print(root.data + " ");
else if (level > 1)
{
printGivenLevel(root.left, level-1);
printGivenLevel(root.right, level-1);
}
}
/* Driver program to test above functions */
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root= new Node(1);
tree.root.left= new Node(2);
tree.root.right= new Node(3);
tree.root.left.left= new Node(4);
tree.root.left.right= new Node(5);
System.out.println("Level order traversal of binary tree is ");
tree.printLevelOrder();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.