2. Binary Search Tree
• Each internal node has at most two children
• Ordered Binary Tree or Binary Search Tree
• Children of node are ordered pair known as left, right child
• Left sub tree of node contains nodes with keys < node's key
• Right sub tree of node has nodes with keys ≥ node's key
• Both left and right subtrees must be binary search trees
• Recursive definition: Either a tree of single node, or whose
root has an ordered pair of children, each is binary search tree
3. Binary Search Trees
• BST – A binary tree where a parent’s value is greater than
its left child and less than or equal to it’s right child
)
(
)
( i
right
i
i
left
4. Operations
• Search(T,k) – Does value k exist in tree T
• Insert(T,k) – Insert value k into tree T
• Delete(T,x) – Delete node x from tree T
• Minimum(T) – What is the smallest value in the tree?
• Maximum(T) – What is the largest value in the tree?
• Successor(T,x) – What is the next element in sorted order
after x
• Predecessor(T,x) – What is the previous element in
sorted order of x
• Median(T) – return the median of the values in tree T
6. Searching in a tree (Recursion)
TreeSearch(x, item)
If x=null or item =x->data
return x
if item < x->data
return Search(x->left,item)
else
return Search(x->right,item)
7. Searching in a tree (Iterative)
TreeSearch(x, item)
{
While x ≠ null and item ≠ x->data
{
if item < x->data
x= x->left
else
x= x->right
}
return x
}
if item = x->data
{
Item found
}
else
Not found
8. Complexity of search
• It will depend upon the height of the tree
because with every loop iteration we are
progressing to next level of the loop.
• Worst case: when we have a twig O(n)
• Average case : When tree is nearly complete
O(logn)
• Best Case: O(1)
9. Tree-Minimum (x)
1. while x->left != NULL
2. x= x->left
3. return x
Tree-Maximum (x):
1. while x-> right != NULL
2. x= x-> right
3. return x
10. Finding Successor
• Given x, find the node with the smallest key
greater then key[x]
• Two cases depend upon right sub tree of x
1: Right sub tree of x is nonempty, then
successor is leftmost node in right sub tree
2: Right sub tree of x is empty, then keep
going up until we are no longer a right
child. If there is no such ancestor then
successor is undefined.
• We are going to the next node in inorder
11. Tree-Successor(x):
1. if x-> right != NULL
2. return Tree-Minimum(x->right)
3. y = x-> parent
4. while y != NULL and x = = y->right
{
5. x = y
6. y = y->parent
}
7. return y
12. Insertion In a binary Search Tree
• Take an element z whose left and right
children are null and insert it into T
• Find place in T where z belongs (as if searching
for z) and add z
• Runtime on a tree of height h is O(h)
13. Special Case
• What if we need to insert a duplicate element
• Does the order of insertion matters : Yes, It
will change the topology of the tree
14. Tree-Insert (z)
1. y = NULL
2. x= root
3. while x != NULL
{
4. y = x
5. if z->data < x ->data
6. x = x->left
7. else x = x->right
}
8. z-> parent = y
9. if y = = NULL
10. root = z // Tree T was empty
11. else if (z->data < y->data)
12. y->left = z
13. else y->right = z
17. Case-IV
• To delete root and successor is undefined,
then need to take care of the start pointer
18. Time complexity
Running time for delete, insertion and search
worst case O(n)
Average Case O(logn)
Creating a BST (Inserting n elements one by one)
worst case O(n2 )
Average case O(nlogn)
Inorder traversal of a BST gives a sorted list and
takes O(n) time.