Computer notes - Deleting a node in BST

1,919 views

Published on

As is common with many data structures, the hardest operation is deletion. Once we have found the node to be deleted, we need to consider several possibilities. If the node is a leaf, it can be deleted immediately.

Published in: Business, Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,919
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
144
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • Start lecture 16
  • End of lecture 15
  • End of lecture 16
  • Computer notes - Deleting a node in BST

    1. 1. Class No.16 Data Structures http://ecomputernotes.com
    2. 2. Deleting a node in BST <ul><li>As is common with many data structures, the hardest operation is deletion. </li></ul><ul><li>Once we have found the node to be deleted, we need to consider several possibilities. </li></ul><ul><li>If the node is a leaf , it can be deleted immediately. </li></ul>http://ecomputernotes.com
    3. 3. Deleting a node in BST <ul><li>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. </li></ul>http://ecomputernotes.com 6 2 4 3 1 8
    4. 4. Deleting a node in BST <ul><li>The inorder traversal order has to be maintained after the delete. </li></ul> http://ecomputernotes.com 6 2 4 3 1 8 6 2 4 3 1 8
    5. 5. Deleting a node in BST <ul><li>The inorder traversal order has to be maintained after the delete. </li></ul>  http://ecomputernotes.com 6 2 4 3 1 8 6 2 4 3 1 8 6 2 3 1 8
    6. 6. Deleting a node in BST <ul><li>The complicated case is when the node to be deleted has both left and right subtrees. </li></ul><ul><li>The strategy is to replace the data of this node with the smallest data of the right subtree and recursively delete that node. </li></ul>http://ecomputernotes.com
    7. 7. Deleting a node in BST <ul><li>Delete(2): locate inorder successor </li></ul>Inorder successor http://ecomputernotes.com 6 2 5 3 1 8 4
    8. 8. Deleting a node in BST <ul><li>Delete(2): locate inorder successor </li></ul>Inorder successor <ul><li>Inorder successor will be the left-most node in the right subtree of 2. </li></ul><ul><li>The inorder successor will not have a left child because if it did, that child would be the left-most node. </li></ul>http://ecomputernotes.com 6 2 5 3 1 8 4
    9. 9. Deleting a node in BST <ul><li>Delete(2): copy data from inorder successor </li></ul> http://ecomputernotes.com 6 2 5 3 1 8 4 6 3 5 3 1 8 4
    10. 10. Deleting a node in BST <ul><li>Delete(2): remove the inorder successor </li></ul>  http://ecomputernotes.com 6 2 5 3 1 8 4 6 3 5 3 1 8 4 6 3 5 3 1 8 4
    11. 11. Deleting a node in BST <ul><li>Delete(2) </li></ul>  http://ecomputernotes.com 6 3 5 4 1 8 6 3 5 3 1 8 4
    12. 12. C++ code for delete <ul><li>‘ delete’ is C++ keyword. We will call our deleteNode routine remove. </li></ul><ul><li>Here is the C++ code for remove. </li></ul>http://ecomputernotes.com
    13. 13. C++ code for delete <ul><li>TreeNode<int>* remove(TreeNode<int>* tree, int info) </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<int>* t; </li></ul><ul><li>int cmp = info - *(tree->getInfo()); </li></ul><ul><li>if( cmp < 0 ){ </li></ul><ul><li>t = remove(tree->getLeft(), info); </li></ul><ul><li>tree->setLeft( t ); </li></ul><ul><li>} </li></ul><ul><li>else if( cmp > 0 ){ </li></ul><ul><li>t = remove(tree->getRight(), info); </li></ul><ul><li>tree->setRight( t ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    14. 14. C++ code for delete <ul><li>TreeNode<int>* remove(TreeNode<int>* tree, int info) </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<int>* t; </li></ul><ul><li>int cmp = info - *(tree->getInfo()); </li></ul><ul><li>if( cmp < 0 ){ </li></ul><ul><li>t = remove(tree->getLeft(), info); </li></ul><ul><li>tree->setLeft( t ); </li></ul><ul><li>} </li></ul><ul><li>else if( cmp > 0 ){ </li></ul><ul><li>t = remove(tree->getRight(), info); </li></ul><ul><li>tree->setRight( t ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    15. 15. C++ code for delete <ul><li>TreeNode<int>* remove(TreeNode<int>* tree, int info) </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<int>* t; </li></ul><ul><li>int cmp = info - *(tree->getInfo()); </li></ul><ul><li>if( cmp < 0 ){ </li></ul><ul><li>t = remove(tree->getLeft(), info); </li></ul><ul><li>tree->setLeft( t ); </li></ul><ul><li>} </li></ul><ul><li>else if( cmp > 0 ){ </li></ul><ul><li>t = remove(tree->getRight(), info); </li></ul><ul><li>tree->setRight( t ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    16. 16. C++ code for delete <ul><li>TreeNode<int>* remove(TreeNode<int>* tree, int info) </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<int>* t; </li></ul><ul><li>int cmp = info - *(tree->getInfo()); </li></ul><ul><li>if( cmp < 0 ){ </li></ul><ul><li>t = remove(tree->getLeft(), info); </li></ul><ul><li>tree->setLeft( t ); </li></ul><ul><li>} </li></ul><ul><li>else if( cmp > 0 ){ </li></ul><ul><li>t = remove(tree->getRight(), info); </li></ul><ul><li>tree->setRight( t ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    17. 17. C++ code for delete <ul><li>//two children, replace with inorder successor </li></ul><ul><li>else if(tree->getLeft() != NULL </li></ul><ul><li>&& tree->getRight() != NULL ){ </li></ul><ul><li>TreeNode<int>* minNode; </li></ul><ul><li>minNode = findMin(tree->getRight()); </li></ul><ul><li>tree->setInfo( minNode->getInfo() ); </li></ul><ul><li>t = remove(tree->getRight(), </li></ul><ul><li>*(minNode->getInfo())); </li></ul><ul><li>tree->setRight( t ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    18. 18. C++ code for delete <ul><li>//two children, replace with inorder successor </li></ul><ul><li>else if(tree->getLeft() != NULL </li></ul><ul><li>&& tree->getRight() != NULL ){ </li></ul><ul><li>TreeNode<int>* minNode; </li></ul><ul><li>minNode = findMin(tree->getRight()); </li></ul><ul><li>tree->setInfo( minNode->getInfo() ); </li></ul><ul><li>t = remove(tree->getRight(), </li></ul><ul><li>*(minNode->getInfo())); </li></ul><ul><li>tree->setRight( t ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    19. 19. C++ code for delete <ul><li>//two children, replace with inorder successor </li></ul><ul><li>else if(tree->getLeft() != NULL </li></ul><ul><li>&& tree->getRight() != NULL ){ </li></ul><ul><li>TreeNode<int>* minNode; </li></ul><ul><li>minNode = findMin(tree->getRight()); </li></ul><ul><li>tree->setInfo( minNode->getInfo() ); </li></ul><ul><li>t = remove(tree->getRight(), </li></ul><ul><li>*(minNode->getInfo())); </li></ul><ul><li>tree->setRight( t ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    20. 20. C++ code for delete <ul><li>//two children, replace with inorder successor </li></ul><ul><li>else if(tree->getLeft() != NULL </li></ul><ul><li>&& tree->getRight() != NULL ){ </li></ul><ul><li>TreeNode<int>* minNode; </li></ul><ul><li>minNode = findMin(tree->getRight()); </li></ul><ul><li>tree->setInfo( minNode->getInfo() ); </li></ul><ul><li>t = remove(tree->getRight(), </li></ul><ul><li>*(minNode->getInfo())); </li></ul><ul><li>tree->setRight( t ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    21. 21. C++ code for delete <ul><li>//two children, replace with inorder successor </li></ul><ul><li>else if(tree->getLeft() != NULL </li></ul><ul><li>&& tree->getRight() != NULL ){ </li></ul><ul><li>TreeNode<int>* minNode; </li></ul><ul><li>minNode = findMin(tree->getRight()); </li></ul><ul><li>tree->setInfo( minNode->getInfo() ); </li></ul><ul><li>t = remove(tree->getRight(), </li></ul><ul><li>*(minNode->getInfo())); </li></ul><ul><li>tree->setRight( t ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    22. 22. C++ code for delete <ul><li>else { // case 1 </li></ul><ul><li>TreeNode<int>* nodeToDelete = tree; </li></ul><ul><li>if( tree->getLeft() == NULL ) // will handle 0 children </li></ul><ul><li>tree = tree->getRight(); </li></ul><ul><li>else if( tree->getRight() == NULL ) </li></ul><ul><li>tree = tree->getLeft(); </li></ul><ul><li>else tree = NULL; </li></ul><ul><li>delete nodeToDelete; </li></ul><ul><li>} </li></ul><ul><li>return tree; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    23. 23. C++ code for delete <ul><li>else { // case 1 </li></ul><ul><li>TreeNode<int>* nodeToDelete = tree; </li></ul><ul><li>if( tree->getLeft() == NULL ) // will handle 0 children </li></ul><ul><li>tree = tree->getRight(); </li></ul><ul><li>else if( tree->getRight() == NULL ) </li></ul><ul><li>tree = tree->getLeft(); </li></ul><ul><li>else tree = NULL; </li></ul><ul><li>delete nodeToDelete; </li></ul><ul><li>} </li></ul><ul><li>return tree; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    24. 24. C++ code for delete <ul><li>else { // case 1 </li></ul><ul><li>TreeNode<int>* nodeToDelete = tree; </li></ul><ul><li>if( tree->getLeft() == NULL ) // will handle 0 children </li></ul><ul><li>tree = tree->getRight(); </li></ul><ul><li>else if( tree->getRight() == NULL ) </li></ul><ul><li>tree = tree->getLeft(); </li></ul><ul><li>else tree = NULL; </li></ul><ul><li>delete nodeToDelete; </li></ul><ul><li>} </li></ul><ul><li>return tree; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    25. 25. C++ code for delete <ul><li>TreeNode<int>* findMin(TreeNode<int>* tree) </li></ul><ul><li>{ </li></ul><ul><li>if( tree == NULL ) </li></ul><ul><li>return NULL; </li></ul><ul><li>if( tree->getLeft() == NULL ) </li></ul><ul><li>return tree; // this is it. </li></ul><ul><li>return findMin( tree->getLeft() ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    26. 26. C++ code for delete <ul><li>TreeNode<int>* findMin(TreeNode<int>* tree) </li></ul><ul><li>{ </li></ul><ul><li>if( tree == NULL ) </li></ul><ul><li>return NULL; </li></ul><ul><li>if( tree->getLeft() == NULL ) </li></ul><ul><li>return tree; // this is it. </li></ul><ul><li>return findMin( tree->getLeft() ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    27. 27. C++ code for delete <ul><li>TreeNode<int>* findMin(TreeNode<int>* tree) </li></ul><ul><li>{ </li></ul><ul><li>if( tree == NULL ) </li></ul><ul><li>return NULL; </li></ul><ul><li>if( tree->getLeft() == NULL ) </li></ul><ul><li>return tree; // this is it. </li></ul><ul><li>return findMin( tree->getLeft() ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    28. 28. BinarySearchTree.h <ul><li>Let us design the BinarySearchTree class (factory). </li></ul>http://ecomputernotes.com
    29. 29. BinarySearchTree.h <ul><li>#ifndef _BINARY_SEARCH_TREE_H_ </li></ul><ul><li>#define _BINARY_SEARCH_TREE_H_ </li></ul><ul><li>#include <iostream.h> // For NULL </li></ul><ul><li>// Binary node and forward declaration </li></ul><ul><li>template <class EType> </li></ul><ul><li>class BinarySearchTree; </li></ul>http://ecomputernotes.com 
    30. 30. BinarySearchTree.h <ul><li>#ifndef _BINARY_SEARCH_TREE_H_ </li></ul><ul><li>#define _BINARY_SEARCH_TREE_H_ </li></ul><ul><li>#include <iostream.h> // For NULL </li></ul><ul><li>// Binary node and forward declaration </li></ul><ul><li>template <class EType> </li></ul><ul><li>class BinarySearchTree; </li></ul>http://ecomputernotes.com 
    31. 31. BinarySearchTree.h <ul><li>template <class EType> </li></ul><ul><li>class BinaryNode </li></ul><ul><li>{ </li></ul><ul><li>EType element; </li></ul><ul><li>BinaryNode *left; </li></ul><ul><li>BinaryNode *right; </li></ul><ul><li>BinaryNode( const EType & theElement, </li></ul><ul><li>BinaryNode *lt, BinaryNode *rt ) </li></ul><ul><li>: element( theElement ), left( lt ), </li></ul><ul><li>right( rt ) { } </li></ul><ul><li>friend class BinarySearchTree<EType>; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    32. 32. BinarySearchTree.h <ul><li>template <class EType> </li></ul><ul><li>class BinaryNode </li></ul><ul><li>{ </li></ul><ul><li>EType element; </li></ul><ul><li>BinaryNode *left; </li></ul><ul><li>BinaryNode *right; </li></ul><ul><li>BinaryNode( const EType & theElement, </li></ul><ul><li>BinaryNode *lt, BinaryNode *rt ) </li></ul><ul><li>: element( theElement ), left( lt ), </li></ul><ul><li>right( rt ) { } </li></ul><ul><li>friend class BinarySearchTree<EType>; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    33. 33. BinarySearchTree.h <ul><li>template <class EType> </li></ul><ul><li>class BinaryNode </li></ul><ul><li>{ </li></ul><ul><li>EType element; </li></ul><ul><li>BinaryNode *left; </li></ul><ul><li>BinaryNode *right; </li></ul><ul><li>BinaryNode( const EType & theElement, </li></ul><ul><li>BinaryNode *lt, BinaryNode *rt ) </li></ul><ul><li>: element( theElement ), left( lt ), </li></ul><ul><li>right( rt ) { } </li></ul><ul><li>friend class BinarySearchTree<EType>; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    34. 34. BinarySearchTree.h <ul><li>template <class EType> </li></ul><ul><li>class BinaryNode </li></ul><ul><li>{ </li></ul><ul><li>EType element; </li></ul><ul><li>BinaryNode *left; </li></ul><ul><li>BinaryNode *right; </li></ul><ul><li>BinaryNode( const EType & theElement, </li></ul><ul><li>BinaryNode *lt, BinaryNode *rt ) </li></ul><ul><li>: element( theElement ), left( lt ), </li></ul><ul><li>right( rt ) { } </li></ul><ul><li>friend class BinarySearchTree<EType>; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    35. 35. BinarySearchTree.h <ul><li>template <class EType> </li></ul><ul><li>class BinarySearchTree </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>BinarySearchTree( const EType& notFound ); </li></ul><ul><li>BinarySearchTree( const BinarySearchTree& rhs ); </li></ul><ul><li>~BinarySearchTree( ); </li></ul><ul><li>const EType& findMin( ) const; </li></ul><ul><li>const EType& findMax( ) const; </li></ul><ul><li>const EType& find( const EType & x ) const; </li></ul><ul><li>bool isEmpty( ) const; </li></ul><ul><li>void printInorder( ) const; </li></ul>http://ecomputernotes.com
    36. 36. BinarySearchTree.h <ul><li>void insert( const EType& x ); </li></ul><ul><li>void remove( const EType& x ); </li></ul><ul><li>const BinarySearchTree & operator= </li></ul><ul><li>( const BinarySearchTree & rhs ); </li></ul>http://ecomputernotes.com
    37. 37. BinarySearchTree.h <ul><li>private: </li></ul><ul><li>BinaryNode<EType>* root; </li></ul><ul><li>// ITEM_NOT_FOUND object used to signal failed finds </li></ul><ul><li>const EType ITEM_NOT_FOUND; </li></ul><ul><li>const EType& elementAt( BinaryNode<EType>* t ); </li></ul><ul><li>void insert(const EType& x, BinaryNode<EType>* & t); </li></ul><ul><li>void remove(const EType& x, BinaryNode<EType>* & t); </li></ul><ul><li>BinaryNode<EType>* findMin(BinaryNode<EType>* t); BinaryNode<EType>* findMax(BinaryNode<EType>* t); </li></ul><ul><li>BinaryNode<EType>* find(const EType& x, BinaryNode<EType>* t ); </li></ul><ul><li>void makeEmpty(BinaryNode<EType>* & t); </li></ul><ul><li>void printInorder(BinaryNode<EType>* t); </li></ul><ul><li>}; </li></ul><ul><li>#endif </li></ul>http://ecomputernotes.com

    ×