algorithm-notes
  • Preface
  • Outline
    • Asymptotic-Analysis
    • Divide-and-Conquer (DnC)
      • Overview
      • Multiplication
      • DnC Strategy
      • Master Method
    • Randomization
      • Overview
      • Discrete Probability
      • Randomized Algorithms
      • Reservoir Sampling
    • Sorting
      • Overview
      • Selection Sort
      • Bubble Sort
      • Insertion Sort
      • Shell Sort
      • Merge Sort
      • Heap Sort
      • Quick Sort
      • Non-Comparison Sort
    • Graph Algorithms
      • Overview
      • Minimum Spanning Tree
      • Shortest Path
      • Topological Sort
    • Tree
      • Overview
      • Balanced Search Tree
        • AVL Tree
        • Red-Black Tree
        • B Tree
      • Heap
      • Disjoint Set
      • Threaded Binary Tree
      • Segment Tree
    • Searching
      • Overview
      • Binary Search
      • Graph Search
      • Hash Table
      • String Search
    • Dynamic Programming
      • Overview
      • Fibonacci Numbers
      • LCS problem
    • Greedy Algorithms
      • Overview
Powered by GitBook
On this page
  • Pseudocode
  • Binary Search Tree (BST)
  • Traversal of BST
  • Searching Performance
  • Rotation
  • Prevalent Implementations of Balanced Search Tree

Was this helpful?

  1. Outline
  2. Searching

Binary Search

PreviousOverviewNextGraph Search

Last updated 6 years ago

Was this helpful?

is built around the foundation of Divide-and-Conquer and its fundamental idea is to locate an item in an ordered list of items in a recursive way.

It is worth noted that performing binary search operation in whatever structures of ordered items are the same given the premise traversing through the items is relatively costless. Whereas, performing INSERTION or DELETION operations in commonly seen sorted structures such as Array or List takes up to Ο(n) complexity; While a prevalent implementation that involves a concept of Tree, could boost the INSERTION and DELETION operations, meanwhile rapidly solve searching problems in a simple yet robust way.

Note: If only demanding fast lookup operation in the algorithm design, the would be better given its Ο(1) running time.

Pseudocode

BINARY_SEARCH(array, start, end, target)
  middle := (start + end) / 2
  if array[middle] = target
    return array[middle]
  else if array[middle] > target
    BINARY_SEARCH(array, start, middle - 1, target)
  else if array[middle] < target
    BINARY_SEARCH(array, middle + 1, end, target)

Binary Search Tree (BST)

is organized on the basis of a structure of binary tree and is a rooted tree; It could be represented in a dynamic list wherein the nodes contain information about pointer to the left, right and parent subtree.

Figure 1. Binary Search Tree Graphical Representation

The design idea of is that given every possible node x in the tree, all keys in the left subtree are smaller than x while all keys in the right subtree are larger than x.

Traversal of BST

  • In-order Tree Walk

The output key entries of sub-trees follows the pattern of left subtree, root and right subtree. e.g. the In-order Tree Walk of Figure 1 binary tree yields 1, 3, 4, 6, 7, 8, 10, 13, 14.

  • Pre-order Tree Walk

The output key entries of sub-trees follows the pattern of root, left subtree and right subtree. e.g. the Pre-order Tree Walk of Figure 1 binary tree yields 8, 3, 1, 6, 4, 7, 10, 14, 13.

  • Post-order Tree Walk

The output key entries of sub-trees follows the pattern of left subtree, right subtree and root. e.g. the Post-order Tree Walk of Figure 1 binary tree yields 1, 4, 7, 6, 3, 13, 14, 10, 8.

Searching Performance

Rotation

Rotations are called when the BBST properties are violated. There are two kinds of rotations: left rotation and right rotation; they both operate on the pointers among the nodes to maintain the balanced features of the tree.

  • Left Rotation

As shown in figure 2, left rotate operation can be formalized as LEFT-ROTATE(T, y), which transforms the right child of y to be the parent of y in the new tree and assigns the β child of x to y.



LEFT_ROTATE(T, y)
  x = y.right
  y.right = x.left
  if x.left ≠ null
    x.left.parent = y
  x.parent = y.parent
  if y.parent == null
    T.root = x
  else if y == y.parent.left
    y.parent.left = x
  else
    y.parent.right = x
  x.left = y
  y.parent = x
  • Right Rotation

As shown in figure 2, right rotate operation can be formalized as RIGHT-ROTATE(T, x), which transforms the left child of x to be the parent of x in the new tree and assigns the β child of y to x.



RIGHT_ROTATE(T, x)
  y = x.left
  x.left = y.right
  if y.right ≠ null
    y.right.parent = x
  y.parent = x.parent
  if x.parent == null
    T.root = y
  else if x.parent.left = x
    x.parent.left = y
  else
    x.parent.right = y
  y.right = x
  x.parent = y

Prevalent Implementations of Balanced Search Tree

There are popular implementations like AVL Tree and Red-Black Tree in practice, and they both propose solutions to help search operation obtain a guaranteed Θ(log n) complexity.

Traverse through an entire structure of recursively or iteratively has three distinct ways: in-order tree walk, pre-order tree walk and post-order tree walk.

Basic operations cost of a is proportional to the height of such tree. Given a randomly built binary search tree with n keys, it is expected to have a tree of height log(n) and the searching consumption is Θ(log n).

It requires a running time of Ο(n) if the arriving sequence of data is monotonically increasing or decreasing during the binary tree formation. Additionally, during the INSERTION or DELETION operations on the tree, there is possibility that properties of binary tree are violated. Therefore, it is necessary to form a tree structure with a relative balanced entry distribution called Balanced Binary Search Tree (BBST), which means both sides of the tree or subtree have a relative same height. In order to maintain such property of BBST even during massive INSERTION or DELETION operations on the tree, a special operation comes into play.

This method is called when (in , height difference between the right subtree and the left subtree is more than 1).

This method is called when (in , the height difference between the left subtree and the right subtree is more than 1).

Figure 2. Binary Search Tree Rotations

- a first balanced binary search tree structure ever invented.

- a sophisticated while popular balanced search tree in various systems.

- a self-adjusting structure to achieve a balanced search tree.

- a distinct tree structure designed to provide BBST solution with high performance for INSERTION operation.

- a practical data structure designed for the database.

AVL Tree
AVL Tree
AVL Tree
Red Black Tree
Splay Tree
2-3 Tree
B-Tree
BST
Binary Search Tree
worst-case
Rotation
Hash Table
Binary Search
Binary Search Tree
BST
BST