Binary Search Trees

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

Binary Search Trees/ordered binary tree

• A binary search tree is a binary tree


• It may be empty. If not empty, then it has the following
properties:
– Every element(including the root) has a key, and all keys are
distinct
– The keys(if any) in the left subtree are smaller than the key in the
root
– The keys(if any) in the right subtree are larger than the key in the
root
• Examples of binary tree with distinct keys:
Binary Search Trees

A binary search tree Not a binary search tree


Binary Search Tree Class

template <class Etype>


class TreeNode
{
protected:
Etype element;
TreeNode* left;
TreeNode* right;
TreeNode ( Etype E=0, TreeNode* L=NULL,
TreeNode* R=NULL) :
element( E ), left( L ), right( R )
{
}
friend int Height( TreeNode* T);
friend class BinarySearchTree<Etype>;
};
Operations of Binary Tree
• Insertion Operation
• Searching Operation
• Deletion Operation
• Deletion Leaf Operation
Searching a BST
• Searching a binary tree for a specific value can be a recursive or iterative
process. This explanation covers a recursive method.

• We begin by examining the root node.


• If the tree is null, the value we are searching for does not exist in the tree.
• If the value equals the root, the search is successful. If the value is less
than the root, search the left subtree.

• Similarly, if it is greater than the root, search the right subtree. This
process is repeated until the value is found or the indicated subtree is null.
• If the searched value is not found before a null subtree is reached, then the
item must not be present in the tree.
Searching a BST
To search for an element with key x:
Begin at root
If root is nil then
search tree has no elements and search is
unsuccessful
Else compare x with key in root
If X equals search key then
search terminates successfully
If x less than key in root, then
search left subtree
Else if x greater than key in root then
search right subtree.
Programming logic
if (T == NULL) return NULL;
else
if ( X < T->element ) return find(
X, T->left );
else
if ( X > T->element ) return find(
X, T->right );
else
return T;
Recursion VS iteration
• Iteration means the act of repeating a process usually with
the aim of approaching a desired goal or target or result.
Each repetition of the process is also called an "iteration",
and the results of one iteration are used as the starting
point for the next iteration.
• Recursion is method where the solution to a problem
depends on solutions to smaller instances of the same
problem
Recursion VS iteration
• Computing the recurrence relation for n = 4:
• b4
• =4 * b3
= 4 * 3 * b2
• = 4 * 3 * 2 * b1
• = 4 * 3 * 2 * 1 * b0
• =4*3*2*1*1
• =4*3*2*1
• =4*3*2
• =4*6
• = 24
• Iteration:
• var i, a := 0
• for i from 1 to 3
• {
• a := a + I
• }
• print a
Insertion into a BST
•Insertion begins as a search would begin; if the root is not
equal to the value, we search the left or right subtrees as
before.
•Eventually, we will reach an external node and add the
value as its right or left child, depending on the node's
value.
•In other words, we examine the root and recursively insert
the new node to the left subtree if the new value is less than
the root, or the right subtree if the new value is greater than
or equal to the root.
Insertion into a BST
• To insert new element, key must be unique (unless duplicates are
allowed)
• Search first for the key (i.e., find key)
• If key not found, then insert new element at point where search
terminated
• Example: insert 80,16,25,4,37 into following tree
Recursive Insert Routine
/*2*/ {
/*3*/ if (t == NULL)
/*4*/ {
/*5*/ t = new TreeNode <Etype> (x);
/*6*/ }
/*7*/ else
/*8*/ if (x < t->element)
/*9*/ Insert(x, t->left);
/*10*/ else
/*11*/ if (x > t->element)
/*12*/ Insert(x, t->right);
/*13*/ //else x is in tree already. Do nothing.
/*14*/ }

Handling duplicate keys:


† Keep extra field in node indicating frequency of occurrence.
† If key is part of a larger record, all records with same key can be stored in a
second
† data structure (list or another tree)
Binary Search Tree – Deletion
• Deleting a leaf (node with no children): Deleting a leaf is easy, as
we can simply remove it from the tree.
• Deleting a node with one child: Delete it and replace it with its child.
• Deleting a node with two children: Call the node to be deleted "N".
Do not delete N. Instead, choose either its in-order successor node or
its in-order predecessor node, "R". Replace the value of N with the
value of R, then delete R. (Note: R itself has up to one child.)
• As with all binary trees, a node's in-order successor is the left-most
child of its right subtree, and a node's in-order predecessor is the right-
most child of its left subtree. In either case, this node will have zero or
one children. Delete it according to one of the two simpler cases
above.
Binary Search Tree – Deletion
• Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal node
a) Replace with largest value Y on left subtree
OR smallest value Z on right subtree
b) Delete replacement value (Y or Z) from subtree
Observation
– O( log(n) ) operation for balanced tree
– Deletions may unbalance tree
Deletion in BST
• Delete a leaf: set parent pointer to nil and dispose the node.
– Ex. delete 80

• Delete nonleaf element that has only one child:


– node containing the element to be deleted is disposed, and the single-child takes the
place of the disposed node.
– Ex. delete 16

• Delete a nonleaf node that has two children:


– replace the element by either the largest element in its left subtree or thesmallest one
in its right subtree.
– delete this replacing element from the subtree from which it was taken.
– Ex. delete 5 -- replace 5 by 4 or by 25
Example Deletion (Leaf)
• Delete ( 25 )
10 10
10 < 25, right
5 30 30 > 25, left 5 30
25 = 25, delete
2 25 45 2 45
Example Deletion (Internal Node)
• Delete ( 10 )
10 5 5

5 30 5 30 2 30

2 25 45 2 25 45 2 25 45

Replacing 10 Replacing 5 Deleting leaf


with largest with largest
value in left value in left
subtree subtree
Example Deletion (Internal Node)
• Delete ( 10 )
10 25 25

5 30 5 30 5 30

2 25 45 2 25 45 2 45

Replacing 10 Deleting leaf Resulting tree


with smallest
value in right
subtree
BST Analysis
• Computational complexity (number of comparisons made during
search) is proportional to number of levels in the search path needed
to locate the node being inserted/deleted (this path maximum depth =
depth of search tree).
• Best performance: when tree is perfectly/nearly balanced
• Worst performance: when elements are ordered and path equals
number of nodes in tree
• Average path length of a random search tree is expected to be O(log n)
because in constant time when we go down a level, we operate on a
tree approximately half the size
Average Case Analysis
• To prove log n bound we first need to prove that average
search/insertion time is O(log n) for any node in a BST
• We first assume that all binary search trees are equally likely
• Then we calculate the average internal path length of all possible
binary search trees
• From this, we obtain the average depth of a single node
• Value of key in root determines shape of the tree

• Consider the following examples (elements are given in the order they
have been inserted:

List 1: {38,25,30,45,47,8,2,40,120} List 2: {25,38,30,45,47,8,2,40,120}

List 3: {47,38,25,30,45,8,2,40,120}
findMin/ findMax
• Return the node containing the smallest element in the tree
• Start at the root and go left as long as there is a left child. The
stopping point is the smallest element

• Similarly for findMax


• Time complexity = O(height of the tree)

You might also like