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)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top