You are on page 1of 136

# Objectives

## In this session, you will learn to:

Store data in a tree
Implement a binary tree
Implement a binary search tree

## Storing Data in a Tree

Directory structure
Consider
a scenario where you are required to represent
the directory structure of your operating system.
The directory structure contains various folders and files. A
folder may further contain any number of sub folders and
files.
In such a case, it is not possible to represent the structure
linearly because all the items have a hierarchical
relationship among themselves.
In such a case, it would be good if you have a data structure
that enables you to store your data in a nonlinear fashion.

Defining Trees
A tree are
is a used
nonlinear
data structure
that represent
Trees
in applications
in which
the relationa between
hierarchical
relationship
among
the various
elements.
data
elements
needs to be
represented
hierarchy.

A
C

J
L

Eachtopmost
The
elementnode
in a tree
in a tree
is referred
is called
to root.
as a node.
root
A
C

J
L

node

## Defining Trees (Contd.)

Each node in a tree can further have subtrees below its
hierarchy.
root
A
C

J
L

node

Tree Terminology
Leafusnode:
It refers
to aterms
nodethat
withare
nomost
children.
Let
discuss
various
frequently used
with trees.

A
C

Nodes E, F, G, H, I, J,
L, and M are leaf nodes.

J
L

## Tree Terminology (Contd.)

Subtree: A
of The
a tree,
which
can
be viewed
a are
Children
ofportion
a node:
roots
of the
subtrees
of aas
node
separate
in itself
is called
called
thetree
children
of the
node.a subtree.
A subtree can also contain just one node called the leaf node.
A
C

E, F, with
G, and
are
Tree
rootHB,
children of nodes
node B.
containing
E,BF,
is the
these of
G,
andparent
H is aof
subtree
nodes.
node
A.

J
L

## Tree Terminology (Contd.)

DegreeA of
node:
It refers
number
Edge:
the
parentto
tothe
a child
nodeofissubtrees
referred of
to aas
node
in a tree.
an
edge.

A
C

Degree of node C is 1
Degree of node D is 2
Degree of node A is 3
Degree of node B is 4

Edge
E

J
L

## Tree Terminology (Contd.)

Siblings/Brothers: It refers to the children of the same
node.

A
C

## Nodes B, C, and D are

siblings of each other.
Nodes E, F, G, and H are
siblings of each other.

J
L

## Tree Terminology (Contd.)

Internal
It refers
to to
any
between
the rootofand a
Level
of node:
a node:
It refers
thenode
distance
(in number
leaf node.
nodes)
of a node from the root. Root always lies at level 0.
As you move down the tree, the level increases by one.
Level 0

A
C

Nodes B, C, D, and K
are internal nodes. Level 1

J
L

Level 2

Level 3

## Tree Terminology (Contd.)

Depth of a tree: Refers to the total number of levels in the
tree.
The depth of the following tree is 4.
Level 0

A
C

Level 1

J
L

Level 2

Level 3

Just a minute
Consider the following tree and answer the questions that
follow:
a.
b.
c.
d.
e.
f.
g.

## What is the depth of the tree?

Which nodes are children of node B?
Which node is the parent of node F?
What is the level of node E?
Which nodes are the siblings of node H?
Which nodes are the siblings of node D?
Which nodes are leaf nodes?

root
A

D
H

C
E

F
I

Just a minute
a.
b.
c.
d.
e.
f.
g.

4
D and E
C
2
H does not have any siblings
The only sibling of D is E
F, G, H, and I

## Defining Binary Trees

Binary
is atree:
specific type of tree in which each node can
Strictly tree
binary
have
at most
two
children
namely
child
rightnodes,
child.
A binary
tree
in which
every
node, left
except
forand
the leaf
hasare
non-empty
and of
right
children.
There
variousleft
types
binary
trees:
Strictly binary tree
Full binary tree
Complete binary tree

## Defining Binary Trees (Contd.)

Full binary tree:
d

nodes.
A

Depth = 3
Total number of
3
nodes = 2 1 = 7

## Defining Binary Trees (Contd.)

Complete binary tree:
A binary tree with n nodes and depth d whose nodes
correspond to the nodes numbered from 0 to n 1 in the full
binary tree of depth k.
0

1
3

B
4

C
6

B
4

C
3

B
4

C
5

## Representing a Binary Tree

Array representation of binary trees:
All the nodes are represented as the

0
A

3
D

C
4
E

Binary Tree















## If there are n nodes in a binary

tree, then for
with index
elements
of any
an node
array.
i, where 0 < i < n 1:
Parent of i is at (i 1)/2.
Left child of i is at 2i + 1:
If 2i + 1 > n 1, then
the node does not
have a left child.
Right child of i is at 2i + 2:

Array Representation

If 2i + 2 > n 1, then
the node does have a
right child.

## Representing a Binary Tree (Contd.)

Linked representation of a binary tree:
It uses a linked list to implement a binary tree.
Each node in the linked representation holds the following
information:
Data
Reference to the left child
Reference to the right child

If a node does not have a left child or a right child or both, the
respective left or right child fields of that node point to NULL.

Data
Node

root

root

52

36

24

68

59

24

72
70

Binary Tree

80

52

.
.

36

68

.
.

59

72

70

80

## Traversing a Binary Tree

You can implement various operations on a binary tree.
A common operation on a binary tree is traversal.
Traversal refers to the process of visiting all the nodes of a
binary tree once.
There are three ways for traversing a binary tree:
Inorder traversal
Preorder traversal
Postorder traversal

Inorder Traversal
Steps for traversing a tree in inorder sequence are as
follows:
1. Traverse the left subtree
2. Visit root
3. Traverse the right subtree

## Inorder Traversal (Contd.)

B is
is not
not NULL.
NULL.
The left subtree of node A
D to traverse the left
Therefore, move to node B
B.
subtree of A.
root
A

## Inorder Traversal (Contd.)

The left subtree of node D is NULL.
Therefore, visit node D.
root
A

## Inorder Traversal (Contd.)

Right subtree of D is not NULL
Therefore, move to the right subtree of node D
root
A

## Inorder Traversal (Contd.)

Left subtree of H is empty.
Therefore, visit node H.
root
A

## Inorder Traversal (Contd.)

Right subtree of H is empty.
Therefore, move to node B.
root
A

## Inorder Traversal (Contd.)

The left subtree of B has been visited.
Therefore, visit node B.
root
A

## Inorder Traversal (Contd.)

Right subtree of B is not empty.
Therefore, move to the right subtree of B.
root
A

## Inorder Traversal (Contd.)

Left subtree of E is empty.
Therefore, visit node E.
root
A

## Inorder Traversal (Contd.)

Right subtree of E is empty.
Therefore, move to node A.
root
A

## Inorder Traversal (Contd.)

Left subtree of A has been visited.
Therefore, visit node A.
root
A

## Inorder Traversal (Contd.)

Right subtree of A is not empty.
Therefore, move to the right subtree of A.
root
A

## Inorder Traversal (Contd.)

Left subtree of C is not empty.
Therefore, move to the left subtree of C.
root
A

## Inorder Traversal (Contd.)

Left subtree of F is empty.
Therefore, visit node F.
root
A

## Inorder Traversal (Contd.)

Right subtree of F is empty.
Therefore, move to node C.
root
A

## Inorder Traversal (Contd.)

The left subtree of node C has been visited.
Therefore, visit node C.
root
A

## Inorder Traversal (Contd.)

Right subtree of C is not empty.
Therefore, move to the right subtree of node C.
root
A

## Inorder Traversal (Contd.)

Left subtree of G is not empty.
Therefore, move to the left subtree of node G.
root
A

## Inorder Traversal (Contd.)

Left subtree of I is empty.
Therefore, visit I.
root
A

## Inorder Traversal (Contd.)

Right subtree of I is empty.
Therefore, move to node G.
root
A

Visit node G.
root
A

## Inorder Traversal (Contd.)

Right subtree of G is empty.
root
A

Traversal complete

Preorder Traversal
Steps for traversing a tree in preorder sequence are as
follows:
1. Visit root
2. Traverse the left subtree
3. Traverse the right subtree

## Preorder Traversal (Contd.)

Perform the preorder traversal of the following tree.
A

Preorder Traversal:

A B D H E

C F

Postorder Traversal
Steps for traversing a tree in postorder sequence are as
follows:
1. Traverse the left subtree
2. Traverse the right subtree
3. Visit the root

## Postorder Traversal (Contd.)

Perform the postorder traversal of the following tree.
A

Postorder Traversal: H D E

B F

G C A

Just a minute
In _________ traversal method, root is processed before
traversing the left and right subtrees.

Preorder

## Implementing a Binary Search Tree

Consider a scenario. SysCall Ltd. is a cellular phone
company with millions of customers spread across the
world. Each customer is assigned a unique identification
number (id). Individual customer records can be accessed
by referring to the respective id. These ids need to be stored
in a sorted manner in such a way so that you can perform
various transactions, such as retrieval, insertion, and
deletion, easily.

## Implementing a Binary Search Tree (Contd.)

Which datayou
structure
willimplement
you use toa store
the id of the
Therefore,
need to
data structure
that
customers?
provides
the advantages of both arrays as well as linked
lists.Can you implement an array?
operation
in an array is
fast.
A binarySearch
search
tree combines
the
However,
insertion
arrays and
lists. and deletion in an array is complex in nature.
In this case, the total number of customer ids to be stored is very
large. Therefore, insertion and deletion will be very time
consuming.

## Can you implement a linked list?

Insert and delete operation in a linked is fast.
However, linked lists allow only sequential search.
If you need to access a particular customer id, which is located
near the end of the list, then it would require you to visit all the
preceding nodes, which again can be very time consuming.

## Defining a Binary Search Tree

The
following
an is
example
a binary
search
tree.
Binary
search istree
a binaryoftree
in which
every
node
satisfies the following conditions:
All values in the left subtree of a node are less than the value
52
of the node.
All values in the right subtree of a node are greater than the
value of the
36 node.
68

24

44

40

59

55

72

## Defining a Binary Search Tree (Contd.)

You can implement various operations on a binary search
tree:
Traversal
Search
Insert
Delete

## Searching a Node in a Binary Search Tree

Search
operation
refers value,
to the you
process
for a
To
search
for a specific
needoftosearching
perform the
specified steps:
value in the tree.
following
1. Make currentNode point to the root node
2. If currentNode is null:
a.
b.

Exit

## 3. Compare the value to be searched with the value of currentNode.

Depending on the result of the comparison, there can be three
possibilities:
a.

i.
ii.

b.

## If the value is less than the value of currentNode:

i.
ii.

c.

Display Found
Exit
Make currentNode point to its left child
Go to step 2

i.
ii.

## Make currentNode point to its right child

Go to step 2

Just a minute
In a binary search tree, all the values in the left subtree of a
node are _______ than the value of the node.

smaller

## Inserting Nodes in a Binary Search Tree

Beforeaninserting
a node
in a the
binary
search
you
firstto be
Write
algorithm
to locate
position
of tree,
a new
node
need to check
whether
the tree
inserted
in a binary
search
tree.is empty or not.
If the tree is empty, make the new node as root.
If the tree is not empty, you need to locate the appropriate
position for the new node to be inserted.
This requires you to locate the parent of the new node to be
inserted.
Once the parent is located, the new node is inserted as the
left child or right child of the parent.
To locate the parent of the new node to be inserted, you
need to implement a search operation in the tree.

## Inserting Nodes in a Binary Search Tree

(Contd.)
Algorithm to locate the
parent of the new node to
be inserted.
root

52

1.

2.

3.

becomes NULL

4.

5.

currentNode:
a.

36

68

6.

of currentNode:
a.

24

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Refer to the algorithm to
of the
new node to be inserted.

## Locate the position of a new node

locate the parent
55.

root

52

1.

2.

3.

becomes NULL

4.

5.

currentNode:
a.

36

68

6.

of currentNode:
a.

24

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

root
currentNode

52

1.

2.

3.

becomes NULL

4.

5.

currentNode:
a.

36

68

6.

of currentNode:
a.

24

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

root

parent = NULL
currentNode

52

1.

2.

3.

becomes NULL

4.

5.

currentNode:
a.

36

68

6.

of currentNode:
a.

24

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

root

parent = NULL
currentNode

52

1.

2.

3.

becomes NULL

4.

5.

currentNode:
a.

36

68

6.

of currentNode:
a.

24

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

root

parent
parent= NULL
currentNode

52

1.

2.

3.

becomes NULL

4.

5.

currentNode:
a.

36

68

6.

of currentNode:
a.

24

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

1.

2.

3.

becomes NULL

4.

5.

currentNode:

55 > 52
root

parent
currentNode

52

a.

36

68

6.

of currentNode:
a.

24

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

1.

2.

3.

becomes NULL

4.

5.

currentNode:

55 > 52
root

parent
currentNode

52

a.

36

68

6.

of currentNode:
a.

24

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

1.

2.

3.

becomes NULL

4.

5.

currentNode:

55 > 52
root

parent
currentNode

52

a.

currentNode

36

68

6.

of currentNode:
a.

24

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

root

parent

52

1.

2.

3.

becomes NULL

4.

5.

currentNode:
a.

currentNode

36

68

6.

of currentNode:
a.

24

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

root

parent

52

.
.

36

2.

3.

becomes NULL

4.

5.

currentNode:
a.

currentNode

parent

1.

68

6.

of currentNode:
a.

24

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

55 < 68
root

52

.
.

36

2.

3.

becomes NULL

4.

5.

currentNode:
a.

currentNode

parent

1.

68

6.

of currentNode:
a.

24

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

55 < 68
root

52

.
.

36

2.

3.

becomes NULL

4.

5.

currentNode:
a.

currentNode

parent

1.

68

6.

of currentNode:
a.

24

59

72

currentNode

70

80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

root

52

1.

2.

3.

becomes NULL

4.

5.

currentNode:
a.

parent

36

68

6.

of currentNode:
a.

24

59

72

currentNode

70

80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

root

52

1.

2.

3.

becomes NULL

4.

5.

currentNode:
a.

parent

36

68

6.

parent
24

of currentNode:
a.

59

72

currentNode

70

80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

1.

2.

3.

becomes NULL

4.

5.

currentNode:

55 < 59
root

52

a.

36

68

6.

parent
24

of currentNode:
a.

59

72

currentNode

70

80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

1.

2.

3.

becomes NULL

4.

5.

## If the value of the new node is less than that of

currentNode:

55 < 59
root

52

a.

36

68

6.

parent
24

currentNode
currentNode = NULL

of currentNode:
a.

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Locate the position of a new node
55.

root

52

1.

2.

3.

becomes NULL

4.

5.

## If the value of the new node is less than that of

currentNode:
a.

36

68

6.

parent
24

currentNode = NULL

of currentNode:
a.

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Once the parent of the new
node is located, you can
insert the node as the child
of its parent.
root

52

1.

2.

3.

becomes NULL

4.

5.

## If the value of the new node is less than that of

currentNode:
a.

36

68

6.

parent
24

currentNode = NULL

of currentNode:
a.

59

70

72

.
80

## Inserting Nodes in a Binary Search Tree

(Contd.)
Write an algorithm to insert a node in a binary search tree.

## Inserting Nodes in a Binary Search Tree

(Contd.)
1.
Allocate memory for the new node.
Insert 55 to insert a node
Algorithm
in a binary search tree.

root

.
.

52

2.

3.

point to NULL.

4.

## Locate the node which will be the parent of

the node to be inserted. Mark it as parent.

5.

a.
b.

36

68

6.

## If the value in the data field of new node is

less than that of parent:
a.

24

59

72

b.
7.

70

80

Exit

the new node
Exit

## If the value in the data field of the new node

is greater than that of the parent:
a.
b.

the new node
Exit

## Inserting Nodes in a Binary Search Tree

(Contd.)
1.
Allocate memory for the new node.

root

.
.

52

2.

3.

point to NULL.

4.

## Locate the node which will be the parent of

the node to be inserted. Mark it as parent.

5.

a.
b.

36

68

6.

## If the value in the data field of new node is

less than that of parent:
a.

24

59

72

b.
7.

70

80

Exit

the new node
Exit

## If the value in the data field of the new node

is greater than that of the parent:
a.
b.

the new node
Exit

## Inserting Nodes in a Binary Search Tree

(Contd.)
1.
Allocate memory for the new node.

root

.
.

52

2.

3.

point to NULL.

4.

## Locate the node which will be the parent of

the node to be inserted. Mark it as parent.

5.

a.
b.

36

68

6.

## If the value in the data field of new node is

less than that of parent:
a.

24

59

72

b.
7.

55

70

80

Exit

the new node
Exit

## If the value in the data field of the new node

is greater than that of the parent:
a.
b.

the new node
Exit

## Inserting Nodes in a Binary Search Tree

(Contd.)
1.
Allocate memory for the new node.

root

.
.

52

2.

3.

point to NULL.

4.

## Locate the node which will be the parent of

the node to be inserted. Mark it as parent.

5.

a.
b.

36

68

6.

## If the value in the data field of new node is

less than that of parent:
a.

24

59

72

b.
7.

55

70

80

Exit

the new node
Exit

## If the value in the data field of the new node

is greater than that of the parent:
a.
b.

the new node
Exit

## Inserting Nodes in a Binary Search Tree

(Contd.)
1.
Allocate memory for the new node.

root

.
.

52

2.

3.

point to NULL.

4.

## Locate the node which will be the parent of

the node to be inserted. Mark it as parent.

5.

a.
b.

36

68

6.

parent
24

72

b.
7.

55

## If the value in the data field of new node is

less than that of parent:
a.

59

70

80

Exit

the new node
Exit

## If the value in the data field of the new node

is greater than that of the parent:
a.
b.

the new node
Exit

## Inserting Nodes in a Binary Search Tree

(Contd.)
1.
Allocate memory for the new node.

root

.
.

52

2.

3.

point to NULL.

4.

## Locate the node which will be the parent of

the node to be inserted. Mark it as parent.

5.

a.
b.

36

68

6.

parent
24

72

b.
7.

55

## If the value in the data field of new node is

less than that of parent:
a.

59

70

80

Exit

the new node
Exit

## If the value in the data field of the new node

is greater than that of the parent:
a.
b.

the new node
Exit

## Inserting Nodes in a Binary Search Tree

(Contd.)
1.
Allocate memory for the new node.

root

.
.

52

2.

3.

point to NULL.

4.

## Locate the node which will be the parent of

the node to be inserted. Mark it as parent.

5.

a.
b.

36

68

6.

parent
24

72

b.
7.

55

## If the value in the data field of new node is

less than that of parent:
a.

59

70

80

Exit

the new node
Exit

## If the value in the data field of the new node

is greater than that of the parent:
a.
b.

the new node
Exit

## Inserting Nodes in a Binary Search Tree

(Contd.)
1.
Allocate memory for the new node.

root

.
.

52

2.

3.

point to NULL.

4.

## Locate the node which will be the parent of

the node to be inserted. Mark it as parent.

5.

a.
b.

36

68

6.

parent

24

72

b.
7.

55

## If the value in the data field of new node is

less than that of parent:
a.

59

70

80

Exit

the new node
Exit

## If the value in the data field of the new node

is greater than that of the parent:
a.
b.

the new node
Exit

## Inserting Nodes in a Binary Search Tree

(Contd.)
1.
Allocate memory for the new node.

root

.
.

52

2.

3.

point to NULL.

4.

## Locate the node which will be the parent of

the node to be inserted. Mark it as parent.

5.

a.
b.

36

68

6.

parent
24

72

b.
7.

55

## If the value in the data field of new node is

less than that of parent:
a.

59

70

80

Exit

the new node
Exit

## If the value in the data field of the new node

is greater than that of the parent:
a.
b.

the new node
Exit

## Deleting Nodes from a Binary Search Tree

Deletean
operation
a locate
binary the
search
tree refers
to theto
Write
algorithminto
position
of the node
process from
of deleting
thesearch
specified
deleted
a binary
tree.node from the tree.
Before implementing a delete operation, you first need to
locate the position of the node to be deleted and its parent.
To locate the position of the node to be deleted and its
parent, you need to implement a search operation.

(Contd.)
Suppose
Algorithmyou
to locate
want to
thedelete
node
node
to be 70
deleted.
root

.
.
24

52

.
.

36

68

69

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

59

1.

72

c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root
currentNode

.
24

52

.
.

36

68

69

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

59

1.

72

c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root
currentNode
parent = NULL

.
24

52

.
.

36

68

69

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

59

1.

72

c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root
currentNode
parent = NULL

.
24

52

.
.

36

68

69

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

59

1.

72

c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root
currentNode
parent = NULL

.
24

52

parent

36

68

69

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

59

1.

72

c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root
currentNode

.
24

52

parent

36

68

.
69

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

70 > 52

59

1.

72

i.
c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root
currentNode

.
24

52

parent

36

68

.
69

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

70 > 52

59

1.

72

i.
c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root
currentNode

52

parent
currentNode

.
24

36

68

.
69

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

70 > 52

59

1.

72

i.
c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root

52

parent
currentNode

.
24

36

68

69

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

59

1.

72

c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root

52

parent
currentNode

.
24

36

parent

68

69

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

59

1.

72

c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root

52

70 > 68

currentNode

.
24

36

parent

68

69

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

59

1.

72

c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root

52

70 > 68

currentNode

.
24

36

parent

68

69

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

59

1.

72

c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root

52

70 > 68

currentNode

36

parent

68

1.

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

currentNode
24

59

.
69

72

c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root

.
.

36

52

parent

68

1.

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

currentNode
24

59

.
69

72

c.

## If the value to be deleted is greater

than that of currentNode:
i.

70

80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root

.
.

36

52

parent

68

1.

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

currentNode
24

59

.
69

72

c.

70

## If the value to be deleted is greater

than that of currentNode:
i.

parent
80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root

.
.

52

70 < 72

36

68

1.

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

currentNode
24

59

.
69

72

c.

70

## If the value to be deleted is greater

than that of currentNode:
i.

parent
80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root

.
.

52

70 < 72

36

68

1.

the ROOT node.

2.

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

i.

currentNode
24

59

72

c.

70

## If the value to be deleted is greater

than that of currentNode:
i.

parent

currentNode
69

.
80

left child.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Suppose you want to delete
node 70
root

.
.

52

.
.

36

24

68

72

3.

## Repeat steps a, b, and c until currentNode

becomes NULL or the value of the node to be
searched becomes equal to that of
currentNode:
a.

b.

## If the value to be deleted is less than

that of currentNode:

c.

70

80

left child.

## If the value to be deleted is greater

than that of currentNode:
i.

parent

currentNode
69

2.

i.

.
.

the ROOT node.

59

Nodes located

1.

right child.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Once the nodes are located, there can be three cases:
Case I: Node to be deleted is the leaf node
Case II: Node to be deleted has one child (left or right)
Case III: Node to be deleted has two children

## Deleting Nodes from a Binary Search Tree

(Contd.)
Write an algorithm to delete a leaf node from a binary
search tree.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Delete node
Algorithm
to delete
69
a leaf
node from the binary tree.

52

1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

// NULL
a.
b.

.
3.

36

68

a.
b.

24

59

72

4.

70

b.

80
5.

69

NULL.
Go to step 5.

a.

Go to step 5.

NULL.
Go to step 5.

(Contd.)
Delete node 69

52

1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

// NULL
a.
b.

.
3.

36

68

a.
b.

24

59

72

4.

70

b.

80
5.

69
currentNode

parent

NULL.
Go to step 5.

a.

Go to step 5.

NULL.
Go to step 5.

(Contd.)
Delete node 69

52

1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

// NULL
a.
b.

.
3.

36

68

a.
b.

24

59

72

4.

70

b.

80
5.

69
currentNode

parent

NULL.
Go to step 5.

a.

Go to step 5.

NULL.
Go to step 5.

(Contd.)
Delete node 69

52

1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

// NULL
a.
b.

.
3.

36

68

a.
b.

24

59

72

4.

70

b.

80
5.

69
currentNode

parent

NULL.
Go to step 5.

a.

Go to step 5.

NULL.
Go to step 5.

(Contd.)
Delete node 69

52

1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

// NULL
a.
b.

.
3.

36

68

a.
b.

24

59

72

4.

70

b.

80
5.

69
currentNode

parent

NULL.
Go to step 5.

a.

Go to step 5.

NULL.
Go to step 5.

(Contd.)
Delete node 69

52

1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

// NULL
a.
b.

.
3.

36

68

a.
b.

24

59

72

4.

70

b.

80
5.

69
currentNode

parent

NULL.
Go to step 5.

a.

Go to step 5.

NULL.
Go to step 5.

(Contd.)
Delete node 69

52

1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

// NULL
a.
b.

.
3.

36

68

a.
b.

24

59

72

4.

70

b.

80
5.

69

parent

currentNode

NULL.
Go to step 5.

a.

Go to step 5.

NULL.
Go to step 5.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Write an algorithm to delete a node, which has one child
from a binary search tree.

(Contd.)
Algorithm
to delete
a node
Delete
node
80
with one child.

1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## If currentNode has a left child:

a.
Mark the left child of currentNode as
child.
b.
Go to step 4.

3.

## If currentNode has a right child:

a.
Mark the right child of currentNode as
child.
b.
Go to step 4.

4.

## If currentNode is the root node:

a.
Mark child as root.
b.
Go to step 7.

5.

## If currentNode is the left child of parent:

a.
Make left child field of parent point to
child.
b.
Go to step 7.

6.

## If currentNode is the right child of parent:

a.
Make right child field of parent point to
child.
b.
Go to step 7.

7.

root

.
.
24

52

.
.

36

68

.
.

59

72

.
.

70

80

75

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## If currentNode has a left child:

a.
Mark the left child of currentNode as
child.
b.
Go to step 4.

3.

## If currentNode has a right child:

a.
Mark the right child of currentNode as
child.
b.
Go to step 4.

4.

## If currentNode is the root node:

a.
Mark child as root.
b.
Go to step 7.

Delete node 80
root

.
.

52

.
.

36

68

.
parent

24

59

72

.
currentNode 5.

70

80

## If currentNode is the left child of parent:

a.
Make left child field of parent point to
child.
b.
Go to step 7.

6.

## If currentNode is the right child of parent:

a.
Make right child field of parent point to
child.
b.
Go to step 7.

7.

75

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## If currentNode has a left child:

a.
Mark the left child of currentNode as
child.
b.
Go to step 4.

3.

## If currentNode has a right child:

a.
Mark the right child of currentNode as
child.
b.
Go to step 4.

4.

## If currentNode is the root node:

a.
Mark child as root.
b.
Go to step 7.

Delete node 80
root

.
.

52

.
.

36

68

.
parent

24

59

72

.
currentNode 5.

70

80

## If currentNode is the left child of parent:

a.
Make left child field of parent point to
child.
b.
Go to step 7.

6.

## If currentNode is the right child of parent:

a.
Make right child field of parent point to
child.
b.
Go to step 7.

7.

75

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## If currentNode has a left child:

a.
Mark the left child of currentNode as
child.
b.
Go to step 4.

3.

## If currentNode has a right child:

a.
Mark the right child of currentNode as
child.
b.
Go to step 4.

4.

## If currentNode is the root node:

a.
Mark child as root.
b.
Go to step 7.

Delete node 80
root

.
.

52

.
.

36

68

.
parent

24

59

72

.
currentNode 5.

70

80
6.

## If currentNode is the right child of parent:

a.
Make right child field of parent point to
child.
b.
Go to step 7.

7.

75
child

## If currentNode is the left child of parent:

a.
Make left child field of parent point to
child.
b.
Go to step 7.

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## If currentNode has a left child:

a.
Mark the left child of currentNode as
child.
b.
Go to step 4.

3.

## If currentNode has a right child:

a.
Mark the right child of currentNode as
child.
b.
Go to step 4.

4.

## If currentNode is the root node:

a.
Mark child as root.
b.
Go to step 7.

Delete node 80
root

.
.

52

.
.

36

68

.
parent

24

59

72

.
currentNode 5.

70

80
6.

## If currentNode is the right child of parent:

a.
Make right child field of parent point to
child.
b.
Go to step 7.

7.

75
child

## If currentNode is the left child of parent:

a.
Make left child field of parent point to
child.
b.
Go to step 7.

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## If currentNode has a left child:

a.
Mark the left child of currentNode as
child.
b.
Go to step 4.

3.

## If currentNode has a right child:

a.
Mark the right child of currentNode as
child.
b.
Go to step 4.

4.

## If currentNode is the root node:

a.
Mark child as root.
b.
Go to step 7.

Delete node 80
root

.
.

52

.
.

36

68

.
parent

24

59

72

.
currentNode 5.

70

80
6.

## If currentNode is the right child of parent:

a.
Make right child field of parent point to
child.
b.
Go to step 7.

7.

75
child

## If currentNode is the left child of parent:

a.
Make left child field of parent point to
child.
b.
Go to step 7.

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## If currentNode has a left child:

a.
Mark the left child of currentNode as
child.
b.
Go to step 4.

3.

## If currentNode has a right child:

a.
Mark the right child of currentNode as
child.
b.
Go to step 4.

4.

## If currentNode is the root node:

a.
Mark child as root.
b.
Go to step 7.

Delete node 80
root

.
.

52

.
.

36

68

.
parent

24

59

72

.
currentNode 5.

70

80
6.

## If currentNode is the right child of parent:

a.
Make right child field of parent point to
child.
b.
Go to step 7.

7.

75
child

## If currentNode is the left child of parent:

a.
Make left child field of parent point to
child.
b.
Go to step 7.

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## If currentNode has a left child:

a.
Mark the left child of currentNode as
child.
b.
Go to step 4.

3.

## If currentNode has a right child:

a.
Mark the right child of currentNode as
child.
b.
Go to step 4.

4.

## If currentNode is the root node:

a.
Mark child as root.
b.
Go to step 7.

Delete node 80
root

.
.

52

.
.

36

68

.
parent

24

59

72

.
currentNode 5.

70

80
6.

## If currentNode is the right child of parent:

a.
Make right child field of parent point to
child.
b.
Go to step 7.

7.

75
child

## If currentNode is the left child of parent:

a.
Make left child field of parent point to
child.
b.
Go to step 7.

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## If currentNode has a left child:

a.
Mark the left child of currentNode as
child.
b.
Go to step 4.

3.

## If currentNode has a right child:

a.
Mark the right child of currentNode as
child.
b.
Go to step 4.

4.

## If currentNode is the root node:

a.
Mark child as root.
b.
Go to step 7.

Delete node 80
root

.
.

52

.
.

36

68

.
parent

24

59

72

.
currentNode 5.

70

80
6.

## If currentNode is the right child of parent:

a.
Make right child field of parent point to
child.
b.
Go to step 7.

7.

75
child

## If currentNode is the left child of parent:

a.
Make left child field of parent point to
child.
b.
Go to step 7.

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## If currentNode has a left child:

a.
Mark the left child of currentNode as
child.
b.
Go to step 4.

3.

## If currentNode has a right child:

a.
Mark the right child of currentNode as
child.
b.
Go to step 4.

4.

## If currentNode is the root node:

a.
Mark child as root.
b.
Go to step 7.

Delete node 80
root

.
.

52

.
.

36

68

.
parent

24

59

72

.
currentNode 5.

70

80
6.

## If currentNode is the right child of parent:

a.
Make right child field of parent point to
child.
b.
Go to step 7.

7.

75
child

## If currentNode is the left child of parent:

a.
Make left child field of parent point to
child.
b.
Go to step 7.

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## If currentNode has a left child:

a.
Mark the left child of currentNode as
child.
b.
Go to step 4.

3.

## If currentNode has a right child:

a.
Mark the right child of currentNode as
child.
b.
Go to step 4.

4.

## If currentNode is the root node:

a.
Mark child as root.
b.
Go to step 7.

Delete node 80
root

.
.

52

.
.

36

68

.
parent

24

59

72

.
currentNode 5.

70

80
6.

## If currentNode is the right child of parent:

a.
Make right child field of parent point to
child.
b.
Go to step 7.

7.

75

child

## If currentNode is the left child of parent:

a.
Make left child field of parent point to
child.
b.
Go to step 7.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Write an algorithm to delete a node, which has two children
from a binary search tree.

## Deleting Nodes from a Binary Search Tree

(Contd.)
Delete node
72
Algorithm
to delete
a node
with two children.

1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## Locate the inorder successor of currentNode.

Mark it as Inorder_suc. Execute the following
steps to locate Inorder_suc:
a.
Mark the right child of currentNode as
Inorder_suc.
b.
Repeat until the left child of
Inorder_suc becomes NULL:
i.
Make Inorder_suc point to its
left child.

3.

## Replace the information held by currentNode

with that of Inorder_suc.

4.

## If the node marked Inorder_suc is a leaf

node:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case I.

5.

## If the node marked Inorder_suc has one

child:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case II.

root

.
.
24

52

.
.

36

68

.
.

59

72

.
.

70

75

80

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## Locate the inorder successor of currentNode.

Mark it as Inorder_suc. Execute the following
steps to locate Inorder_suc:
a.
Mark the right child of currentNode as
Inorder_suc.
b.
Repeat until the left child of
Inorder_suc becomes NULL:
i.
Make Inorder_suc point to its
left child.

3.

## Replace the information held by currentNode

with that of Inorder_suc.

4.

## If the node marked Inorder_suc is a leaf

node:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case I.

5.

## If the node marked Inorder_suc has one

child:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case II.

Delete node 72
root

52

.
parent

36

68

.
currentNode

24

59

72

.
.

70

75

80

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## Locate the inorder successor of currentNode.

Mark it as Inorder_suc. Execute the following
steps to locate Inorder_suc:
a.
Mark the right child of currentNode as
Inorder_suc.
b.
Repeat until the left child of
Inorder_suc becomes NULL:
i.
Make Inorder_suc point to its
left child.

3.

## Replace the information held by currentNode

with that of Inorder_suc.

4.

## If the node marked Inorder_suc is a leaf

node:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case I.

5.

## If the node marked Inorder_suc has one

child:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case II.

Delete node 72
root

52

.
parent

36

68

.
currentNode

24

59

72

.
.

70

75

80

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## Locate the inorder successor of currentNode.

Mark it as Inorder_suc. Execute the following
steps to locate Inorder_suc:
a.
Mark the right child of currentNode as
Inorder_suc.
b.
Repeat until the left child of
Inorder_suc becomes NULL:
i.
Make Inorder_suc point to its
left child.

3.

## Replace the information held by currentNode

with that of Inorder_suc.

4.

## If the node marked Inorder_suc is a leaf

node:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case I.

5.

## If the node marked Inorder_suc has one

child:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case II.

Delete node 72
root

52

.
parent

36

68

.
currentNode

24

59

72

.
.

70

80

Inorder_suc
75

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## Locate the inorder successor of currentNode.

Mark it as Inorder_suc. Execute the following
steps to locate Inorder_suc:
a.
Mark the right child of currentNode as
Inorder_suc.
b.
Repeat until the left child of
Inorder_suc becomes NULL:
i.
Make Inorder_suc point to its
left child.

3.

## Replace the information held by currentNode

with that of Inorder_suc.

4.

## If the node marked Inorder_suc is a leaf

node:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case I.

5.

## If the node marked Inorder_suc has one

child:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case II.

Delete node 72
root

52

.
parent

36

68

.
currentNode

24

59

72

.
.

70

80

Inorder_suc
75

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## Locate the inorder successor of currentNode.

Mark it as Inorder_suc. Execute the following
steps to locate Inorder_suc:
a.
Mark the right child of currentNode as
Inorder_suc.
b.
Repeat until the left child of
Inorder_suc becomes NULL:
i.
Make Inorder_suc point to its
left child.

3.

## Replace the information held by currentNode

with that of Inorder_suc.

4.

## If the node marked Inorder_suc is a leaf

node:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case I.

5.

## If the node marked Inorder_suc has one

child:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case II.

Delete node 72
root

52

.
parent

36

68

.
currentNode

24

59

72

.
.

70

80

Inorder_suc
75
Inorder_suc

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## Locate the inorder successor of currentNode.

Mark it as Inorder_suc. Execute the following
steps to locate Inorder_suc:
a.
Mark the right child of currentNode as
Inorder_suc.
b.
Repeat until the left child of
Inorder_suc becomes NULL:
i.
Make Inorder_suc point to its
left child.

3.

## Replace the information held by currentNode

with that of Inorder_suc.

4.

## If the node marked Inorder_suc is a leaf

node:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case I.

5.

## If the node marked Inorder_suc has one

child:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case II.

Delete node 72
root

52

.
parent

36

68

.
currentNode

24

59

72
75

.
.

70

80

75
Inorder_suc

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## Locate the inorder successor of currentNode.

Mark it as Inorder_suc. Execute the following
steps to locate Inorder_suc:
a.
Mark the right child of currentNode as
Inorder_suc.
b.
Repeat until the left child of
Inorder_suc becomes NULL:
i.
Make Inorder_suc point to its
left child.

3.

## Replace the information held by currentNode

with that of Inorder_suc.

4.

## If the node marked Inorder_suc is a leaf

node:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case I.

5.

## If the node marked Inorder_suc has one

child:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case II.

Delete node 72
root

52

.
parent

36

68

.
currentNode

24

59

75

.
.

70

80

75
Inorder_suc

(Contd.)
1.

## Locate the node to be deleted. Mark it as

currentNode and its parent as parent.

2.

## Locate the inorder successor of currentNode.

Mark it as Inorder_suc. Execute the following
steps to locate Inorder_suc:
a.
Mark the right child of currentNode as
Inorder_suc.
b.
Repeat until the left child of
Inorder_suc becomes NULL:
i.
Make Inorder_suc point to its
left child.

3.

## Replace the information held by currentNode

with that of Inorder_suc.

4.

## If the node marked Inorder_suc is a leaf

node:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case I.

5.

## If the node marked Inorder_suc has one

child:
a.
Delete the node marked Inorder_suc
by using the algorithm for Case II.

root

52

.
parent

36

68

.
currentNode

24

59

75

.
.

70

80

75

Inorder_suc

## Activity: Implementing a Binary Search Tree

Problem Statement:
Write a program to implement insert and traverse operations
on a binary search tree that contains the words in a dictionary.

Summary
In this session, you learned that:
A tree is a nonlinear data structure that represents a
hierarchical relationship among the various data elements.
A binary tree is a specific type of tree in which each node can
have a maximum of two children.
Binary trees can be implemented by using arrays as well as
linked lists, depending upon requirement.
Traversal of a tree is the process of visiting all the nodes of the
tree once. There are three types of traversals, namely inorder,
preorder, and postorder traversal.
Binary search tree is a binary tree in which the value of the left
child of a node is always less than the value of the node, and
the value of the right child of a node is greater than the value of
the node.

Summary (Contd.)
Inserting a node in a binary search tree requires you to first
locate the appropriate position for the node to be inserted.
You need to check for the following three cases before deleting
a node from a binary search tree.
If the node to be deleted is the leaf node
If the node to be deleted has one child (left or right)
If the node to be deleted has two children