0% found this document useful (0 votes)
11 views4 pages

AVL Tree

The document discusses AVL trees, including what they are, how balance factors are calculated, how elements are inserted and deleted, and how to implement an AVL tree in Java. AVL trees are balanced binary search trees where the heights of each node's left and right subtrees differ by no more than one.

Uploaded by

haniakhaled615
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

AVL Tree

The document discusses AVL trees, including what they are, how balance factors are calculated, how elements are inserted and deleted, and how to implement an AVL tree in Java. AVL trees are balanced binary search trees where the heights of each node's left and right subtrees differ by no more than one.

Uploaded by

haniakhaled615
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Hanya Khaled

 AVL Tree
223157

In this report, we’ll discuss:

 What is an AVL tree?


 Calculating the balance factor in an AVL tree.
 How to insert elements, and how to delete them?
 implement an AVL tree in Java.

What is an AVL tree?


An AVL tree is a balanced binary search tree, which implies that the heights of
each node's left and right subtrees differ by no more than one.

This invariant is tested after each insert and delete operation, and the balance is
restored via AVL rotation if necessary.

Height of an AVL tree:


The height of a (sub)tree represents the distance between the root and the
lowest node. As a result, a (sub)tree with only a root node has a height of 0.

AVL tree balance factor:


A node's balance factor "BF" is the difference between the heights "H" of the right
and left subtrees ("node.right" and "node.left"):
H = BF(node) (node.right) - H(node.left) (node.left)
A non-existent subtree has a height of -1. (One less than the height of a subtree
consisting of only one node).

There are three possibilities:


1. The node is left-heavy if the balancing factor is 0.
2. If the balancing factor is more than zero, the node is right-heavy.
3. A balanced node has a balance factor of 0.
Each node in an AVL tree has a balance factor of -1, 0, or 1.

Example of an AVL Tree


The diagram below displays an AVL tree with height and balance factor defined at each
node:
Java AVL Tree Implementation

We take the Java source code for the binary search tree to implement the AVL
tree in Java.

The AVL tree is implemented by the AVLTree class. It extends


the BinarySearchTreeRecursive class. We will reuse much of its functionality.
Insertion & deletion:
inserting into and deleting from an AVL tree works basically as binary search
trees.

You might also like