Question

In: Computer Science

AVL tree; Insert and Range Minimum operation in Java code.

AVL tree; Insert and Range Minimum operation in Java code.

Solutions

Expert Solution

1.) INSERTION

class Node { 
    int key, height; 
    Node left, right; 
  
    Node(int d) { 
        key = d; 
        height = 1; 
    } 
} 
  
class AVLTree { 
  
    Node root; 
  
    
    int height(Node N) { 
        if (N == null) 
            return 0; 
  
        return N.height; 
    } 
  
    
    int max(int a, int b) { 
        return (a > b) ? a : b; 
    } 
  
   
    Node rightRotate(Node y) { 
        Node x = y.left; 
        Node T2 = x.right; 
  
        
        x.right = y; 
        y.left = T2; 
  
        
        y.height = max(height(y.left), height(y.right)) + 1; 
        x.height = max(height(x.left), height(x.right)) + 1; 
  
        
        return x; 
    } 
  
    
    Node leftRotate(Node x) { 
        Node y = x.right; 
        Node T2 = y.left; 
  
        y.left = x; 
        x.right = T2; 
  
        
        x.height = max(height(x.left), height(x.right)) + 1; 
        y.height = max(height(y.left), height(y.right)) + 1; 
  
        
        return y; 
    } 
  
    
    int getBalance(Node N) { 
        if (N == null) 
            return 0; 
  
        return height(N.left) - height(N.right); 
    } 
  
    Node insert(Node node, int key) { 
  
       
        if (node == null) 
            return (new Node(key)); 
  
        if (key < node.key) 
            node.left = insert(node.left, key); 
        else if (key > node.key) 
            node.right = insert(node.right, key); 
        else 
            return node; 
  
       
        node.height = 1 + max(height(node.left), 
                              height(node.right)); 
  
       
        int balance = getBalance(node); 
  
        
        if (balance > 1 && key < node.left.key) 
            return rightRotate(node); 
  
        if (balance < -1 && key > node.right.key) 
            return leftRotate(node); 
  
        
        if (balance > 1 && key > node.left.key) { 
            node.left = leftRotate(node.left); 
            return rightRotate(node); 
        } 
  
       
        if (balance < -1 && key < node.right.key) { 
            node.right = rightRotate(node.right); 
            return leftRotate(node); 
        } 
  
        
        return node; 
    } 
  
    void preOrder(Node node) { 
        if (node != null) { 
            System.out.print(node.key + " "); 
            preOrder(node.left); 
            preOrder(node.right); 
        } 
    } 
  
    public static void main(String[] args) { 
        AVLTree tree = new AVLTree(); 
  
        
        tree.root = tree.insert(tree.root, 10); 
        tree.root = tree.insert(tree.root, 20); 
        tree.root = tree.insert(tree.root, 30); 
        tree.root = tree.insert(tree.root, 40); 
        tree.root = tree.insert(tree.root, 50); 
        tree.root = tree.insert(tree.root, 25); 
  
       
        System.out.println("Preorder traversal" + 
                        " of constructed tree is : "); 
        tree.preOrder(tree.root); 
    } 
} 

2.) Range minimum

class SegmentTreeRMQ 
{ 
    int st[]; 
  
   
    int minVal(int x, int y) { 
        return (x < y) ? x : y; 
    } 
  
  
    int getMid(int s, int e) { 
        return s + (e - s) / 2; 
    } 
  
   
      
    int RMQUtil(int ss, int se, int qs, int qe, int index) 
    { 
       
        if (qs <= ss && qe >= se) 
            return st[index]; 
  
       
        if (se < qs || ss > qe) 
            return Integer.MAX_VALUE; 
  
         
        int mid = getMid(ss, se); 
        return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1), 
                RMQUtil(mid + 1, se, qs, qe, 2 * index + 2)); 
    } 
  
  
    int RMQ(int n, int qs, int qe) 
    { 
        
        if (qs < 0 || qe > n - 1 || qs > qe) { 
            System.out.println("Invalid Input"); 
            return -1; 
        } 
  
        return RMQUtil(0, n - 1, qs, qe, 0); 
    } 
  
    
    int constructSTUtil(int arr[], int ss, int se, int si) 
    { 
       
        if (ss == se) { 
            st[si] = arr[ss]; 
            return arr[ss]; 
        } 
  
        int mid = getMid(ss, se); 
        st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1), 
                constructSTUtil(arr, mid + 1, se, si * 2 + 2)); 
        return st[si]; 
    } 
  
   
    void constructST(int arr[], int n) 
    { 
        int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); 
  
      
        int max_size = 2 * (int) Math.pow(2, x) - 1; 
        st = new int[max_size]; 
  
       
        constructSTUtil(arr, 0, n - 1, 0); 
    } 
  
   
    public static void main(String args[])  
    { 
        int arr[] = {1, 3, 2, 7, 9, 11}; 
        int n = arr.length; 
        SegmentTreeRMQ tree = new SegmentTreeRMQ(); 
  
       
        tree.constructST(arr, n); 
  
        int qs = 1;  
        int qe = 5; 
  
        
        System.out.println("Minimum of values in range [" + qs + ", "
                           + qe + "] is = " + tree.RMQ(n, qs, qe)); 
    } 
} 

Related Solutions

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
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...
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...
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key...
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key and then takes it out of the tree. The program can be C++/Java or C-style pseudocode. Do not use function call to either Min or delete done in the class. Write this from scratch. (No need to ensure AVL properties, just show it for a generic BST) Node * extract min(Node * x) { }
Write code for O(n) worst-case algorithm that verifies that a tree is actually an AVL tree by detecting imbalance.
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 file2. Create a Book Object; and an AVL node object to be inserted into the AVL tree3. At each insert, detect imbalance and fix the AVL tree4. Report each imbalance detection and the node...
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
Give the entire code for Minimum spanning tree using Boruvka's algorithm in java (not copy-pasted from...
Give the entire code for Minimum spanning tree using Boruvka's algorithm in java (not copy-pasted from any other website)
Write a simple Java code to make a Binary Tree for the following tree: Don’t use...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use serializable interface implantation /** Class to encapsulate a tree node. */ protected static class Node implements Serializable {
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace std; AVLTree::AVLTree() { root = NULL; } AVLTree::~AVLTree() { delete root; root = NULL; } // insert finds a position for x in the tree and places it there, rebalancing // as necessary. void AVLTree::insert(const string& x) { // YOUR IMPLEMENTATION GOES HERE } // remove finds x's position in the tree and removes it, rebalancing as // necessary. void AVLTree::remove(const string& x) {...
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT