Вы находитесь на странице: 1из 21

# Data Structures

Deleting a Node
 most complicated but one of the common
operations you can perform
 three cases to consider
 The node is a leaf node (has no children)
 The node has only one subtree/child (either
left or right)
 The node has two subtrees
 there are at most three nodes whose pointer
fields are affected whenever a node is deleted
 The parent node of the node to be deleted
 The parent node of the replacing node
 The replacing node

Trees (cont..)

*Property of STI
Page 1 of 21

Data Structures

Deleting a Node
 Case 1: The node to be deleted has no children

Trees (cont..)

*Property of STI
Page 2 of 21

Data Structures

Deleting a Node
 Case 1
public boolean delete(int key)
{
Node current = root;
Node parent = root;
boolean isLeftChild = true;
while(current.iData != key)
{
parent = current;
if(key < current.iData)
{
isLeftChild = true;
current = current.leftChild;
}
else
{
isLeftChild = false;
current = current.rightChild;
}
if(current==null)
return false;
}
// found node to delete
//continues
}
Trees (cont..)

*Property of STI
Page 3 of 21

Data Structures

Deleting a Node
 Case 1

//delete()
//if no children, simply delete it
if(current.leftChild==null &&
current.rightChild==null)
{
if(current == root)
root = null;
else if(isLeftChild)
parent.leftChild = null;
else
parent.rightChild = null;
}
// continues

Trees (cont..)

*Property of STI
Page 4 of 21

Data Structures

Deleting a Node
 Case 2: The node to be deleted has one child

Trees (cont..)

*Property of STI
Page 5 of 21

Data Structures

Deleting a Node
 Case 2
// if no right child, replace with left
subtree
else if(current.rightChild == null)
if(current==root)
root = current.leftChild;
else if(isLeftChild)
parent.leftChild=current.leftChild;
else
parent.rightChild=current.leftChild;
// if no left child, replace with right
//subtree
else if(current.leftChild == null)
if(current == root)
root = current.rightChild;
else if(isLeftChild)
parent.leftChild=current.rightChild;
else
parent.rightChild=current.rightChild;
//continued..
Trees (cont..)

*Property of STI
Page 6 of 21

Data Structures

Deleting a Node
 Case 3: The node to be deleted has two
children

 Inorder Successor

Trees (cont..)

*Property of STI
Page 7 of 21

Data Structures

Exercise
 Given a BST, perform the three cases of
deletion. For every deletion, identify which case
must be performed. (Note that the answers
should be continuous.)




Trees (cont..)

Delete B
Delete H
Delete I

*Property of STI
Page 8 of 21

Data Structures

 Delete B case 1

 Delete H case 2

 Delete I case 3

Trees (cont..)

*Property of STI
Page 9 of 21

Data Structures

## Finding the Successor

 go through the original nodes right child
 then goes on the right childs left child (if it has
one)
 to this left childs left child and so on
 the last left child in this path is the successor of
the original node

Trees (cont..)

*Property of STI
Page 10 of 21

Data Structures

## Finding the Successor

 If the right child of the original node has no left
children, then this right child itself is the
successor

Trees (cont..)

*Property of STI
Page 11 of 21

Data Structures

## Finding the Successor

 Minimum and Maximum value

## public Node minimum()

{
Node current, last;
current = root;
while(current != null )
{
last = current;
current = current.leftChild;
}
return last;
}

Trees (cont..)

*Property of STI
Page 12 of 21

Data Structures

## private node getSuccessor(node delNode)

{
Node successorParent = delNode;
Node successor = delNode ;
Node current = delNode.rightChild ;
while(current != null)
{
successorParent = successor;
successor = current;
current = current.leftChild;
}
if(successor != delNode.rightChild)
{
successorParent.leftChild=
successor.rightChild;
successor.rightChild=
delNode.rightChild;
}
return successor;
}

Trees (cont..)

*Property of STI
Page 13 of 21

Data Structures

## Finding the Successor

 successor is Right Child of delNode
 Unplug current from the rightChild field
of its parent (or leftChild field if
appropriate), and set this field to point to
successor.


## Unplug currents left child from current and

plug it into the leftChild field of
successor.

##  fragments excerpted from delete() method

that carries out the above steps
1. parent.rightChild = successor;
2. successor.leftChild
current.leftChild;

Trees (cont..)

*Property of STI
Page 14 of 21

Data Structures

## Finding the Successor

 summarizes the situation showing connections
affected by the said two steps

Trees (cont..)

*Property of STI
Page 15 of 21

Data Structures

## Finding the Successor

// delete() continued
else //two children, so replace with
inorder successor
{
//get successor of node to
delete(current)
Node successor = getSuccessor(current);
//connect parent of current to
if (current == root)
root = successor;
else if(isLeftChild)
parent.leftChild = successor;
else
parent.rightChild = successor;
//connect successor to currents
left child
successor.leftChild =
current.leftChild;
}
return true;
} //end delete()

Trees (cont..)

*Property of STI
Page 16 of 21

Data Structures

## Finding the Successor

 successor is the Left Descendant of Right
Child of delNode
 four steps to perform the deletion
 Plug the right child of successor into the
leftChild field of the successors parent.


Trees (cont..)

## Plug the right child of the node to be

deleted into the rightChild field of
successor.
Unplug current from the rightChild field
of its parent, and set this field to point to
successor.
Unplug currents left child from current, and
plug it into the leftChild field of
successor.

*Property of STI
Page 17 of 21

Data Structures

##  The following corresponds to the four steps:

1. successorParent.leftChild=succes
sor.rightChild:
2. successor.rightChild =
delNode.rightChild;
3. parent.rightChild = successor;
4. successor.leftChild =
current.leftChild;
Trees (cont..)

*Property of STI
Page 18 of 21

Data Structures

## Trees Represented as Arrays

 it is in the array that nodes are stored and are
 the position of the node in the array
corresponds to its position in the tree

Trees (cont..)

*Property of STI
Page 19 of 21

Data Structures

Exercise
 Represent the tree in an array

Trees (cont..)

*Property of STI
Page 20 of 21

Data Structures