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
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
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.
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
(Java) Implement a RightThreadTree class as an extension of a BinarySearchTree A right-threaded tree is a...
(Java) Implement a RightThreadTree class as an extension of a BinarySearchTree A right-threaded tree is a binary search tree in which each right link that would normally be null is a "thread" that links that node to its inorder successor. The thread enables nonrecursive algorithms to be written for search and inorder traversals that are more efficient than recursive ones. Implement a RightThreadTree class as an extension of a BinarySearchTree. You will also need an RTNode that extends the Node...
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print...
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print out the data value for each node it encounters in the insert operation. Remember that the module AVL tree code gets part of its functionality from the BST type, since an AVL tree is a binary search tree, but adds some additional functionality. 2. Add code to the main method to meet the following requirements: (a) Create an AVL tree to hold names stored...
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
Please do this in java program. In this assignment you are required to implement the Producer...
Please do this in java program. In this assignment you are required to implement the Producer Consumer Problem . Assume that there is only one Producer and there is only one Consumer. 1. The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT