Question

Write code in java Implement a method to build an AVL tree out of a sorted...

Write code in java

Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You may use any of the Math class methods (max, min, abs, etc.) as needed.

Hint: Given that the input array is sorted, think of how you might build the tree without doing any rotations.

public class AVLTreeNode {

    public int key;

    public AVLTreeNode left, right;

    public char balanceFactor; // ‘/’, ‘\’, or ‘-‘

    public AVLTreeNode(key, left, right) {

        this.key = key;

        this.left = left;

        this.right = right;

        height = 0;

        balanceFactor = ‘-‘;

    }

}

// builds an AVL tree out of a sorted array of integer keys,

// returns the root of the tree, null if array is empty

public static AVLTreeNode buildAVLTree(int[] arr) {

    // COMPLETE THIS METHOD

}

Homework Answers

Answer #1

ANSWERI

have used O(n) approach.

 AVLTreeNode buildHelper(int[] arr, int start, int end) {   if (start > end) { return null; } /* Get the middle element and make it root */ int mid = (start + end) / 2; /* left subtree */ AVLTreeNode left = buildHelper(arr, start, mid - 1); /* right subtree */ AVLTreeNode right = buildHelper(arr, mid + 1, end); AVLTreeNode node = new AVLTreeNode(arr[mid],left,right); return node; } public static AVLTreeNode buildAVLTree(int[] arr) {     int n = arr.length;     if(n == 0)      {               return null;    }       else    {               return buildHelper(arr,0,n-1);  } }
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
Java Program: You will be inserting values into a generic tree, then printing the values inorder,...
Java Program: You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert...
Java Program: You will be traversing through an integer tree to print the data. Given main(),...
Java Program: You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is: 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 -...
You will be traversing through an integer tree to print the data. Given main(), write the...
You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 -...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Complete the java code as per the comments public class Sorting {    ///////////////////////////////////////////////    //...
Complete the java code as per the comments public class Sorting {    ///////////////////////////////////////////////    // STEP 1 -- Make sorting methods generic    ///////////////////////////////////////////////       /**    * Re-orders the contents given array using the insertion sort algorithm.    *    * @param data The array to be sorted.    */    //TODO: Make me generic to work on any kind of Comparable data!    public static void insertionSort(int[] data)    {        int insert; // temporary...
Lab 9 You will be inserting values into a generic tree, then printing the values inorder,...
Lab 9 You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert...
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort...
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort algorithm. The Merge step merges n elements which takes O(n) time. Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Any code that is found to exceed linear time will fail the tests. Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] import java.util.Scanner; import java.util.ArrayList; public class Solution...
Do • public static String sort(String inputString) o This method returns the sorted version of the...
Do • public static String sort(String inputString) o This method returns the sorted version of the input string. The sorting must be accomplished using an insertion sort o You are not allowed to use Arrays.sort, you need to implement the insertion sort yourself. o We do not care about lower/upper case. Hint: Java's builtin String method toLowerCase will come in handy when sorting o return null if the input string is null or empty. Check empty or null string and...