© All Rights Reserved

33 views

© All Rights Reserved

- Chapter8.Ans
- 12_DS and Algorithm_session_17.pps
- 05_DS and Algorithm_session_07.pps
- 11 DS and Algorithm Tree
- Lec-10 Trees - BST
- C program for Binary Search Tree
- 03 DS and Algorithm Session 04
- 02 DS and Algorithm Session 02
- Unit 6final
- 10_DS and Algorithm_session_14.pps
- 07_DS and Algorithm_session_10.pps
- 06_DS and Algorithm_session_08.pps
- 08_DS and Algorithm_session_11.pps
- 04_DS and Algorithm_session_05.pps
- 01 DS and Algorithm Session 01
- Algorithms Questions
- Binary Search Trees
- Bst
- 12-Binary Search Trees
- binary tree.docx

You are on page 1of 136

Store data in a tree

Implement a binary tree

Implement a binary search 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

in adata

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

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

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

DegreeA of

node:

It refers

number

Edge:

linka from

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

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

node.

A

C

siblings of each other.

Nodes E, F, G, and H are

siblings of each other.

J

L

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

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.

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

Answer:

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

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

Full binary tree:

d

nodes.

A

Depth = 3

Total number of

3

nodes = 2 1 = 7

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

Array representation of binary trees:

All the nodes are represented as the

0

A

3

D

C

4

E

Binary Tree

[0]

[1]

[2]

[3]

[4]

[5]

[6]

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.

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

Linked Representation

80

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

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

The left subtree of node D is NULL.

Therefore, visit node D.

root

A

Right subtree of D is not NULL

Therefore, move to the right subtree of node D

root

A

Left subtree of H is empty.

Therefore, visit node H.

root

A

Right subtree of H is empty.

Therefore, move to node B.

root

A

The left subtree of B has been visited.

Therefore, visit node B.

root

A

Right subtree of B is not empty.

Therefore, move to the right subtree of B.

root

A

Left subtree of E is empty.

Therefore, visit node E.

root

A

Right subtree of E is empty.

Therefore, move to node A.

root

A

Left subtree of A has been visited.

Therefore, visit node A.

root

A

Right subtree of A is not empty.

Therefore, move to the right subtree of A.

root

A

Left subtree of C is not empty.

Therefore, move to the left subtree of C.

root

A

Left subtree of F is empty.

Therefore, visit node F.

root

A

Right subtree of F is empty.

Therefore, move to node C.

root

A

The left subtree of node C has been visited.

Therefore, visit node C.

root

A

Right subtree of C is not empty.

Therefore, move to the right subtree of node C.

root

A

Left subtree of G is not empty.

Therefore, move to the left subtree of node G.

root

A

Left subtree of I is empty.

Therefore, visit I.

root

A

Right subtree of I is empty.

Therefore, move to node G.

root

A

Visit node G.

root

A

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

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

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.

Answer:

Preorder

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.

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

advantages of both

However,

insertion

arrays and

linked

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.

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.

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

You can implement various operations on a binary search

tree:

Traversal

Search

Insert

Delete

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

Depending on the result of the comparison, there can be three

possibilities:

a.

i.

ii.

b.

i.

ii.

c.

Display Found

Exit

Make currentNode point to its left child

Go to step 2

i.

ii.

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.

Answer:

smaller

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.

(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

(Contd.)

Refer to the algorithm to

of the

new node to be inserted.

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

(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

(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

(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

(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

(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

(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

(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

(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

(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

(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

(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

(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

(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

(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

(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

currentNode

currentNode = NULL

of currentNode:

a.

59

70

72

.

80

(Contd.)

Locate the position of a new node

55.

root

52

1.

2.

3.

becomes NULL

4.

5.

currentNode:

a.

36

68

6.

parent

24

currentNode = NULL

of currentNode:

a.

59

70

72

.

80

(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.

currentNode:

a.

36

68

6.

parent

24

currentNode = NULL

of currentNode:

a.

59

70

72

.

80

(Contd.)

Write an algorithm to insert a node 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.

the node to be inserted. Mark it as parent.

5.

a.

b.

36

68

6.

less than that of parent:

a.

24

59

72

b.

7.

70

80

Exit

the new node

Exit

is greater than that of the parent:

a.

b.

the new node

Exit

(Contd.)

1.

Allocate memory for the new node.

root

.

.

52

2.

3.

point to NULL.

4.

the node to be inserted. Mark it as parent.

5.

a.

b.

36

68

6.

less than that of parent:

a.

24

59

72

b.

7.

70

80

Exit

the new node

Exit

is greater than that of the parent:

a.

b.

the new node

Exit

(Contd.)

1.

Allocate memory for the new node.

root

.

.

52

2.

3.

point to NULL.

4.

the node to be inserted. Mark it as parent.

5.

a.

b.

36

68

6.

less than that of parent:

a.

24

59

72

b.

7.

55

70

80

Exit

the new node

Exit

is greater than that of the parent:

a.

b.

the new node

Exit

(Contd.)

1.

Allocate memory for the new node.

root

.

.

52

2.

3.

point to NULL.

4.

the node to be inserted. Mark it as parent.

5.

a.

b.

36

68

6.

less than that of parent:

a.

24

59

72

b.

7.

55

70

80

Exit

the new node

Exit

is greater than that of the parent:

a.

b.

the new node

Exit

(Contd.)

1.

Allocate memory for the new node.

root

.

.

52

2.

3.

point to NULL.

4.

the node to be inserted. Mark it as parent.

5.

a.

b.

36

68

6.

parent

24

72

b.

7.

55

less than that of parent:

a.

59

70

80

Exit

the new node

Exit

is greater than that of the parent:

a.

b.

the new node

Exit

(Contd.)

1.

Allocate memory for the new node.

root

.

.

52

2.

3.

point to NULL.

4.

the node to be inserted. Mark it as parent.

5.

a.

b.

36

68

6.

parent

24

72

b.

7.

55

less than that of parent:

a.

59

70

80

Exit

the new node

Exit

is greater than that of the parent:

a.

b.

the new node

Exit

(Contd.)

1.

Allocate memory for the new node.

root

.

.

52

2.

3.

point to NULL.

4.

the node to be inserted. Mark it as parent.

5.

a.

b.

36

68

6.

parent

24

72

b.

7.

55

less than that of parent:

a.

59

70

80

Exit

the new node

Exit

is greater than that of the parent:

a.

b.

the new node

Exit

(Contd.)

1.

Allocate memory for the new node.

root

.

.

52

2.

3.

point to NULL.

4.

the node to be inserted. Mark it as parent.

5.

a.

b.

36

68

6.

parent

24

72

b.

7.

55

less than that of parent:

a.

59

70

80

Exit

the new node

Exit

is greater than that of the parent:

a.

b.

the new node

Exit

(Contd.)

1.

Allocate memory for the new node.

root

.

.

52

2.

3.

point to NULL.

4.

the node to be inserted. Mark it as parent.

5.

a.

b.

36

68

6.

parent

24

72

b.

7.

55

less than that of parent:

a.

59

70

80

Exit

the new node

Exit

is greater than that of the parent:

a.

b.

the new node

Exit

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.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

59

1.

72

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

currentNode

.

24

52

.

.

36

68

69

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

59

1.

72

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

currentNode

parent = NULL

.

24

52

.

.

36

68

69

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

59

1.

72

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

currentNode

parent = NULL

.

24

52

.

.

36

68

69

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

59

1.

72

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

currentNode

parent = NULL

.

24

52

parent

36

68

69

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

59

1.

72

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

currentNode

.

24

52

parent

36

68

.

69

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

70 > 52

59

1.

72

i.

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

currentNode

.

24

52

parent

36

68

.

69

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

70 > 52

59

1.

72

i.

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

currentNode

52

parent

currentNode

.

24

36

68

.

69

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

70 > 52

59

1.

72

i.

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

52

parent

currentNode

.

24

36

68

69

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

59

1.

72

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

52

parent

currentNode

.

24

36

parent

68

69

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

59

1.

72

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

52

70 > 68

currentNode

.

24

36

parent

68

69

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

59

1.

72

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

52

70 > 68

currentNode

.

24

36

parent

68

69

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

59

1.

72

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

52

70 > 68

currentNode

36

parent

68

1.

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

currentNode

24

59

.

69

72

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

.

.

36

52

parent

68

1.

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

currentNode

24

59

.

69

72

c.

than that of currentNode:

i.

70

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

.

.

36

52

parent

68

1.

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

currentNode

24

59

.

69

72

c.

70

than that of currentNode:

i.

parent

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

.

.

52

70 < 72

36

68

1.

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

currentNode

24

59

.

69

72

c.

70

than that of currentNode:

i.

parent

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

.

.

52

70 < 72

36

68

1.

the ROOT node.

2.

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

i.

currentNode

24

59

72

c.

70

than that of currentNode:

i.

parent

currentNode

69

.

80

left child.

right child.

(Contd.)

Suppose you want to delete

node 70

root

.

.

52

.

.

36

24

68

72

3.

becomes NULL or the value of the node to be

searched becomes equal to that of

currentNode:

a.

b.

that of currentNode:

c.

70

80

left child.

than that of currentNode:

i.

parent

currentNode

69

2.

i.

.

.

the ROOT node.

59

Nodes located

1.

right child.

(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

(Contd.)

Write an algorithm to delete a leaf node from a binary

search tree.

(Contd.)

Delete node

Algorithm

to delete

69

a leaf

node from the binary tree.

52

1.

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.

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.

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.

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.

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.

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.

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.

(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.

currentNode and its parent as parent.

2.

a.

Mark the left child of currentNode as

child.

b.

Go to step 4.

3.

a.

Mark the right child of currentNode as

child.

b.

Go to step 4.

4.

a.

Mark child as root.

b.

Go to step 7.

5.

a.

Make left child field of parent point to

child.

b.

Go to step 7.

6.

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.

currentNode and its parent as parent.

2.

a.

Mark the left child of currentNode as

child.

b.

Go to step 4.

3.

a.

Mark the right child of currentNode as

child.

b.

Go to step 4.

4.

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

a.

Make left child field of parent point to

child.

b.

Go to step 7.

6.

a.

Make right child field of parent point to

child.

b.

Go to step 7.

7.

75

(Contd.)

1.

currentNode and its parent as parent.

2.

a.

Mark the left child of currentNode as

child.

b.

Go to step 4.

3.

a.

Mark the right child of currentNode as

child.

b.

Go to step 4.

4.

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

a.

Make left child field of parent point to

child.

b.

Go to step 7.

6.

a.

Make right child field of parent point to

child.

b.

Go to step 7.

7.

75

(Contd.)

1.

currentNode and its parent as parent.

2.

a.

Mark the left child of currentNode as

child.

b.

Go to step 4.

3.

a.

Mark the right child of currentNode as

child.

b.

Go to step 4.

4.

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.

a.

Make right child field of parent point to

child.

b.

Go to step 7.

7.

75

child

a.

Make left child field of parent point to

child.

b.

Go to step 7.

(Contd.)

1.

currentNode and its parent as parent.

2.

a.

Mark the left child of currentNode as

child.

b.

Go to step 4.

3.

a.

Mark the right child of currentNode as

child.

b.

Go to step 4.

4.

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.

a.

Make right child field of parent point to

child.

b.

Go to step 7.

7.

75

child

a.

Make left child field of parent point to

child.

b.

Go to step 7.

(Contd.)

1.

currentNode and its parent as parent.

2.

a.

Mark the left child of currentNode as

child.

b.

Go to step 4.

3.

a.

Mark the right child of currentNode as

child.

b.

Go to step 4.

4.

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.

a.

Make right child field of parent point to

child.

b.

Go to step 7.

7.

75

child

a.

Make left child field of parent point to

child.

b.

Go to step 7.

(Contd.)

1.

currentNode and its parent as parent.

2.

a.

Mark the left child of currentNode as

child.

b.

Go to step 4.

3.

a.

Mark the right child of currentNode as

child.

b.

Go to step 4.

4.

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.

a.

Make right child field of parent point to

child.

b.

Go to step 7.

7.

75

child

a.

Make left child field of parent point to

child.

b.

Go to step 7.

(Contd.)

1.

currentNode and its parent as parent.

2.

a.

Mark the left child of currentNode as

child.

b.

Go to step 4.

3.

a.

Mark the right child of currentNode as

child.

b.

Go to step 4.

4.

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.

a.

Make right child field of parent point to

child.

b.

Go to step 7.

7.

75

child

a.

Make left child field of parent point to

child.

b.

Go to step 7.

(Contd.)

1.

currentNode and its parent as parent.

2.

a.

Mark the left child of currentNode as

child.

b.

Go to step 4.

3.

a.

Mark the right child of currentNode as

child.

b.

Go to step 4.

4.

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.

a.

Make right child field of parent point to

child.

b.

Go to step 7.

7.

75

child

a.

Make left child field of parent point to

child.

b.

Go to step 7.

(Contd.)

1.

currentNode and its parent as parent.

2.

a.

Mark the left child of currentNode as

child.

b.

Go to step 4.

3.

a.

Mark the right child of currentNode as

child.

b.

Go to step 4.

4.

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.

a.

Make right child field of parent point to

child.

b.

Go to step 7.

7.

75

child

a.

Make left child field of parent point to

child.

b.

Go to step 7.

(Contd.)

1.

currentNode and its parent as parent.

2.

a.

Mark the left child of currentNode as

child.

b.

Go to step 4.

3.

a.

Mark the right child of currentNode as

child.

b.

Go to step 4.

4.

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.

a.

Make right child field of parent point to

child.

b.

Go to step 7.

7.

75

child

a.

Make left child field of parent point to

child.

b.

Go to step 7.

(Contd.)

Write an algorithm to delete a node, which has two children

from a binary search tree.

(Contd.)

Delete node

72

Algorithm

to delete

a node

with two children.

1.

currentNode and its parent as parent.

2.

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.

with that of Inorder_suc.

4.

node:

a.

Delete the node marked Inorder_suc

by using the algorithm for Case I.

5.

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.

currentNode and its parent as parent.

2.

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.

with that of Inorder_suc.

4.

node:

a.

Delete the node marked Inorder_suc

by using the algorithm for Case I.

5.

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.

currentNode and its parent as parent.

2.

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.

with that of Inorder_suc.

4.

node:

a.

Delete the node marked Inorder_suc

by using the algorithm for Case I.

5.

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.

currentNode and its parent as parent.

2.

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.

with that of Inorder_suc.

4.

node:

a.

Delete the node marked Inorder_suc

by using the algorithm for Case I.

5.

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.

currentNode and its parent as parent.

2.

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.

with that of Inorder_suc.

4.

node:

a.

Delete the node marked Inorder_suc

by using the algorithm for Case I.

5.

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.

currentNode and its parent as parent.

2.

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.

with that of Inorder_suc.

4.

node:

a.

Delete the node marked Inorder_suc

by using the algorithm for Case I.

5.

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.

currentNode and its parent as parent.

2.

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.

with that of Inorder_suc.

4.

node:

a.

Delete the node marked Inorder_suc

by using the algorithm for Case I.

5.

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.

currentNode and its parent as parent.

2.

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.

with that of Inorder_suc.

4.

node:

a.

Delete the node marked Inorder_suc

by using the algorithm for Case I.

5.

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.

currentNode and its parent as parent.

2.

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.

with that of Inorder_suc.

4.

node:

a.

Delete the node marked Inorder_suc

by using the algorithm for Case I.

5.

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

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

- Chapter8.AnsUploaded bygabboudeh
- 12_DS and Algorithm_session_17.ppsUploaded byVikas Singh
- 05_DS and Algorithm_session_07.ppsUploaded byVikas Singh
- 11 DS and Algorithm TreeUploaded byRahul Sharma
- Lec-10 Trees - BSTUploaded byTaqi Shah
- C program for Binary Search TreeUploaded bySaiyasodharan
- 03 DS and Algorithm Session 04Uploaded byVikas Singh
- 02 DS and Algorithm Session 02Uploaded bySrinivasa Helavar
- Unit 6finalUploaded byAnonymous bTh744z7E6
- 10_DS and Algorithm_session_14.ppsUploaded byVikas Singh
- 07_DS and Algorithm_session_10.ppsUploaded byVikas Singh
- 06_DS and Algorithm_session_08.ppsUploaded byVikas Singh
- 08_DS and Algorithm_session_11.ppsUploaded byVikas Singh
- 04_DS and Algorithm_session_05.ppsUploaded byVikas Singh
- 01 DS and Algorithm Session 01Uploaded byDaya Nand Mishra
- Algorithms QuestionsUploaded byMuralidhar Nimmagadda
- Binary Search TreesUploaded byBop Dara
- BstUploaded byapi-3825915
- 12-Binary Search TreesUploaded byFatima Mattar
- binary tree.docxUploaded byGarima Taank
- Lecture 10Uploaded bySyed Haider
- Binary Search Tree AlgorithmUploaded byMalathi Sankar
- dstUploaded bySachdeva Ashu
- BstUploaded bysuvaidzar_551293571
- 07 Arboles de Búsqueda BinariosUploaded byCesar Gutierrez Tineo
- AV482_DS_DBMS-Lecture-13-BinarySearchTree_a.pdfUploaded byshikhamaharana
- Binary Search Tree- Data StructuresUploaded byAhmed Fiaz Jaan
- bst-2.pdfUploaded bysudhan
- DA_3.1_RMUploaded byB S Praveen Bsp
- AVL TreeUploaded bysalithakk

- TocUploaded byVikas Singh
- Maths formula TrignometryUploaded byVivek Singh
- 08_DS and Algorithm_session_11.ppsUploaded byVikas Singh
- 06_DS and Algorithm_session_08.ppsUploaded byVikas Singh
- 07_DS and Algorithm_session_10.ppsUploaded byVikas Singh
- 10_DS and Algorithm_session_14.ppsUploaded byVikas Singh
- 03 DS and Algorithm Session 04Uploaded byVikas Singh
- 04_DS and Algorithm_session_05.ppsUploaded byVikas Singh
- 02 DS and Algorithm Session 02Uploaded bySrinivasa Helavar
- 01 DS and Algorithm Session 01Uploaded byDaya Nand Mishra

- Gradle User GuideUploaded byGonzalo Zavala
- thereses resumeUploaded byapi-436983625
- 6 Blended Learning ModelsUploaded byTeguh Budiarto
- Predictable-Revenue-Guide-To-Tripling-Your-Sales-Parts-1-4.pdfUploaded byalexi.wiedemann6244
- Senior Thesis in Computing and the ArtsUploaded byShoMatsuzaki
- Philips G90-AEUploaded byaulin64_845645735
- k Relay-e9 Lowres 26-28 EnUploaded byAnonymous CRQ100
- Ali PayUploaded byAhmed Gul
- Training CommunicationUploaded bymukeshh
- Hitec - Katalog CMM 2018 ENUploaded byD.T.
- ImageProcessing1-Introduction-Bryan-Mac-Namee.pdfUploaded bytweenturbo
- Corporate Employee Welfare SRSUploaded byKrishnaPandey
- DELL R610 TroubleshootingUploaded bylankoor
- Chapter7rev114587mkjuytrUploaded byAparna Kadam
- BIDM-regUploaded bySagar Damani
- Contingency analysis of Power Systems.pdfUploaded bybenitogaldos19gmail.com
- Nebula in a Mastering Context - Gearslutz Pro Audio CommunityUploaded byGustavo Soler
- Stakeholder Influence MapUploaded byprasad
- Airtel Digital TV DTH FAQsUploaded bySushubh
- SAS® Email Data Driven ApplicationUploaded byfffresh
- Motion of Public Advocates Office for an Immediate Order Requiring Communications ProvidersUploaded byBayAreaNewsGroup
- Etbkk ProfileUploaded byTamer Saleh El Sayed
- 04073852 RockefellerUploaded bykarlTronxo
- Turing Machine based EncryptionUploaded byMahalingam Ramaswamy
- Computer Systems Servicing Nc IIUploaded byRubyrosa Luna
- SAP MMUploaded byPraveen Malavae
- Girador de BaldeUploaded byotidracir23
- AcrSavoniac D - IUploaded byЕвгения
- Phpunit BookUploaded byJose Gaspar Agullo Soro
- 2012 Average Inductor Current Sensor for Digitally Controlled Switched-Mode Power SuppliesUploaded byersathis