0% found this document useful (0 votes)
3 views18 pages

Binary Search

Uploaded by

Halima Rashid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

Binary Search

Uploaded by

Halima Rashid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Binary Search Tree

Presented By
Halima Binta Rashid
Binary Search Tree

A Binary Search Tree is a binary tree with the following


properties:
(a). All items in the left subtree are less than the root.
(b). All items in the right subtree are greater or equal to
the root.
(c). Each subtree is itself a binary search tree.

Root

2
All < Root Root<= All
Cont.
Following is a pictorial representation of BST

We observe that the root node key (27) has all less-valued
keys on the left sub-tree and the higher valued keys on the
right sub-tree. 3

There must be no duplicate nodes.


Cont.
Which of the above trees are binary search trees?
 (a) and (b)
Why isn’t (b) a binary search tree?
 It violates the property no. 3
6 6

2 8
2
8

1 4 1 4

7 4
3

A Binary Search Tree Not a Binary Search Tree


Example of sorting using BST
Input Sequence: 2 3 8 4 5 7 6 1 2.5 2.4
Creating Binary Search Tree of above given input sequence
2

3
1

8
2.5

4
2.4

7 5

6
BST Searching
Suppose T is a binary search tree.
Suppose an ITEM of information is given. The following
algorithm finds the location of ITEM in the binary search tree
T, or inserts ITEM as a new node in its appropriate place in
the tree.
(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). We meet an empty subtree, which indicates that the search is 6

unsuccessful, and we insert ITEM in place of the empty subtree.


Example
Consider the binary search tree T in fig. Suppose ITEM=20 is
given. Simulating the above algorithm, we obtain the following
steps:
(1). Compare ITEM=20 with the root, 38, of the tree T. Since
20<38, proceed to the left child of 38, which is 14.
(2). Compare ITEM=20 with 14. Since 20>14, proceed to the right
child of 14, which is 23.
(3). Compare ITEM=20 with 23. Since 20<23, proceed to the left
child of 23, which is 18.
(4). Compare ITEM=20 with 18. Since 20>18 and 18 does not have
a right child, insert 20 at the right child of 18.

7
Cont.

38 38

14 56 14 56

45 82 45 82
8 23 8 23

70 70
18 18

20

Fig ITEM=20 Inserted


Cont.

9
Cont.

10
Successor and Predecessor
The Successor of a node X is the node with the smallest key
greater than X.
The Predecessor of a node X is the node with the largest key
smaller than X.

11
Deleting in a Binary Search Tree
Suppose T is a Binary search tree, and suppose an ITEM of
information is given.
Case-1: N has no children. Then N is deleted from T by
simply replacing the location of N in the parent node P(N)
by the null pointer.
Case-2: N has exactly one child. Then N is deleted from T
by simply replacing the location of N in the parent node
P(N) by the location of the only child of N.
Case-3: N has two children. Let S(N) denote the inorder
successor of N. ( The reader 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 12
node N in T by the node S(N).
Deleting in a Binary Search Tree
Suppose T is a Binary search tree, and suppose an ITEM of
information is given.
Case-1: N has no children. Then N is deleted from T by
simply replacing the location of N in the parent node P(N)
by the null pointer.
Example-1: Delete key=7 Example-2: Delete key=35

13
Cont.
Case-2: N has exactly one child. Then N is deleted from T by
simply replacing the location of N in the parent node P(N) by
the location of the only child of N.

Example-1: Delete key=40 Example-2: Delete key=15

14
Cont.
Case-3: N has two children. Let S(N) denote the inorder
successor of N.Then N is deleted from T by first deleting
S(N) from T and then replacing node N in T by the node
S(N).

¥Replace with the largest key in the left subtree or the


smallest in the right subtree.
¥The largest key must be in a leaf or degree 1 node.

15
Cont.
Replace with the largest key in the left subtree or the smallest
in the right subtree.
Example-1: Delete key=10

16
Cont.
Which node is the largest key in the left subtree?

Example-2: Delete key=8

17
18

You might also like