Deleting Leaf Nodes from an Ordered Binary Tree

In summary, the conversation discusses an algorithm for deleting the rightmost child that is a leaf from each node in a binary tree. There are some potential mistakes in the provided algorithm, such as missing recursive calls and a potentially incorrect step for setting a child to NULL. The implementation of the algorithm also does not involve using the free() function, which may be necessary for properly deleting a node.
  • #1
evinda
Gold Member
MHB
3,836
0
Hi! (Nerd)

Given a tree, I want to write an algorithm, that deletes from each node, from the corresponding ordered binary tree, the rightmost child, that is a leaf.

That's what I have tried:

Code:
Algorithm(NODE *P){
   if (P==NULL) return error;
   if (P->RC!=NULL) P=P->RC;
   if (P->LC!=NULL) Algorithm(P->LC);
   if (P->RC==NULL) P->LC->RC=NULL;
   Algorithm(P->RC)

Could you tell me if it is right? :confused:
 
Last edited:
Technology news on Phys.org
  • #2
I think that there are some mistakes.. But, is it entirely wrong? (Thinking)
 
  • #3
evinda said:
Given a tree, I want to write an algorithm, that deletes from each node, from the corresponding ordered binary tree, the rightmost child, that is a leaf.
Code:
Algorithm(NODE *P){
   if (P==NULL) return error;
   if (P->RC!=NULL) P=P->RC;
   if (P->LC!=NULL) Algorithm(P->LC);
   if (P->RC==NULL) P->LC->RC=NULL;
   Algorithm(P->RC)

evinda said:
I think that there are some mistakes.. But, is it entirely wrong? (Thinking)

Hi! (Happy)

You have the set up for a recursive algorithm, which includes a check for the final condition, and recursive calls.
Good! (Smile)

Code:
   if (P==NULL) return error;

What is the reason that you think this results in an error? (Wondering)
Code:
   if (P->RC!=NULL) P=P->RC;

This seems to break the set up of the recursive function call.
As a result the left sub tree will not be visited any more.
Should it be like that? (Wondering)
Code:
   if (P->LC!=NULL) Algorithm(P->LC);

The check for a NULL pointer seems redundant, since we'll check that in the function anyway. (Nerd)
Code:
   if (P->RC==NULL) P->LC->RC=NULL;

What should this do? :confused:

If there is no child on the right side, set the grand child on the left side to NULL?
That doesn't seem to make sense. (Worried)

Btw, I think deleting a node should involve calling [m]free()[/m]. (Nerd)
 

FAQ: Deleting Leaf Nodes from an Ordered Binary Tree

What is an Ordered Binary Tree?

An Ordered Binary Tree is a data structure in computer science that is composed of nodes, where each node has at most two child nodes and the values of the left child node are smaller than the value of the parent node, while the values of the right child node are larger than the value of the parent node. This structure allows for efficient searching and sorting of data.

Why would you want to delete leaf nodes from an Ordered Binary Tree?

Deleting leaf nodes from an Ordered Binary Tree can help to optimize the structure and improve the efficiency of searches and other operations. It can also help to reduce the memory usage of the tree.

How do you delete leaf nodes from an Ordered Binary Tree?

To delete leaf nodes from an Ordered Binary Tree, you would need to traverse the tree and identify the leaf nodes. Then, you can remove these nodes by reassigning the parent node's child pointer to null. This process may need to be repeated until all desired leaf nodes are deleted.

Are there any potential drawbacks to deleting leaf nodes from an Ordered Binary Tree?

One potential drawback is that deleting leaf nodes can change the structure of the tree and potentially affect the order of the remaining nodes. This could impact the efficiency of future operations on the tree.

What are some alternative methods to deleting leaf nodes from an Ordered Binary Tree?

Instead of deleting leaf nodes, you could also consider pruning the tree by removing entire branches or reorganizing the tree to balance it. These methods may also help to optimize the structure and improve efficiency.

Similar threads

Replies
1
Views
1K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
4
Views
1K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
2K
Back
Top