Click here to Skip to main content
15,886,963 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Unwanted radio button click Pin
David Crow1-Apr-15 2:14
David Crow1-Apr-15 2:14 
QuestionBinary Tree Recursion Problem Pin
Member 1156724330-Mar-15 5:21
Member 1156724330-Mar-15 5:21 
GeneralRe: Binary Tree Recursion Problem Pin
David Crow30-Mar-15 6:01
David Crow30-Mar-15 6:01 
SuggestionRe: Binary Tree Recursion Problem Pin
Member 1156724330-Mar-15 6:09
Member 1156724330-Mar-15 6:09 
QuestionRe: Binary Tree Recursion Problem Pin
David Crow30-Mar-15 7:30
David Crow30-Mar-15 7:30 
AnswerRe: Binary Tree Recursion Problem Pin
Member 1156724330-Mar-15 8:04
Member 1156724330-Mar-15 8:04 
QuestionRe: Binary Tree Recursion Problem Pin
David Crow30-Mar-15 9:27
David Crow30-Mar-15 9:27 
AnswerRe: Binary Tree Recursion Problem Pin
Member 1156724330-Mar-15 9:31
Member 1156724330-Mar-15 9:31 
That works, but I wanted to understand why the other version does not work?
Here is all the code if you want to try.

C#
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct treenode {
    int num;
    struct treenode * left;
    struct treenode * right;
} TreeNode;

int recLookup (TreeNode *, int);
int lookUp(TreeNode *, int);
int sizeRec(TreeNode *);
int maxDepth(TreeNode *);
int minValue(TreeNode *);
int maxValue(TreeNode *);
int hasPathSum(TreeNode *, int);

TreeNode * createNode (int);
TreeNode * insertRec(TreeNode *, int);
TreeNode * buildTree();

void deleteRec(TreeNode *);
void printIncreasing(TreeNode *);
void printPostOrder(TreeNode *);
void printPaths(TreeNode *);
void printPathRecur(TreeNode *, int[], int);

enum { FALSE, TRUE };


int main (void) {
    TreeNode * root = buildTree();
    printf("Size: %d\n", sizeRec(root));
    printf("Max Depth: %d\n", maxDepth(root));
    printf("Min Value: %d\n", minValue(root));
    printf("Max Value: %d\n", maxValue(root));
    printIncreasing(root); puts("");
    printPostOrder(root); puts("");
    printf("Has path sum 15: %d\n", hasPathSum(root, 15));
    printPaths(root);
    // not sure why line below throws segmentation core
    printf("Min Val: %d Max Val: %d\n", minValue(root), maxValue(root));

    deleteRec(root);


    return 0;
}

void printPaths(TreeNode * root) {
    int path[10];
    int pathLen = 0;
    printPathRecur(root, path, pathLen);
}

void printPathRecur(TreeNode * node, int path[], int pathLen) {
    if (node == NULL) return;
    path[pathLen] = node->num;
    pathLen++;
    if (node->right == NULL && node->left == NULL) {
        int i;
        for (i = 0; i < pathLen; i++)
            printf("%d ", path[i]);
        puts("");
    }
    else {
        printPathRecur(node->left, path, pathLen);
        printPathRecur(node->right, path, pathLen);
    }
}

int hasPathSum(TreeNode * root, int num) {
    TreeNode * current = root;
    if (current == NULL)
        return num == 0;
    else {
        int n = num - current->num;
        return
            hasPathSum(current->left, n) ||
            hasPathSum(current->right, n);
    }
}


void printPostOrder(TreeNode * root) {
    TreeNode * current = root;
    if (current == NULL) return;
    printPostOrder(current->left);
    printPostOrder(current->right);
    printf("%d ", current->num);
}

void printIncreasing(TreeNode * root) {
    TreeNode * current = root;
    if (current == NULL) return;
    printIncreasing(current->left);
    printf("%d ", current->num);
    printIncreasing(current->right);
}

int minValue(TreeNode * root) {
    TreeNode * current = root;
    if (current == NULL) return -1;
    else {
        while (current->left != NULL)
            current = current->left;
        return current->num;
    }
}

int maxValue(TreeNode * root) {
    TreeNode * current = root;
    if (current == NULL)
        return -1;
    else
     {
        while (current->right != NULL)
            current = current->right;
        return current->num;
     }
}

int maxDepth (TreeNode * root) {
    if (root == NULL) return 0;
    else {
        int lDepth = maxDepth(root->left);
        int rDepth = maxDepth(root->right);
        if (lDepth > rDepth) return (lDepth + 1);
        else return (rDepth + 1);
    }
}

void deleteRec(TreeNode * root) {
    if (root == NULL) return;
    else {
        deleteRec(root->right);
        deleteRec(root->left);
        free(root);
    }
}

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

TreeNode * buildTree() {
    TreeNode * root = insertRec(root, 4);
    root = insertRec(root, 3);
    root = insertRec(root, 6);
    return root;
}

TreeNode * insertRec (TreeNode * node, int num) {
    if (node == NULL)
        return createNode(num);
    else {
        if (num <= node->num) node->left = insertRec(node->left, num);
        else node->right = insertRec(node->right, num);
    }
    return node;
}

TreeNode * createNode(int num) {
    TreeNode * newNode = (TreeNode *) malloc (sizeof (TreeNode));
    newNode->num = num;
    newNode->right = NULL;
    newNode->left = NULL;
    return newNode;
}

int lookUp(TreeNode * node, int num) {
    while (node != NULL) {
        if (node->num == num) return TRUE;
        else {
            if (num < node->num) node = node->left;
            else node = node->right;
        }
    }
    return FALSE;
}


int recLookup (TreeNode * node, int num) {
    if (node == NULL)
        return FALSE;
    else {
        if (node->num == num) return TRUE;
        else {
            if (num < node->num) return recLookup(node->left, num);
            else return recLookup(node->right, num);
        }
    }
}

AnswerRe: Binary Tree Recursion Problem Pin
David Crow30-Mar-15 9:47
David Crow30-Mar-15 9:47 
GeneralRe: Binary Tree Recursion Problem Pin
Member 1156724330-Mar-15 10:09
Member 1156724330-Mar-15 10:09 
QuestionCpu temperature to obtain the sample code in visual c/c++ . Pin
Dandy Smile29-Mar-15 9:57
Dandy Smile29-Mar-15 9:57 
AnswerRe: Cpu temperature to obtain the sample code in visual c/c++ . Pin
Richard MacCutchan29-Mar-15 21:14
mveRichard MacCutchan29-Mar-15 21:14 
GeneralRe: Cpu temperature to obtain the sample code in visual c/c++ . Pin
David Crow30-Mar-15 3:26
David Crow30-Mar-15 3:26 
QuestionNeed to declare a class ( defined in cpp file ) as friend Pin
Raj Abhishek27-Mar-15 0:59
Raj Abhishek27-Mar-15 0:59 
AnswerRe: Need to declare a class ( defined in cpp file ) as friend Pin
Richard Andrew x6427-Mar-15 8:26
professionalRichard Andrew x6427-Mar-15 8:26 
AnswerRe: Need to declare a class ( defined in cpp file ) as friend Pin
Freak3030-Mar-15 1:14
Freak3030-Mar-15 1:14 
AnswerRe: Need to declare a class ( defined in cpp file ) as friend Pin
Stefan_Lang8-Apr-15 4:39
Stefan_Lang8-Apr-15 4:39 
QuestionLinkage Problem in MSVC 2008 (wxWidget 3.0 Application) Pin
Member 1125835326-Mar-15 20:59
Member 1125835326-Mar-15 20:59 
AnswerRe: Linkage Problem Pin
Richard MacCutchan26-Mar-15 21:16
mveRichard MacCutchan26-Mar-15 21:16 
GeneralRe: Linkage Problem Pin
Member 1125835326-Mar-15 23:26
Member 1125835326-Mar-15 23:26 
GeneralRe: Linkage Problem Pin
Jochen Arndt27-Mar-15 0:11
professionalJochen Arndt27-Mar-15 0:11 
GeneralRe: Linkage Problem Pin
Member 1125835327-Mar-15 0:18
Member 1125835327-Mar-15 0:18 
GeneralRe: Linkage Problem Pin
Jochen Arndt27-Mar-15 0:30
professionalJochen Arndt27-Mar-15 0:30 
GeneralRe: Linkage Problem Pin
Member 1125835327-Mar-15 1:50
Member 1125835327-Mar-15 1:50 
GeneralRe: Linkage Problem Pin
Jochen Arndt27-Mar-15 2:14
professionalJochen Arndt27-Mar-15 2:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.