You are on page 1of 6

Introduction To Binary Trees

A binary tree is made of nodes, where each node contains a "left" pointer, a "right"
pointer, and a data element. The "root" pointer points to the topmost node in the tree.
The left and right pointers recursively point to smaller "subtrees" on either side. A
null pointer represents a binary tree with no elements -- the empty tree. The formal
recursive definition is: A binary tree is either empty or is made of a single node,
where the left and right pointers each point to a binary tree.

AVL Trees
It's a simple and efficient data structure to maintain balance

In computer science, an AVL tree is a self-balancing binary search tree, and it was
the first such data structure to be invented. In an AVL tree, the heights of the two
child subtrees of any node differ by at most one. Lookup, insertion, and deletion all
take O(log n) time in both the average and worst cases, where n is the number of
nodes in the tree prior to the operation. Insertions and deletions may require the tree
to be rebalanced by one or more tree rotations.

The AVL tree is named after its two Soviet inventors, G.M. Adelson-Velskii and
E.M. Landis, who published it in their 1962 paper "An algorithm for the organization
of information".

An AVL tree is a special type of binary tree that is always "partially" balanced. The
criteria that is used to determine the "level" of "balanced-ness" is the difference
between the heights of subtrees of a root in the tree.

The "height" of tree is the "number of levels" in the tree. Or to be more formal, the
height of a tree is defined as follows:
1. The height of a tree with no elements is 0
2. The height of a tree with 1 element is 1
3. The height of a tree with > 1 element is equal to 1 + the height of its tallest subtree.

An AVL tree is a binary tree in which the difference between the height of the right
and left subtrees (or the root node) is never more than one.

The height of a tree is the length of the longest path from the root node to a leaf
node.
The balance factor of a node is the height of its left subtree minus the height of its
right subtree and a node with balance factor 1, 0, or −1 is considered balanced.

Balance factor = height of left subtree – height of right subtree

A node with any other balance factor is considered unbalanced and requires
rebalancing the tree. The balance factor is either stored directly at each node or
computed from the heights of the subtrees.

A binary search tree has the AVL property if the balance factor for each node does
not have magnitude greater than one. In other words, each left and right subtree for
each node differ in height by at most one.

To describe AVL trees we need the concept of tree height, which we define as the
maximal length of a path from the root to a leaf. So the empty tree has height 0, the
tree with one node has height 1, a balanced tree with three nodes has height 2. If we
add one more node to this last tree is will have height 3. Alternatively, we can define
it recursively by saying that the empty tree has height 0, and the height of any node is
one greater than the maximal height of its two children. AVL trees maintain a height
invariant (also sometimes called a balance invariant).

Height Invariant: At any node in the tree, the heights of the left and right subtrees
differ by at most 1.
As an example, consider the following binary search tree of height 3.

If we insert a new element with a key of 14, the insertion algorithm for binary search
trees without rebalancing will put it to the right of 13.
Now the tree has height 4, and one path is longer than the others. However, it is easy
to check that at each node, the height of the left and right subtrees still differ only by
one. For example, at the node with key 16, the left subtree has height 2 and the right
subtree has height 1, which still obeys our height invariant.

Now consider another insertion, this time of an element with key 15. This is inserted
to the right of the node with key 14.

All is well at the node labeled 14: the left subtree has height 0 while the right subtree
has height 1. However, at the node labeled 13, the left subtree has height 0, while the
right subtree has height 2, violating our invariant. Moreover, at the node with key 16,
the left subtree has height 3 while the right subtree has height 1, also a difference of 2
and therefore an invariant violation.

We therefore have to take steps to rebalance tree. We can see without too much
trouble, which we can restore the height invariant if we move the node labeled 14 up
and push node 13 down and to the right, resulting in the following tree.
The question is how to do this in general. In order to understand this we need a
fundamental operation called a rotation, which comes in two forms, left rotation and
right rotation.

* Single Right Rotation (SRR)


Performed when A is unbalanced to the left (the left subtree is 2 higher than
the right subtree) and B is left-heavy (the left subtree of B is 1 higher than
the right subtree of B).

* Single Left Rotation (SLR)


performed when A is unbalanced to the right (the right subtree is 2
higher than the left subtree) and B is right-heavy (the right subtree of B
is 1 higher than the left subtree of B).

The AVL tree is the earliest kind of balanced tree. It’s named after its inventors:
Adelson-Velskii and Landis. In AVL trees each node stores an additional piece of
data: the difference between the heights of its left and right subtrees. This
difference may not be larger than 1. That is, the height of a node’s left subtree may
be no more than one level different from the height of its right subtree.

Following insertion, the root of the lowest subtree into which the new node was
inserted is checked. If the height of its children differs by more than 1, a single or
double rotation is performed to equalize their heights. The algorithm then moves up
and checks the node above, equalizing heights if necessary. This continues all the
way back up to the root.
Search times in an AVL tree are O(log N) because the tree is guaranteed to be
balanced. However, because two passes through the tree are necessary to insert (or
delete) a node, one down to find the insertion point and one up to rebalance the
tree.

You might also like