Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 617609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:27:16+00:00 2026-05-13T18:27:16+00:00

I’m a having a little trouble thinking how the hell do I fix the

  • 0

I’m a having a little trouble thinking how the hell do I fix the appropriate pointers when trying to delete a node from a binary tree where that node has 2 children.

I understand the basic concept and I’m basically just having trouble fixing the pointers…

So, basically, my delete function is mostly done and every case is already working (as far as my extensive testing go, everything worked OK), I’m only missing the case node with 2 children. Let’s say we are inside the else if that deals with that case:

I will have 2 pointers there, currPtr which holds the node to be deleted, prevPtr which holds the parent node. I also have a variable named fromLeft which defines if the currPtr parent comes from the left or right pointer on prevPtr. Then, I have a call to another function named extractMin(Tree *t) which extracts the lowest value in the tree. If I call this function with currPtr->right as argument, the successor of currPtr is going to be extracted from the tree (the function will deleted it from tree, fix the pointers and return the node pointer). The successor pointer is going to be saved on tempPtr.

Here’s the structure of my tree:

typedef int TreeElement;

typedef struct sTree {
   TreeElement item;

   struct sTree *left;
   struct sTree *right;
} Tree;

And the code for the extract function:

Tree *extractMin(Tree *tree) {
    Tree *prevPtr = NULL;
    Tree *currPtr = tree;

    while(currPtr->left) {
        prevPtr = currPtr;
        currPtr = currPtr->left;
    }

    if(prevPtr) prevPtr->left = currPtr->right;

    // inorder successor
    return currPtr;
}

The code above is missing the case here the tree only has one node, the root node, it won’t work properly and it also doesn’t check if the tree has any nodes at all but, it works when a tree has a few nodes.

So, how do I fix the necessary pointers on the else if for the node deletion? Also, remember that the left and right pointers on the tree nodes will always be pointing somewhere or be NULL.

By the way, I want to do this iterative.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:27:16+00:00Added an answer on May 13, 2026 at 6:27 pm

    Updated: So you want to keep the ordering of the nodes, by replacing the node with its direct inorder successor or predecessor.

    Let’s suppose the tree below is ordered. The order of the nodes is:

    H < D < I < B < J < E < K < A < F < M < C < N < G < O
    

    You want to delete a node (A) which has two children. You pull up either its inorder predecessor (K) or successor (F) child of the node in place of the original. Let’s pick the successor. This is how you find it: you traverse the left children of C until you find one which has no left child; this is the direct inorder successor of A.

           A
       B       C
     D   E   F   G
    H I J K   M N O
    

    So F is pulled up, and the left subtree of A is not touched. However, now M should be pulled up as well, to become the left child of C (this is done in your extractMin()).

    After all rearrangements, you get

           F
       B       C
     D   E   M   G
    H I J K     N O
    

    In code, this is my solution. I added a NULL check to extractMin() to simplify the caller code, so I don’t need else if.

    Updated extractMin() to cover the case when tree has no children.

    Tree *extractMin(Tree *parent, Tree *tree) {
        if (!tree) return NULL;
    
        Tree *prevPtr = NULL;
        Tree *currPtr = tree;
    
        while(currPtr->left) {
            prevPtr = currPtr;
            currPtr = currPtr->left;
        }
    
        if (prevPtr) {
            prevPtr->left = currPtr->right;
        } else if (parent) {
            parent->right = currPtr->right;
        }
    
        // inorder successor
        return currPtr;
    }
    
    // prevPtr is the parent, currPtr is the node to be deleted
    Tree *successor = extractMin(currPtr, currPtr->right);
    successor->left = currPtr->left;
    successor->right = currPtr->right;
    if (fromLeft) {
        prevPtr->left = successor;
    } else {
        prevPtr->right = successor;
    }
    // Now currPtr can be destroyed
    

    Note that this code is not tested, so I don’t guarantee anything 🙂

    Note that repeated deletes like this may make the tree unbalanced (that is, some paths will get much longer than the others). Binary sort trees do deletions this way, but also use rebalancing after to keep the tree close to the ideal (where each leaf is at the same level, like in my first example above).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 379k
  • Answers 379k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer After more searching I found http://cutycapt.sourceforge.net/ which is exactly what… May 14, 2026 at 9:31 pm
  • Editorial Team
    Editorial Team added an answer Which created_at do you want to sort on? When you're… May 14, 2026 at 9:31 pm
  • Editorial Team
    Editorial Team added an answer You can cast the "sender" parameter (cast to MenuItem) to… May 14, 2026 at 9:31 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.