0% found this document useful (0 votes)
16 views

AVL Tree Without Comments

The document defines an AVL Tree implementation in Python, including methods for insertion, deletion, and balancing the tree. It also includes functions to read elements from a file, write output to a file, and perform an in-order traversal of the tree. The main function orchestrates reading from an input file, performing operations on the AVL tree, and writing the results to an output file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

AVL Tree Without Comments

The document defines an AVL Tree implementation in Python, including methods for insertion, deletion, and balancing the tree. It also includes functions to read elements from a file, write output to a file, and perform an in-order traversal of the tree. The main function orchestrates reading from an input file, performing operations on the AVL tree, and writing the results to an output file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

class AVLNode:

def __init__(self, key):

self.key = key

self.left = None

self.right = None

self.height = 1

class AVLTree:

def __init__(self):

self.root = None

def insert(self, key):

self.root = self._insert(self.root, key)

def _insert(self, node, key):

if not node:

return AVLNode(key)

if key < node.key:

node.left = self._insert(node.left, key)

elif key > node.key:

node.right = self._insert(node.right, key)

else:

return node

node.height = 1 + max(self._get_height(node.left), self._get_height(node.right))

balance = self._get_balance(node)

if balance > 1 and key < node.left.key:


return self._rotate_right(node)

if balance > 1 and key > node.left.key:

node.left = self._rotate_left(node.left)

return self._rotate_right(node)

if balance < -1 and key > node.right.key:

return self._rotate_left(node)

if balance < -1 and key < node.right.key:

node.right = self._rotate_right(node.right)

return self._rotate_left(node)

return node

def delete(self, key):

self.root = self._delete(self.root, key)

def _delete(self, node, key):

if not node:

return node

if key < node.key:

node.left = self._delete(node.left, key)

elif key > node.key:

node.right = self._delete(node.right, key)

else:

if not node.left:

return node.right
if not node.right:

return node.left

temp = self._get_min_value_node(node.right)

node.key = temp.key

node.right = self._delete(node.right, temp.key)

if not node:

return node

node.height = 1 + max(self._get_height(node.left), self._get_height(node.right))

balance = self._get_balance(node)

if balance > 1 and self._get_balance(node.left) >= 0:

return self._rotate_right(node)

if balance > 1 and self._get_balance(node.left) < 0:

node.left = self._rotate_left(node.left)

return self._rotate_right(node)

if balance < -1 and self._get_balance(node.right) <= 0:

return self._rotate_left(node)

if balance < -1 and self._get_balance(node.right) > 0:

node.right = self._rotate_right(node.right)

return self._rotate_left(node)

return node
def _rotate_left(self, z):

y = z.right

T2 = y.left

y.left = z

z.right = T2

z.height = 1 + max(self._get_height(z.left), self._get_height(z.right))

y.height = 1 + max(self._get_height(y.left), self._get_height(y.right))

return y

def _rotate_right(self, z):

y = z.left

T3 = y.right

y.right = z

z.left = T3

z.height = 1 + max(self._get_height(z.left), self._get_height(z.right))

y.height = 1 + max(self._get_height(y.left), self._get_height(y.right))

return y

def _get_height(self, node):

if not node:

return 0

return node.height

def _get_balance(self, node):

if not node:

return 0

return self._get_height(node.left) - self._get_height(node.right)

def _get_min_value_node(self, node):


if node is None or node.left is None:

return node

return self._get_min_value_node(node.left)

def in_order_traversal(self, node, result):

if node:

self.in_order_traversal(node.left, result)

result.append(node.key)

self.in_order_traversal(node.right, result)

def read_elements_from_file(filename):

try:

with open(filename, 'r') as file:

content = file.read().strip()

print(f"File content read: {content}")

elements = content.split()

return [int(e) for e in elements if e.isdigit()]

except FileNotFoundError:

print(f"File {filename} not found.")

return []

except ValueError as e:

print(f"ValueError encountered: {e}")

return []

def write_elements_to_file(filename, elements):

with open(filename, 'w') as file:

file.write(' '.join(map(str, elements)))


def main():

input_filename = 'input.txt'

output_filename = 'output.txt'

elements = read_elements_from_file(input_filename)

if not elements:

print("No elements to insert into the AVL Tree.")

return

avl_tree = AVLTree()

for element in elements:

avl_tree.insert(element)

avl_tree.insert(15)

avl_tree.delete(20)

result = []

avl_tree.in_order_traversal(avl_tree.root, result)

write_elements_to_file(output_filename, result)

print("Output written to", output_filename)

if __name__ == '__main__':

main()

with open('output.txt', 'r') as file:

output = file.read()

print("Output content:", output)


OUTPUT :-

File content read: 10 20 30 40 50 25 15 5

Output written to output.txt

Output content: 5 10 15 25 30 40 50

You might also like