Binary Search Tree 1
Binary Search Tree 1
A binary search tree is a binary tree that has the following properties
Given a node with value K
All values on the left subtree of K are smaller than K
All values on the right subtree of K are greater than K
There are no repeated values in the tree
12
9 16
2 11 13 21
1 3 10
Binary Search Trees
Desirable characteristics of a binary search tree
They support many operations
Search
Delete
Maximum / Minimum
Basic operations take time proportional to the height of the tree
For complete trees this is O(log n)
Undesirable characteristics of a binary search tree
The time of O(log n) cannot be guaranteed in all cases since the
tree can be a linear chain. This can give us worst case time
proportional to O(n)
Binary Search Trees (BSTs)
Where is the
smallest element?
Ans: leftmost
element
12
9 16
2 11 13 21
1 3 10
search(10)
12
9 16
2 11 13 21
1 3 10
search(10)
12
9 16
2 11 13 21
1 3 10
search(10)
12
9 16
2 11 13 21
1 3 10
search(10)
12
9 16
2 11 13 21
1 3 10
search(10)
12
9 16
2 11 13 21
1 3 10
Implementation of Searching
TREE search(TREE tree, int key)
{
while (tree) {
if (key == tree->data) return tree;
if (key < tree->data)
tree = tree->left_child;
else tree = tree->right_child;
}
return NULL;
}
Insertion in Binary Search Trees
To insert an element in a BST we follow the same principle applied in
the searching.
Compare the value to be inserted with the current key and decide if
the new element should be inserted on the left or on the right
The element is always inserted as a leaf in the tree.
insert
insert (target)
(target)
1.
1. IfIf head
head of of the
the treetree is
is null
null create
create new
new node
node toto store
store target;
target;
2.
2. Test
Test whether
whether the the target
target isis the
the same
same as as the
the key
key inin the
the
head
head ofof the
the tree
tree and
and return
return null
null (this
(this means
means that
that the
the
element
element cannotcannot be be added
added to to the
the tree)
tree)
3.
3. IfIf not
not true,
true, ifif target
target isis less
less than
than the
the key
key in
in the
the head
head insert
insert
the
the target
target inin thethe in
in the
the left
left subtree
subtree
4.
4. IfIf target
target isis greater
greater than than the
the key
key in
in the
the head
head
insert
insert the
the target
target in in the
the right
right subtree
subtree
insert(14)
12
9 16
2 11 13 21
1 3 10
insert(14)
12
9 16
2 11 13 21
1 3 10
insert(14)
12
9 16
2 11 13 21
1 3 10
insert(14)
12
9 16
2 11 13 21
1 3 10
insert(14)
12
9 16
2 11 13 21
1 3 10 14
Does the order of inserting elements into a tree
matter?
Yes, certain orders might produce very
unbalanced trees!
Does the
order of
inserting
elements
into a tree
matter?
(cont.)
main()
{
TREE root = NULL, parent,
child; int i;
scanf("%d", &i);
root =
maketree(i);
while(1)
{
scanf("%d", &i);
if(i<0)break;
parent =
child = root;
while((i !=
parent->data) &&
(child
!= NULL))
{
parent =
child; if(i <
parent->data)
child = parent->left;
else
child = parent-
>right;
TREE maketree(int x)
{
TREE tree = (TREE)malloc(sizeof(struct tree));
tree->left = tree->right = NULL;
tree->data =
x; return
tree;
}
void setleft(TREE
tree, int x)
{
tree->left =
maketree(x);
}
void
setright(TREE
tree, int x)
{
tree->right =
maketree(x);
}
Where is second smallest value present?
56
26 200
18 28 190 213
12 24 27
Where is second smallest value present?
15
8 20
2 11 27
6 10 12 22 30
3 7 14
Predecessor and Successor
Successor of node x is the node y such that
key[y] is the smallest key greater than key[x].
The successor of the largest key is NIL.
Search consists of two cases.
If node x has a non-empty right subtree, then x’s
successor is the minimum in the right subtree of x.
If node x has an empty right subtree, then:
As long as we move to the right up the tree (move
up through right children), we are visiting
smaller keys.
The first node y, whose left subtree contains x, is
the successor of x.
Predecessor and Successor
15
8 20
2 11 27
6 10 12 22 30
3 7 14
Function DeleteItem
First, find the item; then, delete it
Binary search tree property must be preserved!!
We need to consider three different cases:
(1) Deleting a leaf
(2) Deleting a node with only one child
(3) Deleting a node with two children
(1) Deleting a leaf
(2) Deleting a node
with only one
child
(3) Deleting a node with
two children
(3) Deleting a node with
two children (cont.)
Find predecessor (i.e., rightmost node in the left
subtree)
Replace the data of the node to be deleted with
predecessor's data
Delete predecessor node
Exercise To Try
Create a BST from following values
13 5 8 1 33 24 56 41 18 2 3
Delete the root node twice
Applications of Binary Search Trees
Searching
Sorting
Removing Duplicates
Counting frequency
Finding Mode
BINARY SEARCH TREE: AN APPLICATION OF
BINARY TREES
The inorder (left-root-right) traversal of the
Binary Search Tree and printing the info part of
the nodes gives the sorted sequence in ascending
order. Therefore, the Binary search tree
approach can easily be used to sort a given array
of numbers.
Application of Binary Search Tree
Suppose that we wanted to find all duplicates in a list of numbers.
One way of doing this to compare each number with all those that
precede it. However this involves a large number of comparison. The
number of comparison can be reduced by using a binary tree. The
first number in the list is placed in a node that is the root of the
binary tree with empty left and right sub-trees. The other numbers in
the list is than compared to the number in the root. If it is matches,
we have duplicate. If it is smaller, we examine the left sub-tree; if it
is larger we examine the right sub-tree. If the sub-tree is empty, the
number is not a duplicate and is placed into a new node at that
position in the tree. If the sub-tree is nonempty, we compare the
number to the contents of the root of the sub-tree and the entire
process is repeated with the sub-tree. A program for doing this
follows .
Using Binary Search Trees Application:
Removing Duplicates
7
v v
3 9
7 3 2 5 3 2 9 3 2 3 5 7 9
2 5
Making Trees Efficient:
Possible Solutions
Keep BSTs shallow by maintaining “balance”
AVL trees