Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

OPTIMAL BINARY SEARCH

TREE
ITRODUCTION

An optimal binary search tree (Optimal BST),


sometimes called a weight-balanced binary
tree,[1] is a binary search tree which provides
the smallest possible search time (or expected
search time) for a given sequence of accesses
(or access probabilities).
Optimal BSTs are generally divided into two types: static and
dynamic.

In the static optimality problem, the tree cannot be modified after it


has been constructed. In this case, there exists some particular layout
of the nodes of the tree which provides the smallest expected search
time for the given access probabilities. Various algorithms exist to
construct or approximate the statically optimal tree given the
information on the access probabilities of the elements.

In the dynamic optimality problem, the tree can be modified at any


time, typically by permitting tree rotations. The tree is considered to
have a cursor starting at the root which it can move or use to perform
modifications. In this case, there exists some minimal-cost sequence
of these operations which causes the cursor to visit every node in the
target access sequence in order. The splay tree is conjectured to have
a constant competitive ratio compared to the dynamically optimal
tree in all cases, though this has not yet been proven.
1. What is Binary Tree
Before we get in the details of the binary
search tree in Java and how to implement
it, it’s very important that we clearly
understand what is a binary tree and
what are its different characteristics.
This will help us when we will implement
the binary tree in Java. A binary tree is a
non-linear data structure where data
objects are generally organized in terms
of hierarchical relationship. A binary tree
is a tree where every node has 2 for fewer
children. We call these nodes as left and
right child of the binary tree.
EXAMPLE 1

2
4 6 7
5

8 9
04 of 15

In binary tree, every node contains the following part:


1. Data (actual data)


2. pointer to left child.
3. pointer to right child.
This is how a fypical node will look like in java
2. What is Binary Search Tree
This is a very common question “What is Binary Search tree?“. To put it simply, a binary search tree is a binary tree with
the following properties.

1. Left child is always less than the parent.


2. Right child is always greater than the parent.
3. The left and right sub-tree each must be a binary search tree.
The binary search tree allows a faster search and deletion of items from the tree. Binary search tree also known as ordered
or sorted binary tree.
Binary search tree is one of the fundamental data structure and used to create more abstract data structure like:

1. Sets.
2. Multisets.
3. Associative arrays

52

15
56

54 61
9 11

3 5
Binary Trees
•Binary tree: Each node has at most 2 children
(branching factor 2)
•Binary tree is
• A root (with data)
• A left subtree (may be empty)
• A right subtree (may be empty)
Given a sorted array key [0.. n-1] of search keys and an array freq[0.. n-1] of
frequency counts, where freq[i] is the number of searches for keys[i]. Construct a
binary search tree of all keys such that the total cost of all the searches is as small as
possible.
Let us first define the cost of a BST. The cost of a BST node is the level of that node
multiplied by its frequency. The level of the root is 1.
EXAMPLE
Input: keys[] = {10, 12}, freq[] = {34, 50}
There can be following two possible BSTs
10 12
\ /
12 10
I II
Frequency of searches of 10 and 12 are 34 and 50
respectively.
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118

Input: keys[] = {10, 12, 20}, freq[] = {34, 8, 50}


There can be following possible BSTs
10 12 20 10 20
\ / \ / \ /
12 10 20 12 20 10
\ / / \
20 10 12 12
I II III IV V
Among all possible BSTs, cost of the fifth BST is
minimum.
Cost of the fifth BST is 1*50 + 2*34 + 3*8 = 142
Complexity Analysis of Optimal Binary
Search Tree
It is very simple to derive the
complexity of this approach from the
above algorithm. It uses three nested
loops. Statements in the innermost
loop run in Q(1) time. The running
time of the algorithm is computed as
Thank You
So Much!

You might also like