Problem StatementImplement an AVL tree data structure that accepts a series of integers as input and constructs the AVL tree. After inserting all the integers, perform an in-order traversal of the AVL tree and print the elements.Input format :The first line of input consists of the number of nodes in the AVL Tree, N.The second line of input consists of the nodes of the AVL Tree, separated by space.Output format :The output prints the in-order traversal of the constructed AVL Tree, separated by space.
Question
Problem StatementImplement an AVL tree data structure that accepts a series of integers as input and constructs the AVL tree. After inserting all the integers, perform an in-order traversal of the AVL tree and print the elements.Input format :The first line of input consists of the number of nodes in the AVL Tree, N.The second line of input consists of the nodes of the AVL Tree, separated by space.Output format :The output prints the in-order traversal of the constructed AVL Tree, separated by space.
Solution
Sure, here is a Python solution for the problem:
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
self.height = 1
class AVL_Tree:
def insert(self, root, key):
if not root:
return Node(key)
elif key < root.key:
root.left = self.insert(root.left, key)
else:
root.right = self.insert(root.right, key)
root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right))
balance = self.getBalance(root)
if balance > 1:
if key < root.left.key:
return self.rightRotate(root)
else:
root.left = self.leftRotate(root.left)
return self.rightRotate(root)
if balance < -1:
if key > root.right.key:
return self.leftRotate(root)
else:
root.right = self.rightRotate(root.right)
return self.leftRotate(root)
return root
def leftRotate(self, z):
y = z.right
T2 = y.left
y.left = z
z.right = T2
z.height = 1 + max(self.getHeight(z.left), self.getHeight(z.right))
y.height = 1 + max(self.getHeight(y.left), self.getHeight(y.right))
return y
def rightRotate(self, y):
x = y.left
T3 = x.right
x.right = y
y.left = T3
y.height = 1 + max(self.getHeight(y.left), self.getHeight(y.right))
x.height = 1 + max(self.getHeight(x.left), self.getHeight(x.right))
return x
def getHeight(self, root):
if not root:
return 0
return root.height
def getBalance(self, root):
if not root:
return 0
return self.getHeight(root.left) - self.getHeight(root.right)
def preOrder(self, root):
if not root:
return
print("{0} ".format(root.key), end="")
self.preOrder(root.left)
self.preOrder(root.right)
def inOrder(self, root):
if not root:
return
self.inOrder(root.left)
print("{0} ".format(root.key), end="")
self.inOrder(root.right)
myTree = AVL_Tree()
root = None
nodes = int(input())
keys = list(map(int, input().split()))
for key in keys:
root = myTree.insert(root, key)
myTree.inOrder(root)
This Python script first defines a Node class for the AVL tree nodes, and an AVL_Tree class for the tree itself. The AVL_Tree class has methods for inserting nodes, performing left and right rotations, getting the height of a node, getting the balance factor of a node, and performing pre-order and in-order traversals. The script then reads the number of nodes and the node keys from the input, inserts the nodes into the tree, and performs an in-order traversal of the tree.
Similar Questions
Problem StatementImplement an AVL tree that dynamically inserts integers one at a time and prints the tree's height after each insertion. Input integers are provided until -1 is entered, signifying the end of input. The tree must maintain balance using rotations during insertion.Input format :The input consists of a series of non-negative integer keys inserted into the AVL tree, separated by spaces.The series of keys will end with a sentinel value of -1.
Step by step, construct an AVL tree for the following sequence: 144, 151, 162, 70, 41, 120, 158. Draw thetree at each step and give the step-by-step workout.
Problem StatementYou are responsible for maintaining an employee database using an AVL tree, where each employee is identified by a unique integer employee ID. Develop a program to support the following operations:Insert a new employee into the AVL tree, considering their employee ID. Ensure that no duplicate IDs are allowed.Remove an employee with a specific employee ID from the AVL tree.Print the in-order traversal of the AVL tree after each operation to keep track of the employee database efficiently.Input format :The first line consists of an integer N, representing the number of employee IDs to insert initially.The next line consists of N space-separated integers representing the employee IDs to be inserted into the AVL tree.The last line contains an integer representing the employee IDs to be deleted from the AVL tree.
Create an AVL Tree for the given values 21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7. What is the root node element.
create a AVL tree whose element is 55,25, 65, 9,8,15
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.