How does the delete function work in a binary search tree?

  • Thread starter abramhum.c.l
  • Start date
  • Tags
    Delete
In summary: That's because when search is invoked, it will return the value of the right child of node #3, which is &node #2 in this case. So, when delete is called, it will actually delete B from the tree.
  • #1
abramhum.c.l
6
0
Hi:
I was in a maze about delete in binary search tree. The followings
is its codes:
Code:
void delete(struct bst_node** node) {
    struct bst_node* old_node = *node;
    if ((*node)->left == NULL) {
        *node = (*node)->right;
        free_node(old_node);
    } else if ((*node)->right == NULL) {
        *node = (*node)->left;
        free_node(old_node);
    } else {
        delete node with two children
    }
}

This problem is from the current node and its parents. In the above codes, obviously,
I cannot image how the parent node connect to the current node's right child when if (*node)->left == NULL; In theoretically, to find out the parent node, I should write codes like following:
Code:
struct bst_node* p;
if(current_node!=null)
{
p=current_node; current_node=current->right;
}
and then when delete the current, the function will connect the parent node's right child to
current node's right child. However, the codes in top section not including this part, I didn't
know why and how it work. Any instruction is appreciated, thanks.
 
Technology news on Phys.org
  • #2


Hey abramhum.c.l.

Do you have code for free_node()?

My guess is this command recursively deletes all children which means they all get freed from memory and the parent node sets whatever child pointer to null (as it does in the branch statement).
 
  • #3


Not exactly, this is a part of BST, and its purpose is to delete the
node that user designated.
The problem make me feel puzzled is the process of recursive.
for example, if these exist four nodes like A, B,C, D; A is the parents
of B, and C is the right child of B, and then D is the left child of C.
If we want to delete B, then in BST algorithm, since the left child of
B is null, it will find C first, and then connect A to C, make the C become
the right child of A, and then delete B.

But in the code snippet I posted, i don't know how it find out the A,
and it seems make no sense.
Thanks a lot
 
  • #4


abramhum.c.l said:
But in the code snippet I posted, i don't know how it find out the A, and it seems make no sense.
The function names are misleading. It appears that for this case, you pass a pointer to A as a parameter to the delete() function, which then deletes one of the child nodes of A.

The code is missing is what is supposed to be done if A has two child nodes. What if each of those two child nodes also have two child nodes? This creates a dilemma because A would need 4 node links if it's two child nodes were deleted.
 
  • #5
I found the data from http://en.literateprograms.org/Binary_search_tree_(C))
In fact, there are many resources and data showing similar algorithm.
Thanks.
 
  • #6
abramhum.c.l said:
I found the data from http://en.literateprograms.org/Binary_search_tree_(C))
Three key points here:
1. The function delete takes a pointer to a pointer to the node to be deleted.
2. The input to delete is the output of a call to search.
3. It's the data that needs to be deleted from the tree, not necessarily the node itself.

Suppose a binary search tree whose data comprises single characters is created by inserting, in order, the letters D (in node #1), F (node #2), B (node #3), C (node #4), E (node #5), and A (node #6). Graphically, the tree will look like this:
Code:
         D
       /   \
      B     F
     / \   / 
    A   C E

The nodes will contain the following:
Code:
root: &node #1

node #1:
  data: D
  left: &node #3
  right: &node #2

node #2:
  data: F
  left: &node #5
  right: null

node #3:
  data: B
  left: &node #6
  right: &node #4

node #4:
  data: C
  left: null
  right: null

node #5:
  data: E
  left: null
  right: null

node #6:
  data: A
  left: null
  right: null

Suppose you want to delete the "A" from the tree. The things need to be done to accomplish this are:
- The left child of node #3 needs to be set to null, removing node #6 from the tree.
- Node #6 and the allocated data that it contains needs to be freed.

The way to do this is to call delete with a pointer to the left element of node #3. That is exactly what the function search will return when asked to search for "A". Deleting C or F from this tree is similarly easy. Deleting E also is surprisingly easy.

What's tricky is deleting B or D from the tree. One way to delete D from the tree is to make it look like
Code:
        C
       / \
      B   F
     /   /  
    A   E
This is what the code at the referenced site accomplishes.
 
  • #7


Hi:
DH, if I hope to delete the point F, based on the codes, it should perform the following
section:
Code:
void delete(struct bst_node** node) {
    struct bst_node* old_node = *node;
    ...
   if ((*node)->right == NULL) {
        *node = (*node)->left;
        free_node(old_node);
    }

since old_node is the original one, like F, it's left child should be E, however, how to
find its parents D, *node = (*node)->left; this clause seems to make F to E, but, in the finally state, the D's left child should be E. I can not figure out how this be done on the upper code snippet. That's why I feel confused. Thanks a lot.
 
  • #8


The function delete doesn't need to find the parent of the node containing F.

The function will change something in that parent node. In this case is will reset the right child element of that parent node to point to the node containing E. So how can delete change the contents of that right child element given that there's no way to traverse up the tree? The answer is simple: What you must provide as input to delete is the address of that right child element. This is exactly what the function search will return when asked to find "F". The key to this whole puzzle is that delete expects to be fed the output of the search function.So what if you want to delete D from the tree rather than F? In this case, the function search will return a pointer to the root pointer. What this version of a binary search tree does is to
  1. Find the pointer to the node that contains the immediate predecessor to D. In this case, that node is node #4, and the thing that points to it is node #3's right child element.
  2. Swap the data contents of node #1 and node #4. This step makes the tree look like this (which violates the heap principle):
    Code:
             C
           /   \
          B     F
         / \   / 
        A   D E
  3. The above step makes the tree not be a tree; it violates the heap principle. The way to fix this problem, and to simultaneous achieve the goal of deleting "D" from the tree is to recursively call delete with the pointer from step 1 (the pointer to the right child element of node #3, which points to node #4). This recursive call to delete will make the tree look like this:
    Code:
            C
           / \
          B   F
         /   /  
        A   E
 
  • #9


I still feel confused about delete F. I think it is somehow the process
like link list, but seems something different. In link-list, if I hope to delete
a node, like B in this case: A->B->C, if the structure is following:
Code:
struct list_node {
    void* data;
    struct bst_node* right;
};

what I need to do is set A->right=B->right; and then delete B.
But the process which in deleting F in BST is different.
What is the theorem that cause the algorithm can reduce the above process.
Thanks a lot.
 
  • #10


There's not much more that I can say without repeating myself other than to execute the code by hand or to execute the code in a debugger.
 
  • #12
Did you walk through the code, step by step, with a debugger?

Better yet, execute the code by hand: act as if YOU are the computer. It's the best way to understand what is going on.

This code is a bit different than the first one you find. The first version required the caller to pass the correct handle, a pointer to a pointer, to insert and delete. This new version doesn't use handles, but instead requires that the outside caller reassign the root pointer to the result returned by insert and delete.
 

1. What is a BST (Binary Search Tree)?

A BST, or Binary Search Tree, is a data structure used in computer science to organize data in a hierarchical way. It consists of nodes that are arranged in a specific order, with each node having at most two child nodes. The left child node contains a value that is less than the parent node, while the right child node contains a value that is greater than the parent node. This structure allows for efficient searching, inserting, and deleting of data.

2. How do you insert data into a BST?

To insert data into a BST, you start at the root node and compare the value of the new data with the value of the current node. If the new data is less than the current node, you move to the left child node. If the new data is greater than the current node, you move to the right child node. This process is repeated until you reach a node with no child nodes, where you insert the new data as a child node.

3. What is the time complexity for deleting a node in a BST?

The time complexity for deleting a node in a BST is O(log n), where n is the number of nodes in the tree. This is because the search for the node to be deleted follows the same process as searching for a node to insert, and then the deletion itself takes constant time.

4. What are the different types of deletion in a BST?

There are three types of deletion in a BST: leaf node deletion, single child node deletion, and two child nodes deletion. Leaf node deletion occurs when the node to be deleted has no child nodes. Single child node deletion occurs when the node to be deleted has only one child node. Two child nodes deletion occurs when the node to be deleted has two child nodes. Each type of deletion has a different process, but the overall structure of the BST remains the same.

5. How do you handle deleting a node with two child nodes in a BST?

When deleting a node with two child nodes in a BST, you need to find the node with the next highest value to replace the deleted node. This can be done by finding the leftmost node in the right subtree of the node to be deleted. This node will have no left child nodes, so it can be easily deleted and used to replace the deleted node. If the node to be deleted has two child nodes, you can also choose to find the rightmost node in the left subtree to replace the deleted node. After replacing the deleted node, the new node's child nodes will need to be reassigned to maintain the BST structure.

Similar threads

  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
20
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top