spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Returning error when traversing through a tree


I'm not sure what your code is trying to do, since you do not define your node struct, however your function int sameFrequency(node *node, char* key) has an obvious bug: not all code paths return a value. Reformatting a bit for clarity, you can see that if strcmp(key, key)!=0 then the return is undefined:

int sameFrequency(node *node, char*
key) {
    if (node != NULL) {

        if(strcmp(key, node->key)==0){
            node->frequency =
node->frequency+1;
            printf("%d
",node->frequency);
            return 1;
        }
        else { 
            sameFrequency(node->left, key);
            sameFrequency(node->right, key);
            // Continue on out of the "if"
statements without returning anything.
        }
    }
    else {
        return 0;
    }
    // NO RETURN STATEMENT HERE
}

My compiler generates a warning for this:

warning C4715: 'sameFrequency' : not all control paths return a value

Surely yours must be doing so as well, unless you intentionally disabled them. Such warnings are important, and should always be cleared up before finishing your code.

I'm guessing you want to do something like this, perhaps?

int sameFrequency(node *node, char*
key) {
    if (node != NULL) {

        if(strcmp(key, node->key)==0){
            node->frequency =
node->frequency+1;
            printf("%d
",node->frequency);
            return 1;
        }
        else { 
            int found;
            if ((found =
sameFrequency(node->left, key)) != 0)
                return found;
            if ((found =
sameFrequency(node->right, key)) != 0)
                return found;
            return 0;
        }
    }
    else {
        return 0;
    }
}

This clears the compiler warning.

Incidentally, the following if statement is probably in the wrong order:

        if((sameFrequency(root,
key)==1)&&root!=NULL) { 

Since && statements in C execute left to right the following makes more sense:

        if(root!=NULL &&
(sameFrequency(root, key)==1)) { 

Categories : C

Related to : Returning error when traversing through a tree
Traversing Through Directories
#!/usr/local/bin/perl use strict; use warnings; use File::Find; my @argv; my $dir = $ARGV[0]; find(&dirRecurs, $dir); sub dirRecurs{ if (-f) { (my $txt = $_) =~ s/pl$/txt/; rename($_, $txt); } } I figured it out with the help of the tutorial @David sent me! Thank you!

Categories : Perl
Traversing a Tree Structure
Issue #1 Error 669 A local variable named 'child' cannot be declared in this scope because it would give a different meaning to 'child', which is already used in a 'parent or current' scope to denote something else. TreeNode<T> child = null; foreach (Tree<T> child in this.root) // <--- this generates an error { PrintDFS(child, spaces); } You already have a chil

Categories : C#
Traversing a Tree Structure and Action
in the first line, you need to pass a variable. Basically, call action just like you would a reglular function: action(myTreeNode) The second error is because you forgot to select the "Values" from the dictionary. internal void Traverse(Action<TreeNode<T>> action) { action(this); foreach (var child in this.children.Values) child.Traverse(a

Categories : C#
nodejs Returning Error when trigerring "npm install"
npm install command installs node packages wich are written in package.json file for example you have package.json : { "name": "application-name" , "version": "0.0.1", "main": "app.js" , "dependencies": { "express": "*" , "jade": "*" ,"cookie-parser":"*" }, "scripts": { "start": "node app.js" } } when you write npm install it will install dependencies express j

Categories : Angularjs
Google App Script connection returning error 401
Try removing the contentType from parameters and give it a shot. For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded multipart/form-data text/plain will trigger the browser to send a preflight OPTIONS request to the server.

Categories : Javascript
Recently Add
Rewrite of IDA decompiled function
Computing the average of grades in C
passing structures to functions
Use semaphores for handling sockets in C
How to search for a string pattern inside html, coding in C?
sprintf invalid format '%d'
Why can't this c program run correctly?
Low Pass filter in C
Child process does not print anything
C: datatypes. sqrt function working with int why?
How to implement Serial Port Profile Link Command used in ConnectBlue Bluetooth Module?
Rerunning cancelled pthread
How to print out a string that is pointed to by a char pointer?
Can unverified scanf call cause an undefined behavior?
nested structures in C
Portable Makevars for R package using C, GSL and OpenMP with help of Rcpp
program doesn't run(getting RUN FINISHED Segmentation fault core dumped)
Finding the sign of a Lua number, from C
Width of symbols created by gcc's objectcopy
Strange behavior when using free in c program
Can't open image C
Strange C code - dynamic arrays?
C program printing weird characters
Fault in decimal to binary in c
How to detect broken pipe on input?
How can I ivestigate use of the register keyword in C?
File scaning with fscanf
why gcc generates "leal -8240(%ebp), %eax; cmpl $1, %eax; ja XXX" instead of jmp instruction?
Wierd Problems With My Own Custom Written Word Search Algorithm
Trouble with making a shell in C
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.