Algorithm to delete the specific node from binary searchtree, Data Structure & Algorithms

Assignment Help:

Q. Write down an algorithm to delete the specific node from binary search tree. Trace the algorithm to delete a node (10) from the following given tree.

1882_binary tree.png

Ans.

Algorithm for Delete ting the specific Node From the Binary Search Tree

To delete the specific node following possibilities may arise

1)      Node id a terminal node

2)      Node have only one child

3)      Node having 2 children.

DEL(INFO, LEFT, RIGT, ROOT, AVAIL, ITEM)

A binary search tree T is in the memory, and an ITEM of information is given as follows.
 This algorithm deletes the specific ITEM from the tree.

1. [to Find the locations of ITEM and its parent] Call FIND(INFO, RIGHT, ROOT, ITEM, LOC, PAR).

2. [ITEM in tree?]

if LOC=NULL, then write : ITEM not in tree, and Exit.

3. [Delete node containing ITEM.]

if RIGHT[LOC] != NULL and LEFT[LOC] !=NULL then:

Call CASEB(INFO,LEFT,RIGHT,ROOT,LOC,PAR). Else:

Call CASEA (INFO,LEFT,RIGHT,ROOT,LOC,PAR).

[End of if structure.]

4. [Return deleted node to AVAIL list.] Set LEFT[LOC]:=AVAIL and AVAIL:=LOC.

5. Exit.

CASEB(INFO,LEFT,RIGHT,ROOT,LOC,PAR)

This procedure will delete the node N at LOC location, where N has two children. The pointer PAR gives us the location of the parent of N, or else PAR=NULL indicates that N is a root node. The pointer SUC gives us the location of the inorder successor of N, and PARSUC gives us the location of the parent of the inorder successor.

1. [Find SUC and PARSUC.]

(a) Set PTR: = RIGHT[LOC] and SAVE:=LOC. (b) Repeat while LEFT[PTR] ≠  NULL:

Set SAVE:=PTR and PTR:=LEFT[PTR]. [End of loop.]

(c) Set SUC : = PTR and PARSUC:=SAVE.

2. [Delete inorder successor]

Call CASEA (INFO, LEFT, RIGHT, ROOT, SUC, PARSUC).

3. [Replace node N by its inorder successor.] (a) If PAR≠NULL, then:

If LOC = LEFT[PAR], then: Set LEFT[PAR]:=SUC.

Else:

Set RIGHT[PAR]: = SUC. [End of If structure.]

Else:

Set ROOT: = SUC. [End of If structure.]

(b) Set LEFT[SUC]:= LEFT [LOC] and

RIGHT[SUC]:=RIGHT[LOC]

4. Return.

CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR)

This procedure deletes the node N at LOC location, where N does not contain two children. The pointer PAR gives us the location of the parent of N, or else PAR=NULL indicates that N is a root node. The pointer CHILD gives us the location of the only child of the N, or else CHILD = NULL indicates N has no children.

1. [Initializes CHILD.]

If LEFT[LOC] = NULL and RIGHT[LOC] = NULL, then: Set CHILD:=NULL.

Else if LEFT[LOC]≠NULL, then:

Set CHILD: = LEFT[LOC].

Else

Set CHILD:=RIGHT[LOC] [End of If structue.]

2. If PAR ≠  NULL, then:

If LOC = LEFT [PAR], then:

Set LEFT[PAR]:=CHILD.

Else:

Set RIGHT[PAR]:CHILD = CHILD [End of If structure.]

Else:

Set ROOT : = CHILD.

[End of If structure.]

3. Return.

Inorder traversal of the tree is

4 6 10 11 12 14 15 20

To delete 10

PAR = Parent of 10 ie 15

SUC = inorder succ of 10 ie. 11

PARSUC = Parent of inorder succ ie 12

PTR = RIGHT [LOC]

Address of 12    SAVE: = address of 10

SAVE: = address of 12

PTR = address of 11

SUC = ADDRESS OF 11

PAR SUCC:= ADDRESS OF 12

CHILD = NULL

LEFT [PARSUC] = CHILD= NULL LEFT [PAR]= ADDRESS OF 11

LEFT [SUC] = LEFT [LOC] = ADDRESS OF 6

RIGHT [SUC] = RIGHT[LOC] = ADDRESS OF 12


Related Discussions:- Algorithm to delete the specific node from binary searchtree

Degree of node, Q. The degree of a node is defined as the number of childre...

Q. The degree of a node is defined as the number of children it has. Shear show that in any binary tree, the total number of leaves is one more than the number of nodes of degree 2

Advantage of list over arrays, The advantage of list over Arrays is flexibi...

The advantage of list over Arrays is flexibility. Over flood is not a problem until the computer memory is bushed. When the individual record are quite large, it may be difficult t

Rules for abstract data type-tree, null(nil) = true                     // ...

null(nil) = true                     // nil refer for empty tree null(fork(e, T, T'))= false   //  e : element , T and T are two sub tree leaf(fork(e, nil, nil)) = true leaf(

Indexed sequential file organisation, When there is requirement to access r...

When there is requirement to access records sequentially by some key value and also to access records directly by the similar key value, the collection of records may be organized

Flowcharts, draw a flowchart which prints all the even numbers between 1-50...

draw a flowchart which prints all the even numbers between 1-50

Conversion of forest into tree, Conversion of Forest into Tree A binary...

Conversion of Forest into Tree A binary tree may be used to show an entire forest, since the next pointer in the root of a tree can be used to point to the next tree of the for

Example of back face detection method, Example of Back Face Detection Metho...

Example of Back Face Detection Method To illustrate the method, we shall start with the tetrahedron (pyramid) PQRS of     Figure with vertices P (1, 1, 2), Q (3, 2, 3), R (1,

Define order of growth, Define order of growth The  efficiency  analysi...

Define order of growth The  efficiency  analysis  framework  concentrates   on  the  order  of  growth  of  an  algorithm's   basic operation count as the principal indicator o

Determine the stereo vision, Determine the stereo vision There is still...

Determine the stereo vision There is still one more major item missing, before we can look at a computer display or plot and perceive it just as we see a real object, namely th

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd