Question

In: Computer Science

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

Solutions

Expert Solution

// Here is the requiremd method

public void insertAVL(Key key, val i)

{

// Creating node

// node have key and value parameter

Node node = new Node(Key, i);

// check if empty tree

// if yes, then return value as root

if(root==null)

{

    root = i;

    return;

}

// now, intialie parent value as null

Node parent = null, y=root;

// traverse till the null value of root

while(y!=null)

{

// intializeing value of root to parent

parent = y;

// cretae a variable for result

int result = key.compareTo(y.key);

// check if result is negative

if(result<0){

    // store value in left subtree

    y=y.left;

}

// check if result is positive

else if(result>0){

    // assign value to right subtree

    y =y.right;

}

else{

    y.val=val;

    return;

}

}

// assign value of parent node as result

int result=key.compareTo(parent.key);

// check if result is negative

if(result<0){

parent.left=node;

}

else{

parent.right=node;

}

}


Related Solutions

AVL tree; Insert and Range Minimum operation in Java code.
AVL tree; Insert and Range Minimum operation in Java code.
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...
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...
a) Based on the binary tree implementation from the Python program below  write a recursive method that...
a) Based on the binary tree implementation from the Python program below  write a recursive method that calculates the number of leaf nodes in the tree. class Binaertre: def __init__(self, datatobjekt): self.data = datatobjekt self.forelder = None self.venstre_barn = None self.hoyre_barn = None @property def venstre_barn(self): return self.__venstre_barn @venstre_barn.setter def venstre_barn(self, node): self.__venstre_barn = node if node is not None: node.forelder = self @property def hoyre_barn(self): return self.__hoyre_barn @hoyre_barn.setter def hoyre_barn(self, node): self.__hoyre_barn = node if node is not None: node.forelder...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT