简体   繁体   中英

Function that counts number of names in a tree that are greater than the input name

I am trying to search through a binary tree for strings that are greater than the string "Jessica" . This is the function I currently have to try to do this:

int greaternames(Bnode* root, string searchname){
    if(root == NULL){return 0;}
    else{
        if(searchname <= root -> data){
            return size(root -> right) + greaternames(root -> left, searchname) + 1;
        }
        else if(searchname > root -> data){
            greaternames(root -> right, searchname);
        }
    }
}

This is my code as a whole, everything else works as it should:

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

struct Bnode{
    string data;
    Bnode* left;
    Bnode* right;
};

void inorder(Bnode* root){
    if(root != NULL){
        inorder(root -> left);
        cout << root -> data << endl;
        inorder(root -> right);
    }
}

void add(Bnode*& root, string item){
    if(root == NULL){
        root = new Bnode;
        root -> data = item;
        root -> left = root -> right = NULL;
    }
    else if (item <= root -> data){
        add(root -> left, item);
    }
    else{
        add(root -> right, item);
    }
}

int size(Bnode* root){
    if(root == NULL) return 0;
    else
    return size(root -> left) + size(root -> right) + 1;
}

void count(Bnode* root, string target){
    int i = 0;
    Bnode* cursor = root;
    while(cursor != NULL){
        if(cursor -> data == target){
            i++;
            cursor = cursor -> left;
        }
        else if(cursor -> data > target){
            cursor = cursor -> left;
        }
        else{cursor = cursor -> right;}
    }
    cout << "Your search name appears " << i << " times" << endl;
}

int greaternames(Bnode* root, string searchname){
    if(root == NULL){return 0;}
    else{
        if(searchname <= root -> data){
            return size(root -> right) + greaternames(root -> left, searchname) + 1;
        }
        else if(searchname > root -> data){
            greaternames(root -> right, searchname);
        }
    }
}

int main(){
    Bnode* root = NULL;
    string name;
    ifstream ins;
    ins.open("names.txt");
    while(ins >> name){
        add(root, name);
    }
    ins.close();

    count(root, "Matthew");
    cout << greaternames(root, "Jessica") << " names greater than Jessica" << endl;

return 0;
}

names.txt is just a file that contains 173 strings of names.

I tried to sort of replicate the size function just instead of counting everything it only counts the right side sub-trees if the root is less than or equal to the searchname which is "Jessica". But the output I currently get is something like -4million.

Also, this is for a homework assignment so the function has to be recursive.

As a hint, compile with warnings turned up to the maximum. You should get a notice somewhere that a function doesn't always return a value. Which function is it, why isn't it always returning a value, and how will changing that fix your issue?

You are missing a return statement in the greaterNames function:

int greaternames(Bnode* root, string searchname){
    if(root == NULL){return 0;}
    if(searchname <= root -> data){
        return size(root -> right) + greaternames(root -> left, searchname) + 1;
    }
    //else if statement that was here is redundant and will give you compiler warnings, 
    //  due to missing return statement at the end of the function 
    return greaternames(root -> right, searchname); //missing return statement
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM