Question

In: Computer Science

How do you implement an AVL tree for strings in Java?

How do you implement an AVL tree for strings in Java?

Solutions

Expert Solution

//Java program for AVL Tree

class TreeNode {
String key;
int height;
TreeNode left, right;
  
TreeNode(String d) {
key = d;
height = 1;
}
}
  
class AVLTree {
  
TreeNode root;
  
int nodeHeight(TreeNode N) {
if (N == null)
return 0;
  
return N.height;
}
  
int maximum(int a, int b) {
return (a > b) ? a : b;
}
  
TreeNode rotateLeft(TreeNode x) {
   TreeNode y = x.right;
   TreeNode T2 = y.left;
y.left = x;
x.right = T2;
x.height = maximum(nodeHeight(x.left), nodeHeight(x.right)) + 1;
y.height = maximum(nodeHeight(y.left), nodeHeight(y.right)) + 1;
  
return y;
}
  
int getBalanceFactor(TreeNode N) {
if (N == null)
return 0;
  
return nodeHeight(N.left) - nodeHeight(N.right);
}
  
TreeNode rotateRight(TreeNode y) {
   TreeNode x = y.left;
   TreeNode T2 = x.right;
  
x.right = y;
y.left = T2;
  
y.height = maximum(nodeHeight(y.left), nodeHeight(y.right)) + 1;
x.height = maximum(nodeHeight(x.left), nodeHeight(x.right)) + 1;
  
return x;
}
  
  
  
TreeNode insertNode(TreeNode node, String key) {
  
if (node == null) return (new TreeNode(key));
  
if (key.compareTo(node.key) < 0)
node.left = insertNode(node.left, key);
else if (key.compareTo(node.key) > 0)
node.right = insertNode(node.right, key);
else
return node;
node.height = 1 + maximum(nodeHeight(node.left),nodeHeight(node.right));
  
int balance = getBalanceFactor(node);
if (balance > 1 && key.compareTo(node.left.key) < 0)
return rotateRight(node);
  
if (balance < -1 && key.compareTo(node.right.key) > 0)
return rotateLeft(node);
  
if (balance > 1 && key.compareTo(node.left.key) > 0) {
node.left = rotateLeft(node.left);
return rotateRight(node);
}
  
if (balance < -1 && key.compareTo(node.right.key) < 0) {
node.right = rotateRight(node.right);
return rotateLeft(node);
}
  
return node;
}
void inOrder(TreeNode node) {
if (node != null) {
   inOrder(node.left);
System.out.print(node.key + " ");
inOrder(node.right);
}
}
  
public static void main(String[] args) {
AVLTree AVLtree = new AVLTree();
  
AVLtree.root = AVLtree.insertNode(AVLtree.root, "Banana");
AVLtree.root = AVLtree.insertNode(AVLtree.root, "Orange");
AVLtree.root = AVLtree.insertNode(AVLtree.root, "Apple");
AVLtree.root = AVLtree.insertNode(AVLtree.root, "mango");
AVLtree.root = AVLtree.insertNode(AVLtree.root, "Grapes");
AVLtree.root = AVLtree.insertNode(AVLtree.root, "Guava");
  
System.out.println("INorder traversal : ");
AVLtree.inOrder(AVLtree.root);
}
}


Related Solutions

AVL trees in Java For a given adjacency matrix of a rooted tree you need to...
AVL trees in Java For a given adjacency matrix of a rooted tree you need to decide whether this tree can be labeled to become an avl tree. Vertices of the tree do not have keys but numbered from 0 to n − 1 in order to write the adjacency matrix. INPUT format: The input consists of blocks. Blocks are written one after another without empty lines between them. Every block starts with a new line and consists of several...
AVL tree; Insert and Range Minimum operation in Java code.
AVL tree; Insert and Range Minimum operation in Java code.
Write a non recursive method to insert into an AVL tree in Java
Write a non recursive method to insert into an AVL tree in Java
Implement a GUI for AVL Tree for int data type using JavaFX. The GUI should provide...
Implement a GUI for AVL Tree for int data type using JavaFX. The GUI should provide facility to add, remove and find data elements and also traversal (in-order, pre-order, post-order, level-order) of the tree structure. If tree become unbalance, it should display the message that tree become imbalanced. It should provide a button to make the tree a Balance Tree. Extra: It should provide the option to load the data from a file and save the data in a file....
C++ AVL tree My AVL tree function void inorder(AVLNode* t) { if (t == NULL) return;...
C++ AVL tree My AVL tree function void inorder(AVLNode* t) { if (t == NULL) return; inorder(t->left); cout << t->data.cancer_rate << " "; inorder(t->right); } void inorder() { inorder(root); } Will Print my cancer city rate Inorder example) 3.1 5.8 19.8 My question is how can I add a decreasing index to this function example) 3) 3.1 2)5.8 1)19.8
AVL Group assignment POST ANSWER IN JAVA PLS Populate a tree via a text file (input.txt)...
AVL Group assignment POST ANSWER IN JAVA PLS Populate a tree via a text file (input.txt) Make sure that after every insert, the tree is balanced. At the end, display the tree in level format. Make sure to include the height and the balance factor of every node in your output. Redirect the display to an output file (output.txt) Hint: //I will not accept any other algorithm //This is not a recursive algorithm node * rebalance(node *node){     node->height =...
1. Starting with an empty tree, show each step in the construction of an AVL tree...
1. Starting with an empty tree, show each step in the construction of an AVL tree using the following input in the order given. For full credit, you must show the tree after each new input is added. 16, 7, 14, 18, 6, 17, 2, 5, 13, 22, 4 (6 pts.) 2. Show how the AVL tree in previous changes with the following operations. For full credit, you must show the tree after each iteration. Remove: 17 Remove: 18 Remove:...
Develop a Java application to implement a binary tree data structure. A tree data structure starts...
Develop a Java application to implement a binary tree data structure. A tree data structure starts from the top node, called the root. Each node in the tree has a set of children, which are also nodes of the tree and can have children of their own, and so on. This keeps on going till we get to the bottom of the tree, to the “leaf” nodes. Each node in the tree, except for the root, has a parent. A...
i have an array of strings, how do i sort them into a binary tree? C...
i have an array of strings, how do i sort them into a binary tree? C Program
Create a random binary tree and verify BST property and AVL balance condition. Do not hard...
Create a random binary tree and verify BST property and AVL balance condition. Do not hard code the tree inside the code. Java or C++ are the options.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT