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 8792327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:57:50+00:00 2026-06-13T22:57:50+00:00

I have finally gotten all of my functions to work after testing the insert

  • 0

I have finally gotten all of my functions to work after testing the insert function and the swapSubTrees and printTree functions. Now I need to create a Binary Tree from a file and I get the file to open and read the first integer of the file but then my program crashes.

There is a ton of a code and I do not want to make a wall of code. I will post my important parts and if required will supply the rest of the code if asked for.

My function to swap and print are:

template<class elemType>
void bSearchTreeType<elemType>::printTree()
{
printTree(root);
}
template<class elemType>
void bSearchTreeType<elemType>::printTree(nodeType<elemType> *p)
{
if(p != NULL)
cout << p->info << endl;
printTree(p->lLink);
printTree(p->rLink);
}

template<class elemType>
void bSearchTreeType<elemType>::swapSubtrees(nodeType<elemType> * p)
{

 if (p != NULL)
  {

   if (p->lLink != NULL && p->rLink != NULL)
    {

      nodeType<elemType> * temp = p->lLink;
      p->lLink = p->rLink;
      p->rLink = temp;
      delete temp;
    }

  if (p->lLink != NULL && p->rLink == NULL)
   {
    swapSubtrees(p->lLink);
   }

 if (p->rLink != NULL && p->lLink == NULL)
  {
   swapSubtrees(p->rLink);
  }

 }
}

My main program is :

#include<iostream>
#include<fstream>
#include<cstdlib>
#include "binarySearchTree.h"

using namespace std;

int main()
{
bSearchTreeType<int>bt;
ifstream infile;

infile.open("binaryTree.txt");

if(!infile){
    cout<<"File not found"<<endl;
}

int tree;

while(infile>> tree)
{
    bt.insert(tree);
}

bt.swapSubtrees();

bt.printTree();
bt.swapSubtrees();

system("PAUSE");
return 0;

  }

The program completely compiles and runs and starts to print the list starting with 12:

This is contents of binaryTree.txt:
12 23 56 45 78 89 98 25 36 65 54

I am a little confused as why it keeps crashing. Any thoughts?
The program was not stable when I manually built the list. After the last item was printed the program would hang. Now it hangs when trying to print. i think it is a problem with the function.

Much Appreciated

  • 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-13T22:57:51+00:00Added an answer on June 13, 2026 at 10:57 pm
    if (p->lLink != NULL && p->rLink != NULL)
       {
    
     nodeType<elemType> * temp = p->lLink;
      p->lLink = p->rLink;
      p->rLink = temp;
      delete temp;
    }
    

    You’re deleting a pointer that was pointing to lLink…. that would mean you’re deleting the entire lLink and presumably trying to reaccess it somewhere else?

    The way to handle this is to just set temp to null instead of deleting. But it doesn’t really matter since temp is a local variable inside of that block which goes out of scope right after temp is set to null.

    One other potential area that is problematic in print:-

       if(p != NULL)
        cout << p->info << endl;
        printTree(p->lLink);
        printTree(p->rLink);
    

    you need braces to encompass more than one statement in the body of an if statement…

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

Sidebar

Related Questions

I have finally gotten the ReportViewer to work in Local mode. Now I would
I have finally gotten my login application to function with my online database. I
Had finally gotten all the bugs out of this and now they said Oh,
I have finally gotten my first app to work properly and I'm attempting to
So after a lot of hard (and fun) work in development I’ve finally gotten
I have finally started messing around with creating some apps that work with RESTful
After searching through some existing libraries for JSON, I have finally ended up with
So, I've finally made the plunge, and have gotten to the state where I'm
I have finally managed to launch my app after many days of trying but
I'm still writing my financial software :P And have finally gotten a reliable real

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.