MHB Deleting Leaf Nodes from an Ordered Binary Tree

AI Thread Summary
The discussion revolves around creating a recursive algorithm to delete the rightmost leaf child from each node in an ordered binary tree. The initial algorithm presented has several issues. The first point of concern is that the line "if (P->RC!=NULL) P=P->RC;" disrupts the recursive structure by preventing the left subtree from being processed. Additionally, the check for a NULL pointer in "if (P->LC!=NULL) Algorithm(P->LC);" is considered unnecessary, as the function already handles NULL checks. The line "if (P->RC==NULL) P->LC->RC=NULL;" raises confusion regarding its intended function, as it seems illogical to set a grandchild to NULL based on the absence of a right child. Finally, there is a note that proper deletion of nodes should involve a memory management function like free(). Overall, the algorithm needs significant revisions to achieve its intended purpose effectively.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
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
I think that there are some mistakes.. But, is it entirely wrong? (Thinking)
 
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)
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top