Docsity

Notes on Binary Search Trees - Algorithm Analysis and Design | CPSC 5115G, Study notes of Computer Science

Toggle sidebar
bg1
Binary Search Trees
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:
1) A value that is stored in the node, and
2) Two (possibly empty) pointers, one for the left subtree and
one for the right subtree.
As the binary search tree is populated, it grows from the root “down”.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Notes on Binary Search Trees - Algorithm Analysis and Design | CPSC 5115G and more Study notes Computer Science in PDF only on Docsity!

Binary Search Trees

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:

  1. A value that is stored in the node, and
  2. Two (possibly empty) pointers, one for the left subtree and one for the right subtree. As the binary search tree is populated, it grows from the root “down”.

Binary Search Trees (“Well” Populated)

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”.

What to Do About Unbalanced Trees?

The above unbalanced tree can be balanced by a straightforward algorithm. There are two approaches to producing balanced search trees.

  1. Produce the tree in any desired way and then apply a balancing procedure to produce a balanced tree equivalent to it.
  2. Use a procedure that creates the search tree as a balanced tree, and maintains the tree’s balance during updates. Search trees that are created and updated using special balancing procedures are called balanced search trees. A binary search tree that just happens to be balanced is not called a “balanced search tree”. That term is applied only to trees created and updated using procedures guaranteed to maintain the balance.

Side Remark: Another Search Tree Structure

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.

Notation for Binary Trees

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.

More Notation for AVL Trees

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.

R–Rotations

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.

L–Rotations

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.

Use of the Rotations

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.

Use of the Double Rotations

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.