








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









In order to better understand balanced search trees, it helps to study simple binary search trees, which can become quite unbalanced. Binary search trees can be made to work, if populated correctly. Each node in a binary search tree has two components:
Here is a binary search tree that has been “well populated”. The insertion sequence for this binary tree is (4, 2, 6, 1, 3, 5, 7) It should be obvious that this tree is balanced. Search time is O( log(N) ). However, it is not called a “balanced search tree”.
The above unbalanced tree can be balanced by a straightforward algorithm. There are two approaches to producing balanced search trees.
Just to be complete, we mention index trees. In these trees, the values are not stored in the tree, but in an attached data structure often stored as a linked list. This figure shows a binary tree adapted as an index tree. It is just an example. This structure is used to access data records that are stored on disk. Only the key values are stored in the index structure. The linked list allows for efficient sequential access to the data.
Here is our accidentally balanced tree and its standard representation The height of each of the left and right subtrees is 1. This happens to be an AVL tree.
Here are some of the standard figures used in depicting AVL trees. Balanced Balanced Becoming Unbalanced Balance Factor = 0 Balance Factor = –1 Balance Factor = – In the figure to the right, the pendant vertex is intended to denote the last vertex added to a AVL tree that previously had been balanced with balance factor = –1.
We start with an AVL tree. Our example has two nodes. Adding a leaf node to the left subtree causes the balance factor to become 2. This case is fixed by a simple R–rotation.
Add a node to an AVL tree. Again, our sample has two nodes before insertion. Adding a node to the right subtree causes the balance factor at the root to become –2. This is fixed by a left rotation.
In building a AVL tree, we apply the rotations as needed after inserting new nodes. Right Rotations A new key is inserted into the left subtree of the left child of a tree whose root node had a balance factor of +1 before the insertion. Its left subtree is deeper. Left Rotations A new key is inserted into the right subtree of the right child of a tree whose root node had a balance factor of –1 before the insertion. The right subtree is deeper.
Double Left–Right Rotation A new key is inserted into the right subtree of the left child of a tree whose root node had a balance factor of +1 before the insertion. Double Right–Left Rotation A new key is inserted into the left subtree of the right child of a tree whose root node had a balance factor of –1 before the insertion.