Deleting Leaf Nodes from an Ordered Binary Tree

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Binary Nodes Tree
Click For Summary
SUMMARY

The discussion focuses on creating a recursive algorithm to delete the rightmost leaf child from each node in an ordered binary tree. The proposed algorithm contains several logical errors, particularly in how it navigates the tree and handles NULL pointers. Key issues include breaking the recursive structure by not visiting the left subtree and mismanaging the deletion process, which should involve proper memory management using free(). The participants agree that the algorithm needs significant revisions to function correctly.

PREREQUISITES
  • Understanding of recursive algorithms
  • Familiarity with binary tree data structures
  • Knowledge of memory management in C/C++ (e.g., using free())
  • Experience with pointer manipulation in programming
NEXT STEPS
  • Review recursive algorithm design patterns
  • Study binary tree traversal techniques
  • Learn about memory management in C/C++
  • Implement and test a corrected version of the leaf deletion algorithm
USEFUL FOR

Software developers, computer science students, and anyone interested in improving their understanding of binary trees and recursive algorithms.

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)
 

Similar threads

Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 20 ·
Replies
20
Views
5K