Click here to Skip to main content
15,894,540 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: const char error Pin
Richard MacCutchan1-Apr-15 5:17
mveRichard MacCutchan1-Apr-15 5:17 
SuggestionRe: const char error Pin
David Crow1-Apr-15 5:13
David Crow1-Apr-15 5:13 
AnswerRe: const char error Pin
vishwadeepak mani tripathi2-Apr-15 8:05
vishwadeepak mani tripathi2-Apr-15 8:05 
GeneralRe: const char error Pin
ForNow2-Apr-15 9:46
ForNow2-Apr-15 9:46 
GeneralRe: const char error Pin
ForNow2-Apr-15 16:54
ForNow2-Apr-15 16:54 
SuggestionRe: const char error Pin
David Crow3-Apr-15 3:23
David Crow3-Apr-15 3:23 
GeneralRe: const char error Pin
ForNow3-Apr-15 4:27
ForNow3-Apr-15 4:27 
Question[C++]execute other program on second monitor Pin
Member 1156860630-Mar-15 19:22
Member 1156860630-Mar-15 19:22 
AnswerRe: [C++]execute other program on second monitor Pin
Richard MacCutchan30-Mar-15 23:13
mveRichard MacCutchan30-Mar-15 23:13 
QuestionUnwanted radio button click Pin
lor7530-Mar-15 8:29
lor7530-Mar-15 8:29 
AnswerRe: Unwanted radio button click Pin
David Crow30-Mar-15 9:24
David Crow30-Mar-15 9:24 
GeneralRe: Unwanted radio button click Pin
lor7531-Mar-15 8:47
lor7531-Mar-15 8:47 
GeneralRe: Unwanted radio button click Pin
David Crow31-Mar-15 9:23
David Crow31-Mar-15 9:23 
GeneralRe: Unwanted radio button click Pin
lor7531-Mar-15 20:42
lor7531-Mar-15 20:42 
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 

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.