0% found this document useful (0 votes)
88 views

Data Structures

The document discusses binary search trees. It defines a binary search tree as a tree where the value at each node is greater than all values in its left subtree and less than all values in its right subtree. It provides examples and algorithms for searching, inserting, and deleting nodes from a binary search tree. Searching and inserting have average-case complexities of O(log n), while deleting covers three cases depending on the number of child nodes of the node being deleted.

Uploaded by

api-3829165
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Data Structures

The document discusses binary search trees. It defines a binary search tree as a tree where the value at each node is greater than all values in its left subtree and less than all values in its right subtree. It provides examples and algorithms for searching, inserting, and deleting nodes from a binary search tree. Searching and inserting have average-case complexities of O(log n), while deleting covers three cases depending on the number of child nodes of the node being deleted.

Uploaded by

api-3829165
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 14

Data Structures

Binary Search Tree


Binary Search Tree
 Introduction
 Searching and Insertion
 Deletion
Introduction
Definition :

The value at a node is greater than every value in the left


subtree of the node and is less than every value in the
right subtree of the node.

Node
Value at node > Value of left subtree

Value at node < Value of right subtree


Left Right
Example of a Binary Tree :

38

16 59

9 25 45
83

18 75
Searching and Insertion
Value to be searched = 20

38

14
56

8 23
45 82

18

70
20
Algorithm for searching an element
FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR)
2. [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 PAR=NULL
and Return
8. [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]
14.Repeat Steps 5 and 6 while PTR ╪ NULL
15. [ITEM Found?]
If ITEM=INFO[PTR], then Set LOC=PTR and
PAR=SAVE, and Return.

Contd…
Algorithm for searching an element

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.

Complexity of Searching Algorithm

Average running time f(n) = O(log2n)


Algorithm for insertion of an element

INSBST(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR)
2. Call FIND(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR)
3. If LOC╪NULL, then EXIT
4. [COPY ITEM into new node in AVAIL list.]
(A) 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.
8. [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.
Deletion of element
 Root node N has no child nodes
 N has exactly one child node either in left or in the
right
 N has child nodes on both the sides
Case 1 : Root Node N has no child nodes

CASEA(INFO,LEFT,RIGHT,ROOT,LOC,PAR)
2. [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]
10.If PAR╪NULL, then:
If LOC=LEFT[PAR], then:
set LEFT[PAR]=CHILD.
Else:
set RIGHT[PAR]=CHILD.
[End of IF structure]
3. RETURN.
Case 2 : Root Node N has one child node

CASEB(INFO,LEFT,RIGHT,ROOT,LOC,PAR)
2. [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.
8. [Delete inorder successor, using case 1]
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]

Contd…
Case 2 : Root Node N has one child node

Else:
set ROOT=SUC
[End of IF structure.]
(b) set LEFT[SUC]=LEFT[LOC] and
RIGHT[SUC]=RIGHT[LOC].
4. RETURN.
Case 3 : Root Node N has two child nodes

DEL(INFO,LEFT,RIGHT,ROOT,AVAIL,ITEM)
2. [Find the location of ITEM and its parent ]
Call FIND(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR)
4. [ITEM in TREE?]
If LOC=NULL, then Write: ITEM not in TREE and EXIT.
6. [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.]
12.[Return deleted node to AVAIL list]
set LEFT[LOC]=AVAIL AND AVAIL=LOC.
5. EXIT.

You might also like