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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:00:52+00:00 2026-06-08T03:00:52+00:00

The code function that I’m specifically talking about is getCount(). There are several other

  • 0

The code function that I’m specifically talking about is getCount(). There are several other functions that I haven’t included here (such as finding the height of this binary tree and the total node count) which work just fine, with correct results. getCount() on the other hand produces segmentation fault except for the first node (the top, first node of the tree). Any ideas?

#include <string> 
#include <algorithm>
#include <iostream>

class Word {
    public:
        std::string keyval;
        long long count;
        Word() {
            keyval = "";
            count = 0;
        }
        Word(std::string S) {
            keyval = S;
            count = 1;
        }
};

class WordBST {
    public:
        Word node;
        WordBST* left_child;
        WordBST* right_child;
        WordBST(std::string key);        
        void add(std::string key){
            if (key == node.keyval){
                node.count++;
            }
            else if (key < node.keyval){
                if (left_child == NULL){
                    left_child = new WordBST(key);
                }else {
                    left_child->add(key);
                }
            }else {
                if (right_child == NULL){
                    right_child = new WordBST(key);
                }else {
                    right_child->add(key);
                }
            }
        }
        long long getCount(std::string key){
            if (key == node.keyval){
                return (node.count);
            }
            else if (key < node.keyval){
                left_child->getCount(key);
            }else if(key > node.keyval){
                right_child->getCount(key);
            }else return 0;
            /*else {
                if (key < node.keyval){
                    left_child->getCount(key);
                }else{
                    right_child->getCount(key);
                }
            }*/
        }
};

WordBST::WordBST(std::string key) {
    node = Word(key);
    left_child = NULL;
    right_child = NULL;
}
  • 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-08T03:00:53+00:00Added an answer on June 8, 2026 at 3:00 am

    This is because you let your code run off the end without hitting a return statement.

    long long getCount(std::string key){
        if (key == node.keyval){
            return (node.count);
        } else if (left_child && key < node.keyval){
            return left_child->getCount(key); // Added return
        } else if(right_child && key > node.keyval){
            return right_child->getCount(key); // Added return
        }
        return 0;
    }
    

    You also need to add null checks in more than one place throughout the code. Your add method has them, but your getCount does not.

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

Sidebar

Related Questions

In my Java code I have function that gets file from the client in
I have the following piece of code from a function that takes the host
That's the code: function pagechange(frompage, topage) { var page = document.getElementById('formpage_' + frompage); if
I have a function that sending a code gets a description through various formatings,
I'm sending the code of a javascript function that is defined in the parent
I have this code snippet inside a function that checks if an object exists
I have written code to perform a function that could take a while to
I've made some function that generates an email template. Code it generates is pure
I'm trying to call a function that contains jQuery code. I want this function
I have a function that finds a regex thingy, then replaces with php code.

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.