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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:38:08+00:00 2026-06-11T14:38:08+00:00

I implemented a Priority Queue using a BST. Its output is not correct. Output:

  • 0

I implemented a Priority Queue using a BST. Its output is not correct.

Output:

Enter number of elements: 9
Enter number 1 of 9: 8
Enter number 2 of 9: 10
Enter number 3 of 9: 14
Enter number 4 of 9: 13
Enter number 5 of 9: 3
Enter number 6 of 9: 6
Enter number 7 of 9: 7
Enter number 8 of 9: 4
Enter number 9 of 9: 1
Outputting number 1 of 9: 14
Outputting number 2 of 9: 13
Outputting number 3 of 9: 10
Outputting number 4 of 9: 3
Outputting number 5 of 9: 3
Outputting number 6 of 9: 3
Outputting number 7 of 9: 3
Outputting number 8 of 9: 3
Outputting number 9 of 9: 3
Press any key to continue . . .

Test.cpp

//Arkadiy Vasilkovskiy 832a1
#include <iostream>
#include "CTree.h"
#include "PriorityQueueBST.h"
using namespace std;

int main()
{

    int num, input, output;
    cout << "Enter number of elements: ";
    cin >> num;
    PriorityQueueBST p;
    for (int x = 0; x < num; x++)
    {
        cout << "Enter number " << x + 1  
            << " of " << num << ": ";
        cin >> input;
        p.Enqueue(input);
    }

    for (int y = 0; y < num; y++)
    {
        cout << "Outputting number " << y + 1  
            << " of " << num << ": ";
        if(p.IsEmpty())
        {
            break; //we are done (this is an error!)
        }

        output = p.Dequeue();
        cout << output << endl;
    }

    system("pause");
    return 0;
    //CTree* tr = new CTree();
    //
    //for (int i = 0; i < 3; i++)
    //  tr->Add();

    //tr->View();
    //system("pause");


    //return 0;
}

BST Declaration File

struct TreeNode
{
    int info;
    TreeNode* leftLink;
    TreeNode* rightLink;
};


class CTree
{
public:
    CTree();
    ~CTree();
    void Add(int);
    void View();
    bool IsEmpty();
    int popLargest(TreeNode*);
    TreeNode *tree;


private:    
    void AddItem( TreeNode*&, TreeNode*);
    void DisplayTree(TreeNode*);
    void Retrieve(TreeNode*&, TreeNode*,bool&);
    void Destroy(TreeNode*&);
};

BST Implementation FIle

#include <iostream>
#include <string>

using namespace std;

#include "CTree.h"

CTree::CTree()
{
    tree = NULL;
}

CTree::~CTree()
{
    Destroy(tree);
}

void CTree::Destroy(TreeNode*& tree)
{
    if (tree != NULL)
    {
    Destroy(tree->leftLink);
    Destroy(tree->rightLink);
    delete tree;
    }
}


bool CTree::IsEmpty()
{
    if(tree == NULL) 
    {
        return true;
    }
    else
    {
        return false;
    }
}

void CTree::Add(int dataToEnter)
{
    TreeNode* newPerson = new TreeNode();
    /*cout << "Enter the person's name: ";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cin.getline(newPerson->name, 20);*/
    //cout << "Enter the person's contribution: ";
    //cin >> newPerson->info;
    /*bool found = false;*/

    newPerson->info = dataToEnter;
    newPerson->leftLink = NULL;
    newPerson->rightLink = NULL;

    /*Retrieve(tree, newPerson, found);
     if (found)
         cout << "info allready entered\n";
     else*/
         AddItem(tree, newPerson);
}

void CTree::View()
{
    if (IsEmpty())
    {
        cout<<"The list is empy";
    }
    else
    {
        DisplayTree(tree);

    }

};

void CTree::AddItem( TreeNode*& ptr, TreeNode* newPer )
{
        if (ptr == NULL)
        {
            ptr = newPer;
        }
        else if ( newPer->info < ptr->info)
            AddItem(ptr->leftLink, newPer); 
        else
            AddItem(ptr->rightLink, newPer); 
}
void CTree::DisplayTree(TreeNode* ptr)
{
    if (ptr == NULL)
                    return;
    DisplayTree(ptr->rightLink);
    cout << ptr->info << endl; //cout<<ptr->name<<" "<<"$"<<ptr->info <<endl;
    DisplayTree(ptr->leftLink); 
}
void CTree::Retrieve(TreeNode*& ptr, TreeNode* newPer, bool& found)
{
    {
        if (ptr == NULL)
        {
            found = false; // item is not found.
        }
        else if ( newPer->info < ptr->info)
        {
            Retrieve(ptr->leftLink, newPer, found);
        }
             // Search left subtree.
        else if (newPer->info > ptr->info)
        {
            Retrieve(ptr->rightLink, newPer, found);// Search right subtree.
        }   
        else
        {
            //newPer.info = ptr->info; // item is found.
            found = true;
        }
    }
}

int CTree::popLargest(TreeNode* tr)
{
    //Failed Attempt at returning one value at a time and deleting that node
    int largest; // = tr->info;
    TreeNode* prev = NULL;
    TreeNode* cur = tr;

    if(tr != NULL)
        largest = tr->info;

    while (cur->rightLink != NULL)
    {
        prev = cur;
        cur = cur->rightLink;
        largest = cur->info;
        //DeleteAttemptTwo(tr, largest);//DeleteItem(largest);  

    }

    /*if (prev != NULL)
    {
        prev->rightLink = cur->leftLink;
    }*/

    if(prev != NULL)
    {
        if (cur->leftLink != NULL)
        {
            prev->rightLink = cur->leftLink;
        }
        else 
        {
            prev->rightLink = NULL;
        }
    }
    else if (cur->leftLink != NULL)
    {
        largest = cur->leftLink->info;
    }

    return largest;
}

Priority Queue Declaration File

#ifndef PRIORITYQUEUESLL__H
#define PRIORITYQUEUESLL__H

class PriorityQueueBST
{
    public:
        PriorityQueueBST();
        ~PriorityQueueBST();
        void Enqueue(int);
        int Dequeue();
        bool IsEmpty();

    private:
        CTree* ourTree;
};

#endif

Priority Queue Implementation File

#include <iostream>
using namespace std;
#include "CTree.h"
#include "PriorityQueueBST.h"

PriorityQueueBST::PriorityQueueBST()
{
    ourTree = new CTree();
    //head = NULL;
}

PriorityQueueBST::~PriorityQueueBST()
{

}

void PriorityQueueBST::Enqueue(int dataToEnter)
{
    ourTree->Add(dataToEnter);
}

int PriorityQueueBST::Dequeue()
{
    int largest = ourTree->popLargest(ourTree->tree);
    return largest;
}

bool PriorityQueueBST::IsEmpty()
{
    return ourTree->IsEmpty();

}
  • 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-11T14:38:09+00:00Added an answer on June 11, 2026 at 2:38 pm

    I ran your code and looked at what was happening in the debugger. As it turns out your tree is being corrupted when you pop the numbers out of it. I didn’t look into exactly how it was happening but basically. Before the first few calls to pop largest your tree looked like:

               8
        3            10
    1      6              14
         4   7         13
    

    And after you pop 14, 13, 10 it became:

               8
        3
    1      6
         4   7
    

    Now if I understood your code correctly the error in logic is here:

    if(prev != NULL)
    {
        if (cur->leftLink != NULL)
        {
            prev->rightLink = cur->leftLink;
        }
        else 
        {
            prev->rightLink = NULL;
        }
    }
    else if (cur->leftLink != NULL)
    {
        largest = cur->leftLink->info;
    }
    

    Now if you notice in the case that the highest number is the root of the tree (meaning prev == NULL), in this case 8, you return the wrong value. You say the highest value is the left link but it isn’t it’s the root node’s value. Wost, you never take it out of the tree, so you’re stuck giving the same answer all the time.

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

Sidebar

Related Questions

Based on Rob Pike's load balancer demo , I implemented my own priority queue,
I need to implement a priority queue in C programming using singly linked list.
In the big picture, I'm trying to implement Dijkstra's algorithm using a priority queue.
I implemented a IEqualityComparer<MyObject> for MyObject in order for my priority queue to be
I have implemented Priority Queue interface for making heap. Can you tell me how
I have to implement Priority Queue using MultiMap. I use MultiMap from Google Collections.
I'm trying to implement a templated priority queue using a heap to process frequencies
I implemented some code using backbone.js in Asp.NEt MVC3 and found backbone.js very helpful.
I wrote an implementation for a priority queue I needed, and now I would
I'm trying to implement a simple priority queue from array of queues. I'm trying

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.