16
Data Structures and Algorithms Binary Search Trees – Delete operation Author of the lecture slides: Dr. Sohail Aslam

Ds lect 17 - trees (iv) bs ts

Embed Size (px)

Citation preview

Page 1: Ds   lect 17 - trees (iv) bs ts

Data Structures and Algorithms

Binary Search Trees – Delete operation

Author of the lecture slides: Dr. Sohail Aslam

Page 2: Ds   lect 17 - trees (iv) bs ts

Deleting a node in BST Once the node to be deleted is found, we need to

consider several possibilities:

Node is leaf node

Node has one child (left or right)

Node has both left and right child nodes

If the node is a leaf, it can be deleted immediately!

Page 3: Ds   lect 17 - trees (iv) bs ts

Deleting a node in BST If the node has one child, the node can be deleted after its

parent adjusts a pointer to bypass the node and connect to inorder successor.

6

2

4

3

1

8

Page 4: Ds   lect 17 - trees (iv) bs ts

Deleting a node in BST The inorder traversal order has to be maintained after

the delete.

6

2

4

3

1

8

6

2

4

3

1

8

Page 5: Ds   lect 17 - trees (iv) bs ts

Deleting a node in BST The inorder traversal order has to be maintained after

the delete.

6

2

4

3

1

8

6

2

4

3

1

8

6

2

31

8

Page 6: Ds   lect 17 - trees (iv) bs ts

Deleting a node in BST The complicated case:

node to be deleted has both left and right subtrees.

The strategy:

replace the data of this node with the smallest data of the right subtree and recursively delete that node.

Page 7: Ds   lect 17 - trees (iv) bs ts

Deleting a node in BST Delete(2): locate inorder successor

6

2

5

3

1

8

4Inorder

successor

Inorder successor will be the left-most node in the right subtree of 2.

The inorder successor will not have a left child because if it did, that child would be the left-most node.

Page 8: Ds   lect 17 - trees (iv) bs ts

Deleting a node in BST Delete(2): copy data from inorder successor

6

2

5

3

1

8

4

6

3

5

3

1

8

4

Page 9: Ds   lect 17 - trees (iv) bs ts

Deleting a node in BST Delete(2): remove the inorder successor

6

2

5

3

1

8

4

6

3

5

3

1

8

4

6

3

5

3

1

8

4

Page 10: Ds   lect 17 - trees (iv) bs ts

Deleting a node in BST Delete(2)

6

3

5

4

1

8

6

3

5

3

1

8

4

Page 11: Ds   lect 17 - trees (iv) bs ts

C++ code for delete

C++ keyword: ‘delete’

deleteNode routine to remove a TreeNode from BinaryTree.

Page 12: Ds   lect 17 - trees (iv) bs ts

Finding the minimum of a BST

TreeNode<int>* BSTree::findMin(TreeNode<int>*tree)

{

if( tree == NULL )

return NULL;

if( tree->getLeft() == NULL )

return tree; // this is it.

return findMin( tree->getLeft() );

}

Page 13: Ds   lect 17 - trees (iv) bs ts

Finding the maximum of a BST

TreeNode<int>* BSTree::findMax(TreeNode<int>* tree)

{

if( tree == NULL )

return NULL;

if( tree->getRight() == NULL )

return tree; // this is it.

return findMax( tree->getRight() );

}

Page 14: Ds   lect 17 - trees (iv) bs ts

C++ code for delete

TreeNode<int>* BSTree::remove(TreeNode<int>* tree, int info)

{TreeNode<int>* t;

int cmp = info -(tree->getInfo());

if( cmp < 0 ){ t = remove(tree->getLeft(), info);

tree->setLeft( t ); }

else if( cmp > 0 ){ t = remove(tree->getRight(), info);

tree->setRight( t ); }

Page 15: Ds   lect 17 - trees (iv) bs ts

C++ code for delete (continued)

// two children, replace with inorder successor

else if(tree->getLeft() != NULL && tree->getRight() != NULL ){

TreeNode<int>* minNode;

minNode = findMin(tree->getRight());

tree->setInfo( minNode->getInfo() );

t = remove(tree->getRight(),(minNode->getInfo()));

tree->setRight( t );

}

Page 16: Ds   lect 17 - trees (iv) bs ts

C++ code for delete (continued)

else { TreeNode<int>* nodeToDelete = tree;

if( tree->getLeft() == NULL )

tree = tree->getRight(); else if( tree->getRight() == NULL )

tree = tree->getLeft();

else tree = NULL; delete nodeToDelete; } return tree;}