Delete operation on binary search tree

In summary, the conversation discusses the implementation of a delete function for a Binary Search Tree (BST). The provided pseudocode only returns the child node of the node to be deleted, rather than actually deleting the node and its corresponding object. It is also mentioned that the pseudocode does not account for cases where a node has two children, and it is assumed that it is a sub-function that should be called by another function. Further research reveals that deleting a BST node only involves removing the node itself and reallocating its child sub-trees. The notes provided by the speaker describe this operation in detail, but do not mention a specific delete keyword or the need to re-point the pointers of the parent node.
  • #1
Defennder
Homework Helper
2,593
5
The following diagram is from the section in my notes titled "Operations on Binary Search Trees". Specifically it explains via pseudocode how to implement a delete function for a BST:

http://img410.imageshack.us/img410/4225/deletetreexz0.jpg

What I don't understand in the 2nd box is why "return T.left" is written. I thought the purpose of deleting a tree node in a BST would be to literally delete the object corresponding to the tree node. Instead the pseudocode appears to return the child node of the node to be deleted. Why is this done?
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
This is not a delete function, and it doesn't include the case where an element has two children. It appears to be a sub-function that should be called by another function that isn't described.
 
Last edited:
  • #3
Thanks for replying, Jeff. I did a search on Google and found the following:
http://www.cs.mcgill.ca/~cs251/OldCourses/1997/topic9/

It turns out that deleting a BST node entails removing only that node itself and not its child sub-trees as I had thought it would. The child sub-trees of that node-to-be deleted would then be re-allocated in the BST. I've uploaded the portion of my notes which describe this operation in full. But it's strange that there isn't any delete keyword which I had expected, nor is there any pseudo-code which explains that deleting a node would require "re-pointing" the pointers of the parent tree-node of the node-to-be-deleted.
 

Attachments

  • Notes.pdf
    54.8 KB · Views: 301
Last edited by a moderator:

Related to Delete operation on binary search tree

What is a binary search tree?

A binary search tree is a data structure used in computer science to store and organize data in a hierarchical way. It is made up of nodes, each containing a value and pointers to its left and right child nodes. The values in the left subtree are smaller than the value of the current node, while the values in the right subtree are larger.

What is the purpose of a delete operation on a binary search tree?

The purpose of a delete operation on a binary search tree is to remove a specific node from the tree while maintaining its structure and properties. This is necessary when a node is no longer needed or when the data it contains needs to be updated.

How does the delete operation work on a binary search tree?

The delete operation on a binary search tree involves three main steps: finding the node to be deleted, replacing it with its successor or predecessor (depending on the type of deletion), and adjusting the tree to maintain its properties. This may include shifting nodes and updating pointers.

What happens if the node to be deleted has child nodes?

If the node to be deleted has child nodes, the delete operation will replace it with its successor or predecessor. The successor is the smallest node in the right subtree, while the predecessor is the largest node in the left subtree. The child nodes of the deleted node will be moved accordingly.

What is the time complexity of a delete operation on a binary search tree?

The time complexity of a delete operation on a binary search tree is O(log n), where n is the number of nodes in the tree. This is because the algorithm involves finding the node to be deleted, which can be done in O(log n) time using the binary search property, and then adjusting the tree, which also takes O(log n) time in the worst case.

Similar threads

Replies
2
Views
1K
Replies
3
Views
3K
Replies
7
Views
75K
Replies
6
Views
2K
Back
Top