DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Data Structures
KCS301
by
DR PARUL YADAV
Institute of Engineering and Technology, Lucknow
Lecture 28
Binary Search Tree
Contents
• Introduction
• Definition
• Searching
• Insertion
• Deletion
Introduction
• One of the most important data structures in computer
science
• This structure contrasts with the following structures:
(a)Sorted linear array: Here one can search for and find an
element with a running time f(n) = O(log2 n), but it is
expensive to insert and delete elements.
(b)Linked list: Here one can easily insert and delete elements,
but it is expensive to search for and find an element, since
one must use a linear search with running time f(n) = O(n).
Definition
Suppose T is a binary tree. Then T
is called a binary search tree (or
binary sorted tree) if each node
N of T has the following property:
The value at N is greater than
every value in the left subtree of
N and is less than every value in
the right subtree of N.
Searching in BST
(a) Compare ITEM with the root node N of the
tree.
(i) If ITEM < N, proceed to the left child of N.
(ii) If ITEM > N, proceed to the right child of N.
(b) Repeat Step (a) until one of the following
occurs:
(i) We meet a node N such that ITEM = N. In
this case the search is successful.
(ii) (ii) We meet an empty subtree, which
indicates that the search is unsuccessful,
and we insert ITEM in place of the empty
subtree.
Insertion in BST
• Insert 20
Inserting in BST
Insert 40, 60, 50, 33, 55, 11 in empty BST
Procedure: Search
FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR)
A binary search tree T is in memory and an ITEM of information is given.
This procedure finds the location LOC of ITEM in T and also the location
PAR of the parent of ITEM. There are three special cases:
(i) LOC = NULL and PAR = NULL will indicate that the tree is empty.
(ii) LOC ≠ NULL and PAR = NULL will indicate that ITEM is the root of T.
(iii) LOC = NULL and PAR ≠ NULL will indicate that ITEM is not in T and can
be added to T as a child of the node N with location PAR.
1. [Tree empty?] If ROOT = NULL, then: Set LOC := NULL and PAR := NULL,
and Return
2. [ITEM at root?] If ITEM = INFO[ROOT], then: Set LOC := ROOT and PAB :=
NULL, and Return.
Procedure: Search contd.
3. [Initialize pointers PTR and SAVE.]
If ITEM < INFO[ROOT], then: Set PTR := LEFT[ROOT] and SAVE := ROOT.
Else: Set PTR := RIGHT[ROOT] and SAVE := ROOT. [End of If structure.]
4. Repeat Steps 5 and 6 while PTR ≠ NULL:
5. [ITEM found?]
If ITEM = INFO[PTR], then: Set LOC := PTR and PAR := SAVE, and Return.
6. If ITEM < INFO[PTR], then: Set SAVE := PTR and PTR := LEFT[PTR].
Else: Set SAVE := PTR and PTR := RIGHT[PTR].
[End of If structure.]
[End of Step 4 loop.]
7. [Search unsuccessful.] Set LOC := NULL and PAR := SAVE.
8. Exit.
Algorithm: Insert
INSBST(INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM, LOC)
A binary search tree T is in memory and an ITEM of
information is given. This algorithm finds the
location LOC of ITEM in T or adds ITEM as a new
node in T at location LOC.
1. Call FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC,
PAR).
Algorithm: Insert
2. If LOC ≠ NULL, then Exit.
3. [Copy ITEM into new node in AVAIL list.]
(a) If AVAIL = NULL, then: Write: OVERFLOW, and Exit.
(b) Set NEW := AVAIL, AVAIL := LEFT[AVAIL] and INFO[NEW] := ITEM.
(c) Set LOC := NEW, LEFT[NEW] := NULL and RIGHT[NEW] := NULL.
4. [Add ITEM to tree.]
If PAR = NULL, then: Set ROOT := NEW.
Else if ITEM < INFO[PAR], then: Set LEFT[PAR] := NEW.
Else: Set RIGHT[PAR] := NEW. [End of If structure.]
5. Exit
Complexity
• The average running time f(n) to search for an
item in a binary tree T with n elements is
proportional to log2 n, that is,
f(n) = O(log2 n).
Deleting in BST
There are three cases:
Case 1: N has no child.
Case 2: N has exactly one child.
Case 3: N has two children.
Deleting in BST
Case 1:
N has no child.
Then N is deleted
from T by simply
replacing the location
of N in the parent
node P(N) by the null
pointer.
Deleting in BST
Case 2:
N has exactly one child.
Then N is deleted from T
by simply replacing the
location of N in P(N) by
the location of the only
child of N
Deleting in BST
Case 3:
N has two children.
Let S(N) denote the inorder
successor of N.
(We can verify that S(N)
does not have a left child.)
Then N is deleted from T by
first deleting S(N) from T
(by using Case 1 or Case 2)
and then replacing node N
in T by the node S(N).
CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR)
1. [Initializes CHILD.]
If LEFT[LOC] = NULL and RIGHT[LOC] = NULL, then: Set CHILD := NULL.
Else if LEFT[LOC] ≠ NULL, then: Set CHILD := LEFT[LOC].
Else Set CHILD := RIGHT[LOC].
[End of If structure.]
2. If PAR ≠ NULL, then:
If LOC = LEFT[PAR], then: Set LEFT[PAR] := CHILD.
Else: Set RIGHT[PAR] := CHILD.
[End of If structure.]
Else: Set ROOT := CHILD.
[End of If structure.]
3. Return.
CASEB(INFO, LEFT, RIGHT, ROOT, LOC, PAR)
1. [Find SUC and PARSUC.]
(a) Set PTR := RIGHT[LOC] and SAVE := LOC.
(b) Repeat while LEFT[PTR] ≠ NULL:
Set SAVE := PTR and PTR := LEFT[PTR].
[End of loop.]
(c) Set SUC := PTR and PARSUC := SAVE.
2. [Delete inorder successor, using Procedure ]
Call CASEA(INFO, LEFT, RIGHT, ROOT, SUC, PARSUC).
3. [Replace node N by its inorder successor.]
(a) If PAR ≠ NULL, then:
If LOC = LEFT[PAR], then: Set LEFT[PAR] := SUC.
Else: Set RIGHT[PAR] := SUC.
[End of If structure.]
Else: Set ROOT := SUC.
[End of If structure.]
(b) Set LEFT[SUC] := LEFT[LOC] and RIGHT[SUC] := RIGHT[LOC].
4. Return
DEL(INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM)
1. Call FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR).
2. [ITEM in tree?]
If LOC = NULL, then: Write: ITEM not in tree, and Exit.
[Delete node containing ITEM.]
If RIGHT[LOC] ≠ NULL and LEFT[LOC] ≠ NULL, then:
Call CASEB(INFO, LEFT, RIGHT, ROOT, LOC, PAR).
Else:
Call CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR).
[End of If structure.]
4. [Return deleted node to the AVAIL list.]
Set LEFT[LOC] := AVAIL and AVAIL := LOC.
5. Exit.
Thank you