136
Data Structures and Algorithms Session 13 Ver. 1.0 Objectives In this session, you will learn to: Store data in a tree Implement a binary tree Implement a binary search tree

Data Structures and Algorithms Session 13 Ver. 1.0 Objectives In this session, you will learn to: Store data in a tree Implement a binary tree Implement

Embed Size (px)

Citation preview

Data Structures and Algorithms

Session 13Ver. 1.0

Objectives

In this session, you will learn to:Store data in a tree

Implement a binary tree

Implement a binary search tree

Data Structures and Algorithms

Session 13Ver. 1.0

Storing Data in a Tree

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.

Directory structure

Data Structures and Algorithms

Session 13Ver. 1.0

A tree is a nonlinear data structure that represent a hierarchical relationship among the various data elements.

A

B C D

I JH KG

L M

FE

Defining Trees

Trees are used in applications in which the relation between data elements needs to be represented in a hierarchy.

Data Structures and Algorithms

Session 13Ver. 1.0

Each element in a tree is referred to as a node.The topmost node in a tree is called root.

root

Defining Trees (Contd.)

node

A

B C D

I JH KG

L M

FE

Data Structures and Algorithms

Session 13Ver. 1.0

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

root

Defining Trees (Contd.)

node

A

B C D

I JH KG

L M

FE

Data Structures and Algorithms

Session 13Ver. 1.0

Tree Terminology

Leaf node: It refers to a node with no children.

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

A

B C D

I JH KG

L M

FE

Let us discuss various terms that are most frequently used with trees.

Data Structures and Algorithms

Session 13Ver. 1.0

Subtree: A portion of a tree, which can be viewed as a separate tree in itself is called a subtree.

A subtree can also contain just one node called the leaf node.

Children of a node: The roots of the subtrees of a node are called the children of the node.

Tree Terminology (Contd.)

Tree with root B, containing nodes E, F, G, and H is a subtree of node A.

E, F, G, and H are children of node B. B is the parent of these nodes.

A

B C D

I JH KG

L M

FE

Data Structures and Algorithms

Session 13Ver. 1.0

Degree of a node: It refers to the number of subtrees of a node in a tree.

Tree Terminology (Contd.)

Degree of node C is 1

Degree of node D is 2

Degree of node A is 3

Degree of node B is 4

A

B C D

I JH KG

L M

FE

Edge: A link from the parent to a child node is referred to as an edge.

Edge

Data Structures and Algorithms

Session 13Ver. 1.0

Nodes B, C, and D are siblings of each other.

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

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

Tree Terminology (Contd.)

A

B C D

I JH KG

L M

FE

Data Structures and Algorithms

Session 13Ver. 1.0

Level of a node: It refers to the distance (in number of nodes) of a node from the root. Root always lies at level 0.

As you move down the tree, the level increases by one.

Tree Terminology (Contd.)

Internal node: It refers to any node between the root and a leaf node.

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

Level 0

Level 1

Level 2

Level 3

A

B C D

I JH KG

L M

FE

Data Structures and Algorithms

Session 13Ver. 1.0

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

Level 1

Level 2

Level 3

A

B C D

I JH KG

L M

FE

Data Structures and Algorithms

Session 13Ver. 1.0

Consider the following tree and answer the questions that follow:a. What is the depth of the tree?

b. Which nodes are children of node B?

c. Which node is the parent of node F?

d. What is the level of node E?

e. Which nodes are the siblings of node H?

f. Which nodes are the siblings of node D?

g. Which nodes are leaf nodes?

Just a minute

A

B

F G

C

ED

H I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Just a minute

Answer:a. 4

b. D and E

c. C

d. 2

e. H does not have any siblings

f. The only sibling of D is E

g. F, G, H, and I

Data Structures and Algorithms

Session 13Ver. 1.0

Strictly binary tree:A binary tree in which every node, except for the leaf nodes, has non-empty left and right children.

Defining Binary Trees

Binary tree is a specific type of tree in which each node can have at most two children namely left child and right child.

There are various types of binary trees:Strictly binary tree

Full binary tree

Complete binary tree

Data Structures and Algorithms

Session 13Ver. 1.0

Full binary tree:

A binary tree of depth d that contains exactly 2d– 1

nodes.

A

B C

D E F G

Depth = 3

Total number of nodes = 2

3– 1 = 7

Defining Binary Trees (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

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.

A

B C

D E F

Complete Binary Tree

A

B C

D E G

Incomplete Binary Tree

Defining Binary Trees (Contd.)

A

B C

D E F

Full Binary Tree

G

0

1 2

6543

0

1 2

3 4 5

0

1 2

3 4 5

Data Structures and Algorithms

Session 13Ver. 1.0

Representing a Binary Tree

Array representation of binary trees:All the nodes are represented as the elements of an array.

A

B C

D E F G

A

B

C

D

E

G

F

[0]

[1]

[2]

[3]

[4]

[5]

[6]

0

1 2

3 4 5 6

Binary Tree Array Representation

If there are n nodes in a binary tree, then for any node with index 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:

If 2i + 2 > n – 1, then the node does have a

right child.

Data Structures and Algorithms

Session 13Ver. 1.0

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

Representing a Binary Tree (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

52

68

59 7224

8070

36

.

.

..

..

..

52

36 68

24 59 72

70 80

Binary Tree Linked Representation

root root

Representing a Binary Tree (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

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

Traversing a Binary Tree

Data Structures and Algorithms

Session 13Ver. 1.0

Steps for traversing a tree in inorder sequence are as follows:

1. Traverse the left subtree

2. Visit root

3. Traverse the right subtree

Let us consider an example.

Inorder Traversal

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

A

C

E

B

F GD

H

I

root

The left subtree of node B is not NULL.

Therefore, move to node D to traverse the left subtree of B.

The left subtree of node A is not NULL.

Therefore, move to node B to traverse the left subtree of A.

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

The left subtree of node D is NULL.

Therefore, visit node D.

D

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Right subtree of D is not NULL

Therefore, move to the right subtree of node D

D

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Left subtree of H is empty.

Therefore, visit node H.

D H

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Right subtree of H is empty.

Therefore, move to node B.

D H

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

The left subtree of B has been visited.

Therefore, visit node B.

D H B

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Right subtree of B is not empty.

Therefore, move to the right subtree of B.

D H B

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Left subtree of E is empty.

Therefore, visit node E.

D H EB

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Right subtree of E is empty.

Therefore, move to node A.

D H EB

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Left subtree of A has been visited.

Therefore, visit node A.

D H EB A

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Right subtree of A is not empty.

Therefore, move to the right subtree of A.

D H EB A

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Left subtree of C is not empty.

Therefore, move to the left subtree of C.

D H EB A

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Left subtree of F is empty.

Therefore, visit node F.

D H EB A F

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Right subtree of F is empty.

Therefore, move to node C.

D H EB A F

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

The left subtree of node C has been visited.

Therefore, visit node C.

D H EB A F C

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Right subtree of C is not empty.

Therefore, move to the right subtree of node C.

D H EB A F C

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Left subtree of G is not empty.

Therefore, move to the left subtree of node G.

D H EB A F C

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Left subtree of I is empty.

Therefore, visit I.

D H EB A F C I

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Right subtree of I is empty.

Therefore, move to node G.

D H EB A F C I

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Visit node G.

D H EB A F C I G

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder Traversal (Contd.)

Right subtree of G is empty.

D H EB A F C I G

Traversal complete

A

C

E

B

F GD

H

I

root

Data Structures and Algorithms

Session 13Ver. 1.0

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

Data Structures and Algorithms

Session 13Ver. 1.0

Preorder Traversal (Contd.)

A

C

E

B

F GD

H

IPerform the preorder traversal of the following tree.

A B D H E C F G IPreorder Traversal:

Data Structures and Algorithms

Session 13Ver. 1.0

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

Data Structures and Algorithms

Session 13Ver. 1.0

Postorder Traversal (Contd.)

A

C

E

B

F GD

H

IPerform the postorder traversal of the following tree.

H D E B F I G C APostorder Traversal:

Data Structures and Algorithms

Session 13Ver. 1.0

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

Just a minute

Answer:Preorder

Data Structures and Algorithms

Session 13Ver. 1.0

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.

Data Structures and Algorithms

Session 13Ver. 1.0

Which data structure will you use to store the id of the customers?

Can you implement an array?Search operation in an array is fast.

However, insertion 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.

Therefore, you need to implement a data structure that provides the advantages of both arrays as well as linked lists.

A binary search tree combines the advantages of both arrays and linked lists.

Implementing a Binary Search Tree (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

Binary search tree is a binary tree in which every node satisfies the following conditions:

All values in the left subtree of a node are less than the value of the node.

All values in the right subtree of a node are greater than the value of the node.

The following is an example of a binary search tree.

52

72

68

24

36

44

40

59

55

Defining a Binary Search Tree

Data Structures and Algorithms

Session 13Ver. 1.0

You can implement various operations on a binary search tree:

Traversal

Search

Insert

Delete

Defining a Binary Search Tree (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

Searching a Node in a Binary Search Tree

Search operation refers to the process of searching for a specified value in the tree.To search for a specific value, you need to perform the following steps:1. Make currentNode point to the root node

2. If currentNode is null:a. Display “Not Found”

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. If the value is equal to the value of currentNode:

i. Display “Found”

ii. Exit

b. If the value is less than the value of currentNode: i. Make currentNode point to its left child

ii. Go to step 2

c. If the value is greater than the value of currentNode: i. Make currentNode point to its right child

ii. Go to step 2

Data Structures and Algorithms

Session 13Ver. 1.0

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

Just a minute

Answer:smaller

Data Structures and Algorithms

Session 13Ver. 1.0

Inserting Nodes in a Binary Search Tree

Before inserting a node in a binary search tree, you first need to check whether the 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.

Write an algorithm to locate the position of a new node to be inserted in a binary search tree.

Data Structures and Algorithms

Session 13Ver. 1.0

Algorithm to locate the parent of the new node to be inserted.

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

Inserting Nodes in a Binary Search Tree (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

Refer to the algorithm to locate the parent of the new node to be inserted.

52

68

59 7224

8070

36

.

.

..

.

..

.

root

Locate the position of a new node 55.

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

Inserting Nodes in a Binary Search Tree (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

52

68

59 7224

8070

36

.

.

..

.

..

.

root

currentNode

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode

parent = NULL

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

Data Structures and Algorithms

Session 13Ver. 1.0

parent = NULL

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

currentNode

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode

parent = NULLparent

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode

parent

55 > 52

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode

parent

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

55 > 52

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode

parent

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

currentNode

55 > 52

Data Structures and Algorithms

Session 13Ver. 1.0

parent

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

currentNode

Data Structures and Algorithms

Session 13Ver. 1.0

parent

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

currentNodeparent

Data Structures and Algorithms

Session 13Ver. 1.0

55 < 68

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

currentNodeparent

Data Structures and Algorithms

Session 13Ver. 1.0

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

currentNodeparent

currentNode

55 < 68

Data Structures and Algorithms

Session 13Ver. 1.0

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

parent

currentNode

Data Structures and Algorithms

Session 13Ver. 1.0

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

parent

currentNode

parent

Data Structures and Algorithms

Session 13Ver. 1.0

55 < 59

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

currentNode

parent

Data Structures and Algorithms

Session 13Ver. 1.0

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

currentNode

parent

currentNode = NULL

55 < 59

Data Structures and Algorithms

Session 13Ver. 1.0

Locate the position of a new node 55.

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36

.

.

..

.

..

.

root

parent

currentNode = NULL

Data Structures and Algorithms

Session 13Ver. 1.0

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

Inserting Nodes in a Binary Search Tree (Contd.)

1. Mark the root node as currentNode

2. Make parent point to NULL

3. Repeat steps 4, 5, and 6 until currentNode becomes NULL

4. Make parent point to currentNode

5. If the value of the new node is less than that of currentNode:

a. Make currentNode point to its left child

6. If the value of the new node is greater than that of currentNode:

a. Make currentNode point to its right child

52

68

59 7224

8070

36.

..

.

..

.

root

parent

currentNode = NULL .

Data Structures and Algorithms

Session 13Ver. 1.0

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

Inserting Nodes in a Binary Search Tree (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

Inserting Nodes in a Binary Search Tree (Contd.)

Algorithm to insert a node in a binary search tree.

1. Allocate memory for the new node.

2. Assign value to the data field of new node.

3. Make the left and right child of the new node point to NULL.

4. Locate the node which will be the parent of the node to be inserted. Mark it as parent.

5. If parent is NULL (Tree is empty):

a. Mark the new node as ROOTb. Exit

6. If the value in the data field of new node is less than that of parent:

a. Make the left child of parent point to the new node

b. Exit

7. If the value in the data field of the new node is greater than that of the parent:

a. Make the right child of parent point to the new node

b. Exit

52

68

59 7224

8070

36

.

.

..

.

..

.

root

Insert 55

Data Structures and Algorithms

Session 13Ver. 1.0

1. Allocate memory for the new node.

2. Assign value to the data field of new node.

3. Make the left and right child of the new node point to NULL.

4. Locate the node which will be the parent of the node to be inserted. Mark it as parent.

5. If parent is NULL (Tree is empty):

a. Mark the new node as ROOTb. Exit

6. If the value in the data field of new node is less than that of parent:

a. Make the left child of parent point to the new node

b. Exit

7. If the value in the data field of the new node is greater than that of the parent:

a. Make the right child of parent point to the new node

b. Exit

Inserting Nodes in a Binary Search Tree (Contd.)

52

68

59 7224

8070

36

.

.

..

.

..

.

root

Data Structures and Algorithms

Session 13Ver. 1.0

55

Inserting Nodes in a Binary Search Tree (Contd.) 1. Allocate memory for the new node.

2. Assign value to the data field of new node.

3. Make the left and right child of the new node point to NULL.

4. Locate the node which will be the parent of the node to be inserted. Mark it as parent.

5. If parent is NULL (Tree is empty):

a. Mark the new node as ROOTb. Exit

6. If the value in the data field of new node is less than that of parent:

a. Make the left child of parent point to the new node

b. Exit

7. If the value in the data field of the new node is greater than that of the parent:

a. Make the right child of parent point to the new node

b. Exit

52

68

59 7224

8070

36

.

.

..

.

..

.

root

Data Structures and Algorithms

Session 13Ver. 1.0

Inserting Nodes in a Binary Search Tree (Contd.) 1. Allocate memory for the new node.

2. Assign value to the data field of new node.

3. Make the left and right child of the new node point to NULL.

4. Locate the node which will be the parent of the node to be inserted. Mark it as parent.

5. If parent is NULL (Tree is empty):

a. Mark the new node as ROOTb. Exit

6. If the value in the data field of new node is less than that of parent:

a. Make the left child of parent point to the new node

b. Exit

7. If the value in the data field of the new node is greater than that of the parent:

a. Make the right child of parent point to the new node

b. Exit

52

68

59 7224

8070

36

.

.

..

.

..

.

root

55

Data Structures and Algorithms

Session 13Ver. 1.0

Inserting Nodes in a Binary Search Tree (Contd.) 1. Allocate memory for the new node.

2. Assign value to the data field of new node.

3. Make the left and right child of the new node point to NULL.

4. Locate the node which will be the parent of the node to be inserted. Mark it as parent.

5. If parent is NULL (Tree is empty):

a. Mark the new node as ROOTb. Exit

6. If the value in the data field of new node is less than that of parent:

a. Make the left child of parent point to the new node

b. Exit

7. If the value in the data field of the new node is greater than that of the parent:

a. Make the right child of parent point to the new node

b. Exit

55

52

68

59 7224

8070

36

.

.

..

.

..

.

root

parent

Data Structures and Algorithms

Session 13Ver. 1.0

Inserting Nodes in a Binary Search Tree (Contd.) 1. Allocate memory for the new node.

2. Assign value to the data field of new node.

3. Make the left and right child of the new node point to NULL.

4. Locate the node which will be the parent of the node to be inserted. Mark it as parent.

5. If parent is NULL (Tree is empty):

a. Mark the new node as ROOTb. Exit

6. If the value in the data field of new node is less than that of parent:

a. Make the left child of parent point to the new node

b. Exit

7. If the value in the data field of the new node is greater than that of the parent:

a. Make the right child of parent point to the new node

b. Exit

55

52

68

59 7224

8070

36

.

.

..

.

..

.

root

parent

Data Structures and Algorithms

Session 13Ver. 1.0

Inserting Nodes in a Binary Search Tree (Contd.) 1. Allocate memory for the new node.

2. Assign value to the data field of new node.

3. Make the left and right child of the new node point to NULL.

4. Locate the node which will be the parent of the node to be inserted. Mark it as parent.

5. If parent is NULL (Tree is empty):

a. Mark the new node as ROOTb. Exit

6. If the value in the data field of new node is less than that of parent:

a. Make the left child of parent point to the new node

b. Exit

7. If the value in the data field of the new node is greater than that of the parent:

a. Make the right child of parent point to the new node

b. Exit

55

52

68

59 7224

8070

36

.

.

..

.

..

.

root

parent

Data Structures and Algorithms

Session 13Ver. 1.0

Inserting Nodes in a Binary Search Tree (Contd.) 1. Allocate memory for the new node.

2. Assign value to the data field of new node.

3. Make the left and right child of the new node point to NULL.

4. Locate the node which will be the parent of the node to be inserted. Mark it as parent.

5. If parent is NULL (Tree is empty):

a. Mark the new node as ROOTb. Exit

6. If the value in the data field of new node is less than that of parent:

a. Make the left child of parent point to the new node

b. Exit

7. If the value in the data field of the new node is greater than that of the parent:

a. Make the right child of parent point to the new node

b. Exit

55

52

68

59 7224

8070

36

.

.

..

.

..

.

root

parent

.

Data Structures and Algorithms

Session 13Ver. 1.0

Insert operation complete

Inserting Nodes in a Binary Search Tree (Contd.) 1. Allocate memory for the new node.

2. Assign value to the data field of new node.

3. Make the left and right child of the new node point to NULL.

4. Locate the node which will be the parent of the node to be inserted. Mark it as parent.

5. If parent is NULL (Tree is empty):

a. Mark the new node as ROOTb. Exit

6. If the value in the data field of new node is less than that of parent:

a. Make the left child of parent point to the new node

b. Exit

7. If the value in the data field of the new node is greater than that of the parent:

a. Make the right child of parent point to the new node

b. Exit

55

52

68

59 7224

8070

36

.

.

..

.

..

.

root

parent

Data Structures and Algorithms

Session 13Ver. 1.0

Deleting Nodes from a Binary Search Tree

Delete operation in a binary search tree refers to the process of deleting the specified 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.

Write an algorithm to locate the position of the node to deleted from a binary search tree.

Data Structures and Algorithms

Session 13Ver. 1.0

Algorithm to locate the node to be deleted.

52

68

59 72

69

24

8070

36

.

.

..

..

..

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

root

Suppose you want to delete node 70

Deleting Nodes from a Binary Search Tree (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

Deleting Nodes from a Binary Search Tree (Contd.)

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

currentNodeparent = NULL

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

currentNodeparent = NULL

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

currentNodeparent = NULL

parent

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode parent

70 > 52

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode parent

70 > 52

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode

currentNode

parent

70 > 52

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode

parent

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

.currentNode

parent

parent

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode

parent

70 > 68

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode

parent

70 > 68

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode

parent

70 > 68

currentNode

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

parentcurrentNode

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

parentcurrentNode

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

parent

Data Structures and Algorithms

Session 13Ver. 1.0

70 < 72

currentNode

parent

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode

parent

70 < 72

currentNode

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

parent

currentNode

Nodes located

Deleting Nodes from a Binary Search Tree (Contd.)

1. Make a variable/pointer currentNode point to the ROOT node.

2. Make a variable/pointer parent point to NULL.

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. Make parent point to currentNode.

b. If the value to be deleted is less than that of currentNode:

i. Make currentNode point to its left child.

c. If the value to be deleted is greater than that of currentNode:

i. Make currentNode point to its right child.

52

68

59 7224

8070

36

.

.

..

..

..

root

Suppose you want to delete node 70

69

Data Structures and Algorithms

Session 13Ver. 1.0

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

Data Structures and Algorithms

Session 13Ver. 1.0

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

Deleting Nodes from a Binary Search Tree (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

Algorithm to delete a leaf node from the binary tree.

1. Locate the node to be deleted. Mark it as currentNode and its parent as parent.

2. If currentNode is the root node: // If parent is // NULL

a. Make ROOT point to NULL.b. Go to step 5.

3. If currentNode is left child of parent:

a. Make left child field of parent point to NULL.

b. Go to step 5.

4. If currentNode is right child of parent:

a. Make right child field of parent point to NULL.

b. Go to step 5.

5. Release the memory for currentNode.

52

68

59 72

69

24

8070

36

.

.

..

..

..

Delete node 69

Deleting Nodes from a Binary Search Tree (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

Delete node 69

parent

1. Locate the node to be deleted. Mark it as currentNode and its parent as parent.

2. If currentNode is the root node: // If parent is // NULL

a. Make ROOT point to NULL.b. Go to step 5.

3. If currentNode is left child of parent:

a. Make left child field of parent point to NULL.

b. Go to step 5.

4. If currentNode is right child of parent:

a. Make right child field of parent point to NULL.

b. Go to step 5.

5. Release the memory for currentNode.

currentNode

Deleting Nodes from a Binary Search Tree (Contd.)

52

68

59 7224

8070

36

.

.

..

..

..

69

Data Structures and Algorithms

Session 13Ver. 1.0

Delete node 69

Deleting Nodes from a Binary Search Tree (Contd.)

1. Locate the node to be deleted. Mark it as currentNode and its parent as parent.

2. If currentNode is the root node: // If parent is // NULL

a. Make ROOT point to NULL.b. Go to step 5.

3. If currentNode is left child of parent:

a. Make left child field of parent point to NULL.

b. Go to step 5.

4. If currentNode is right child of parent:

a. Make right child field of parent point to NULL.

b. Go to step 5.

5. Release the memory for currentNode.

52

68

59 7224

8070

36

.

.

..

..

..

69parent

currentNode

Data Structures and Algorithms

Session 13Ver. 1.0

Delete node 69

Deleting Nodes from a Binary Search Tree (Contd.)

1. Locate the node to be deleted. Mark it as currentNode and its parent as parent.

2. If currentNode is the root node: // If parent is // NULL

a. Make ROOT point to NULL.b. Go to step 5.

3. If currentNode is left child of parent:

a. Make left child field of parent point to NULL.

b. Go to step 5.

4. If currentNode is right child of parent:

a. Make right child field of parent point to NULL.

b. Go to step 5.

5. Release the memory for currentNode.

52

68

59 7224

8070

36

.

.

..

..

..

69parent

currentNode

Data Structures and Algorithms

Session 13Ver. 1.0

Delete node 69

Deleting Nodes from a Binary Search Tree (Contd.)

1. Locate the node to be deleted. Mark it as currentNode and its parent as parent.

2. If currentNode is the root node: // If parent is // NULL

a. Make ROOT point to NULL.b. Go to step 5.

3. If currentNode is left child of parent:

a. Make left child field of parent point to NULL.

b. Go to step 5.

4. If currentNode is right child of parent:

a. Make right child field of parent point to NULL.

b. Go to step 5.

5. Release the memory for currentNode.

52

68

59 7224

8070

36

.

.

..

..

..

69parent

currentNode

Data Structures and Algorithms

Session 13Ver. 1.0

Delete node 69

Deleting Nodes from a Binary Search Tree (Contd.)

1. Locate the node to be deleted. Mark it as currentNode and its parent as parent.

2. If currentNode is the root node: // If parent is // NULL

a. Make ROOT point to NULL.b. Go to step 5.

3. If currentNode is left child of parent:

a. Make left child field of parent point to NULL.

b. Go to step 5.

4. If currentNode is right child of parent:

a. Make right child field of parent point to NULL.

b. Go to step 5.

5. Release the memory for currentNode.

52

68

59 7224

8070

36

.

.

..

..

..

69parent

currentNode

Data Structures and Algorithms

Session 13Ver. 1.0

Delete operation complete

Delete node 69

Deleting Nodes from a Binary Search Tree (Contd.)

1. Locate the node to be deleted. Mark it as currentNode and its parent as parent.

2. If currentNode is the root node: // If parent is // NULL

a. Make ROOT point to NULL.b. Go to step 5.

3. If currentNode is left child of parent:

a. Make left child field of parent point to NULL.

b. Go to step 5.

4. If currentNode is right child of parent:

a. Make right child field of parent point to NULL.

b. Go to step 5.

5. Release the memory for currentNode.

52

68

59 7224

8070

36

.

.

..

..

..

69parent

currentNode

Data Structures and Algorithms

Session 13Ver. 1.0

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

Deleting Nodes from a Binary Search Tree (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

Algorithm to delete a node 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. Release the memory of currentNode.

52

68

59 72

75

24

8070

36.

..

..

..

.

root

Deleting Nodes from a Binary Search Tree (Contd.)

Delete node 80

Data Structures and Algorithms

Session 13Ver. 1.0

52

68

59 72

75

24

8070

36.

..

..

..

.

root

parent

currentNode

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. Release the memory of currentNode.

Deleting Nodes from a Binary Search Tree (Contd.)

Delete node 80

Data Structures and Algorithms

Session 13Ver. 1.0

52

68

59 72

75

24

8070

36.

..

..

..

.

root

parent

currentNode

Deleting Nodes from a Binary Search Tree (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.

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. Release the memory of currentNode.

Delete node 80

Data Structures and Algorithms

Session 13Ver. 1.0

52

68

59 72

75

24

8070

36.

..

..

..

.

root

parent

currentNode

child

Deleting Nodes from a Binary Search Tree (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.

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. Release the memory of currentNode.

Delete node 80

Data Structures and Algorithms

Session 13Ver. 1.0

52

68

59 72

75

24

8070

36.

..

..

..

.

root

parent

currentNode

child

Deleting Nodes from a Binary Search Tree (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.

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. Release the memory of currentNode.

Delete node 80

Data Structures and Algorithms

Session 13Ver. 1.0

52

68

59 72

75

24

8070

36.

..

..

..

.

root

parent

currentNode

child

Deleting Nodes from a Binary Search Tree (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.

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. Release the memory of currentNode.

Delete node 80

Data Structures and Algorithms

Session 13Ver. 1.0

52

68

59 72

75

24

8070

36.

..

..

..

.

root

parent

currentNode

child

Deleting Nodes from a Binary Search Tree (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.

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. Release the memory of currentNode.

Delete node 80

Data Structures and Algorithms

Session 13Ver. 1.0

52

68

59 72

75

24

8070

36.

..

..

..

.

root

parent

currentNode

child

Deleting Nodes from a Binary Search Tree (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.

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. Release the memory of currentNode.

Delete node 80

Data Structures and Algorithms

Session 13Ver. 1.0

52

68

59 72

75

24

8070

36.

..

..

..

root

parent

currentNode

child

Deleting Nodes from a Binary Search Tree (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.

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. Release the memory of currentNode.

Delete node 80

.

Data Structures and Algorithms

Session 13Ver. 1.0

52

68

59 72

75

24

8070

36.

..

..

..

root

parent

currentNode

child

Deleting Nodes from a Binary Search Tree (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.

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. Release the memory of currentNode.

Delete node 80

.

Data Structures and Algorithms

Session 13Ver. 1.0

52

68

59 72

75

24

8070

36.

..

..

..

root

parent

currentNode

child

Delete operation complete

Deleting Nodes from a Binary Search Tree (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.

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. Release the memory of currentNode.

Delete node 80

.

Data Structures and Algorithms

Session 13Ver. 1.0

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

Deleting Nodes from a Binary Search Tree (Contd.)

Data Structures and Algorithms

Session 13Ver. 1.0

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 72Algorithm to delete a node with two children.

Deleting Nodes from a Binary Search Tree (Contd.)

52

68

59 72

75

24

8070

36.

..

..

..

.

root

Data Structures and Algorithms

Session 13Ver. 1.0

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.

currentNode

parent

Delete node 72

Deleting Nodes from a Binary Search Tree (Contd.)

52

68

59 72

75

24

8070

36.

..

.

..

.

root

.

Data Structures and Algorithms

Session 13Ver. 1.0

currentNode

parent

Delete node 72

Deleting Nodes from a Binary Search Tree (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.

52

68

59 72

75

24

8070

36.

..

.

..

.

root

.

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder_suc

Delete node 72

Deleting Nodes from a Binary Search Tree (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.

52

68

59 72

75

24

8070

36.

..

.

..

.

root

currentNode

parent

.

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder_suc

Delete node 72

Deleting Nodes from a Binary Search Tree (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.

52

68

59 72

75

24

8070

36.

..

.

..

.

root

currentNode

parent

.

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder_suc

Inorder_suc

Delete node 72

Deleting Nodes from a Binary Search Tree (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.

52

68

59 72

75

24

8070

36.

..

.

..

.

root

currentNode

parent

.

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder_suc

Delete node 72

Deleting Nodes from a Binary Search Tree (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.

52

68

59 72

75

24

8070

36.

..

.

..

.

root

currentNode

parent

.

75

Data Structures and Algorithms

Session 13Ver. 1.0

Inorder_suc

Delete node 72

Deleting Nodes from a Binary Search Tree (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.

52

68

59

75

24

8070

36.

..

.

..

.

root

currentNode

parent

.

75

Data Structures and Algorithms

Session 13Ver. 1.0

75

Inorder_suc

Delete operation complete

Deleting Nodes from a Binary Search Tree (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.

52

68

59

75

24

8070

36.

..

.

..

.

root

currentNode

parent

.

75

Delete node 72:

Data Structures and Algorithms

Session 13Ver. 1.0

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.

Data Structures and Algorithms

Session 13Ver. 1.0

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

Data Structures and Algorithms

Session 13Ver. 1.0

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

Summary (Contd.)