Jump to content

Background: I am writing a program that takes a list of data and parses it into a binary search tree. The key for this data is of type "string". I'm working on writing a boolean function that returns "true" if the key of the data to be added precedes the key of the data in the reference node, "false" if the new key succeeds the old, and recursively runs back through the function, incrementing by one position, should the characters in question be equal is ASCII value.

 

Currently, it appears that my comparisons are correct and appears to be recursing correctly, but is returning false for all cases.

bool abcOrder(movie m, mNode *ref, int i) {
        int data = NULL;
        //Gets key data and converts to lowercase as needed
        int newNode = m.getTitle().at(i);
        if (newNode < 97)
            newNode = newNode + 32;
        int refNode = ref->M.getTitle().at(i);
        if (refNode < 97)
            refNode = refNode + 32;
        //Compares key data at position 'i'
        if (newNode < refNode) {
            return true;
        } else if (newNode > refNode) {
            return false;
        } else {
            //If newNode.key == refNode.key, increment position and run again
            i++;
            abcOrder(m, ref, i);
        }
    }

Given keys of:

1. laak

2. laab

3. laaj

4. laal

 

We should see an output of:

true (2 is left of 1)

true then false (3 is left of 1 and right of 2)

false (k is right of 1)

 

What it's actually returning is all false with 4 being the right child of 3, 3 being right of 2, and 2 being right of 1.

Note: Insertion method worked properly before I implemented this function, albeit with unique first characters.

Spoiler

Intel Core i7 8700k | be quiet! Dark Rock 4 | Fatal1ty Z370 Gaming-ITX/ac | 32 GB G.Skill TridentZ | 256 GB Intel® SSD 600p Series | ZOTAC GeForce® GTX 1080 Ti Mini | Fractal Design Node 304 | Cooler Master V750 | Asus MG279Q | Asus VC279 | Logitech G710+ | Corsair M65 Pro RGB

 

Link to comment
https://linustechtips.com/topic/1056064-alphabetical-order-c-recursive-function/
Share on other sites

Link to post
Share on other sites

Note.

The good fellers over at Stack Overflow have come to my rescue.

bool abcOrder(movie m, mNode *ref, int i) {
        int data = NULL;
        //Gets key data and converts to lowercase as needed
        int newNode = m.getTitle().at(i);
        if (newNode < 97)
            newNode = newNode + 32;
        int refNode = ref->M.getTitle().at(i);
        if (refNode < 97)
            refNode = refNode + 32;
        //Compares key data at position 'i'
        if (newNode < refNode) {
            return true;
        } else if (newNode > refNode) {
            return false;
        } else {
            //If newNode.key == refNode.key, increment position and run again
            i++;
            return abcOrder(m, ref, i);
        }
    }

On the third to last line, the recursive call needed a return preceding it. 

Spoiler

Intel Core i7 8700k | be quiet! Dark Rock 4 | Fatal1ty Z370 Gaming-ITX/ac | 32 GB G.Skill TridentZ | 256 GB Intel® SSD 600p Series | ZOTAC GeForce® GTX 1080 Ti Mini | Fractal Design Node 304 | Cooler Master V750 | Asus MG279Q | Asus VC279 | Logitech G710+ | Corsair M65 Pro RGB

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×