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

  • SEARCH
  • Home
  • 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 9258687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:28:15+00:00 2026-06-18T12:28:15+00:00

Can anyone explain to me why the delete_min() function does not work? I have

  • 0

Can anyone explain to me why the delete_min() function does not work? I have been trying to figure this out for hours now.

I have tried different variations of the function, but it seems to either delete the wrong thing, only alter data and not delete it, or delete the wrong thing. Also, the insertion function doesn’t always work properly after delete_min() is called, so I’m sure this has something to do with pointers.

Here is the function:

int BinaryTree::delete_min_helper(TreeNode *node){

   while(node->left != NULL){
      node = node->left;
   }

  if(node == root){
     puts("attempted to delete root");
     return 1;
  }

  delete node;
  node = NULL;
  return 0;
}

And here is a compilable example:

#ifndef _TREE_H_
#define _TREE_H_

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct TreeNode {
  int val;
  char *str;
  TreeNode *left;
  TreeNode *right;
};

class BinaryTree {
  public:

  BinaryTree();
  ~BinaryTree();

  int insert_node(unsigned int val, char *str);
  TreeNode *find_min();
  int delete_min();
  void print();

 private:

 int insert_node_helper(TreeNode *&node, unsigned int val, char *str);
 int delete_min_helper(TreeNode *node);
 void print_helper(TreeNode *node);

 TreeNode *root;  
};

#endif

BinaryTree::BinaryTree(){
  this->root = NULL;
}

BinaryTree::~BinaryTree(){
}

int BinaryTree::insert_node(unsigned int val, char *str){
  return insert_node_helper(this->root, val, str);
}

int BinaryTree::insert_node_helper(TreeNode *&node, unsigned int val, char *str){

  if(node == NULL){
    node = new TreeNode;
    node->val = val;
    node->str = strdup(str);
    node->left = NULL;
    node->right = NULL;
    if(node != NULL){
      return 0;
    }else{
      puts("inserted null node");
      return 1;
   }
 }else if(val <= node->val){
   return insert_node_helper(node->left, val, str);
 }else if(val > node->val){
   return insert_node_helper(node->right, val, str);
 }

 return 1;
}

void BinaryTree::print(){
  print_helper(this->root);
}


void BinaryTree::print_helper(TreeNode *node){

    if(node != NULL){
      print_helper(node->right);
      printf("%d occurrences of \"%s\"\n", node->val, node->str);
      print_helper(node->left);
    }

}

TreeNode *BinaryTree::find_min(){
  TreeNode *temp = this->root;
  while(temp->left != NULL){
    temp = temp->left;
  }
  return temp;
}

int BinaryTree::delete_min(){
   return delete_min_helper(root);
}

int BinaryTree::delete_min_helper(TreeNode *node){

   while(node->left != NULL){
     node = node->left;
   }

  if(node == root){
    puts("attempted to delete root");
    return 1;
  }

  delete node;
  node = NULL;
  return 0;
}

#include <time.h>

int main(){

  BinaryTree bt;

  srand(time(NULL));
  for(int i = 0; i < 10; i++){
     bt.insert_node(rand() % 20, "test");
  }

  bt.print();
  printf("min val = %d\n", bt.find_min()->val);
  puts("#################################");
  bt.delete_min();
  bt.print();
  printf("min val = %d\n", bt.find_min()->val);

  return 0;
}
  • 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-06-18T12:28:16+00:00Added an answer on June 18, 2026 at 12:28 pm

    Two things come to mind right away:

    1. a parent node is still pointing at the node which you deleted

    2. the node you’re deleting might have a node->right pointer hanging off of it, which you are lopping off the tree, instead of re-hooking it back into the tree.

    There might be more problems, but you need to address these issues at a bare minimum.

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

Sidebar

Related Questions

Can anyone explain to me why this code works (why does it return a
Can anyone explain me how this query will work and find nth highest salary
Can anyone explain to me why the following xslt: <xsl:if test=EventDocument> Does not pickup
Can anyone explain to me why this program: for(float i = -1; i <
Can anyone explain this code to me? Whats the meaning and how to use
Can anyone explain, if Struts Action classes are threadsafe in Struts 1.x. Does RequestProcessor
Can anyone explain why this code: {% form_theme form _self %} {% block avo_gallery_upload_widget
Can anyone explain from this simple login script why the session seems to delete
I not family with PL/SQL. Can anyone explain why I can't do the following?
Can anyone explain why here a = [] ? 1 : 2 a will

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.