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.
Write a JAVA program to modify the insert and remove of a binary search tree to...
Write a JAVA program to modify the insert and remove of a binary search tree to become an AVL tree.
Insert the following data into an AVL tree and show the steps 10, 20, 30, 25,...
Insert the following data into an AVL tree and show the steps 10, 20, 30, 25, 40, 50, 35, 33, 37, 60, 38.
How do you implement an AVL tree for strings in Java?
How do you implement an AVL tree for strings in Java?
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...
Please write in java: Write a recursive method toNumber that forms the integer sum of all...
Please write in java: Write a recursive method toNumber that forms the integer sum of all digit characters in a string. For example, the result of toNumber("3ac4") would be 7. Hint: If next is a digit character ('0' through '9'), Character.isDigit(next) is true and the numeric value of next is Character. digit(next, 10).
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT