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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:52:31+00:00 2026-05-18T00:52:31+00:00

I’m working on a project where I have to make a binary search tree

  • 0

I’m working on a project where I have to make a binary search tree that stores strings and takes account of doubles. While i’ve already tackled the specifics, I can’t for the life of me get this damn insert function to work. It seems to only store the root node, leaving it’s “children” NULL even though it does actually seem to assign the left and right pointers to new nodes. However when I attempt to output it, only the main parent (root) node exists. I guess the changes do not get saved for whatever reason.

Here’s the header file:

#ifndef BST_H
#define BST_H

#include <string>
#include <vector>
#include <iostream>
using namespace std; 

typedef string ItemType; 

class Node
{
 public:
 Node(); // constructor 
 ItemType data; // contains item
 Node* left; // points to left child
 Node* right;// points to right child
 int dataCount; // keeps track of repeats
 vector<int> lineNumber; // keeps track of line numbers 
};

class Tree
{
 public:
 Tree(); // constructor
// ~Tree(); // destructor. not working.
 bool isEmpty(); // tests for empty tree
 Node* find(Node* root, ItemType item); // finds an item
 void insert(Node* root, ItemType item, int lineN, Tree tree); // inserts an item
 void outputTree(Node* root); // lists all items in tree 
 void treeStats(Tree tree); // outputs tree stats 
 void clearTree(); // erases the tree (restart)
 Node* getRoot(); // returns the root
 void setRoot(Node*& root); 
// void getHeight(Tree *root); // gets height of tree

 private:
 Node* root; // root of tree
 int nodeCount; // number of nodes
};

#endif

cpp:

#include "BST.h"

bool setRootQ = true;

/** Node constructor- creates a node, sets children 
 *  to NULL and ups the count. */
Node::Node()
{
 left = right = NULL; 
 dataCount = 1;
}

/** Tree constructor- creates instance of tree
 *  and sets parameters to NULL */  
Tree::Tree()
{
 root = NULL; 
 nodeCount = 0; 
}

/** Destructor- deallocates tree/node data;
 *  avoids heap leaks. SOMETHING WRONG. CAUSES SEGFAULT
Tree::~Tree()
{
 if(this->root->left) // if a left child is present
 {
  delete this->root->left; //recursive call to destructor ("~Tree(->left)")
  this->root->left = NULL; 
 }
 if(this->root->right) // if a right child is present
 {
  delete this->root->right; //recursive call to destructor 
  this->root->right = NULL; 
 }
} */

/** Returns true if tree is empty. 
 *  Otherwise returns false (DUH). */
bool Tree::isEmpty()
{
 return root == NULL; 
}

/** Searches tree for item; returns the node if found
 * @param root- tree node.
 *   item- data to look for. */
Node* Tree::find(Node* root, ItemType item)
{
 if(root == NULL) // if empty node
 {
  return NULL;
 }
 else if(item == root->data) // if found
 {
  return root; 
 }
 else if(item < root->data) // if item is less than node 
 {
  find(root->left, item); 
 }
 else if(item > root->data) // if item is more than node
 {
  find(root->right, item); 
 }

 return NULL;
}

/** Adds a new node to the tree. If duplicate, increases count. 
 * @param item- data to insert.
 *   root- tree node/ */
void Tree::insert(Node* root, ItemType item, int lineN, Tree tree)
{
 Node* temp = find(tree.getRoot(), item);
 if(temp != NULL) // if item already exists
 {
  temp->dataCount += 1;
  temp->lineNumber.push_back(lineN);
  return;
 }

 if(root == NULL) // if there is an empty space
 {
  root = new Node;   // insert new node  
  root->data = item; // w/ data value
  root->lineNumber.push_back(lineN);
  nodeCount++; 

  if(setRootQ)
  { 
   setRoot(root);
   setRootQ = false;
  } 
  return; 
 }

 if(item < root->data)
 {
  insert(root->left, item, lineN, tree);
 }
 if(item > root->data)
 {
  insert(root->right, item, lineN, tree);
 }
}

/** Outputs tree to console in inorder.
 * @param root- tree root. */
void Tree::outputTree(Node* root)
{
 if(isEmpty()) // if empty tree
 {
  cout << "Error: No items in tree" << endl; // error message
 }
 else
 { 
  if(root->left != NULL)
  {
   outputTree(root->left);
  }

  cout << "- " << root->data << " (" << root->dataCount << ") line#s: ";
  for(unsigned int i = 0; i < root->lineNumber.size(); i++)
  {
   cout << root->lineNumber[i] << ", ";
  }
  cout << endl;

  if(root->right != NULL)
  {
   outputTree(root->right);
  }
 }
}

/** Displays tree stats including number of nodes,
 *  tree height, and more frequent item.
 * @param tree- tree instance. */
void Tree::treeStats(Tree tree) 
{
 cout << "Number of entries: " << nodeCount << endl;
 // unfinished
}

/** Clears tree. 
void Tree::clearTree()
{
 this->~Tree();
} */

/** Returns the root of the tree. */
Node* Tree::getRoot()
{
 return root;
}

void Tree::setRoot(Node*& rootS)
{
 root = rootS;
}

I realize my destructor isn’t working but I’ll tackle that myself later. I’ve been pulling my hair out over this trying to figure out what I’m missing, but to no avail. If anyone can give me any help and point me in the direction towards a solution I would greatly appreciate it.

i think it might have something to do with

void Tree::insert(Node* root, ItemType item, int lineN, Tree tree)

and instead should be something like

void Tree::insert(Node* &root, ItemType item, int lineN, Tree tree)

but when i try i get a “no matching function” error. :/

  • 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-18T00:52:32+00:00Added an answer on May 18, 2026 at 12:52 am

    The solution you suggest yourself (with insert() taking Node *& root) will work. You have to make a corresponding change to the .h file, AND also change getRoot() to return Node *& in both .h and .cc files.

    This will work. But your code has other problems. For example, setRoot and setRootQ don’t do anything useful, as far as I can tell. The fourth argument to insert() only confuses things and does nothing to detect duplicates. It should be removed. To detect a duplicate simply do if (item == root->data) just before you do if (item < root->data) in the insert() method (i.e. it’ll be even more similar to find()).

    Also, if anyone besides you will ever use your code, you shouldn’t require passing in getRoot() to methods like find() and insert(). Instead, create private helper methods, like find_helper(Node*, Item) and insert_helper(Node*&,Item), and have the public methods call them with the root node as the first argument. E.g.:

    Node *find(Item item) { return find_helper(root, item); }
    

    This would also make the weird return type of getRoot() unnecessary, and you could change it back to returning the plain Node*, or get rid of that accessor altogether.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I want use html5's new tag to play a wav file (currently only supported

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.