Thread: Binary tree problem...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    61

    Binary tree problem...

    Please help..
    The output must be "-B -C -A ", but when I run it, It shows "- -B -C - A ".
    Code:
    
    
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    typedef struct Node
    {
    char data;
    struct Node *right;
    struct Node *left;
    };
    
    typedef struct Tree
    {
    Node *root;
    };
    
    Tree tree;
    
    void iLeft(Node* node, char data)
    {
    Node* current = (Node*) malloc(sizeof(Node*));
    current->left = NULL;
    current->right = NULL;
    current->data = data;
    node->left = current;
    }
    
    void iRight(Node* node, char data)
    {
    Node* current = (Node*) malloc(sizeof(Node*));
    current->left = NULL;
    current->right = NULL;
    current->data = data;
    node->right = current;
    }
    
    void print(Node* node)
    {
    if(node->left != NULL)
    print(node->left);
    if(node->right != NULL)
    print(node->right);
    printf("-%c ", node->data);
    }
    
    int main()
    {
    char ins[] = {"ABC"};
    tree.root->data = ins[0];
    iLeft(tree.root, ins[1]);
    iRight(tree.root, ins[2]);
    print(tree.root);
    getch();
    return 0;
    }
    
    Last edited by programmerc; 03-25-2013 at 01:15 PM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    I'm stunned that you have a compiler that would turn this into an executable. Turn warnings on full.

    Your use of typedef is incomplete and your attempt to dereference tree.root will cause a segmentation fault.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary tree problem
    By moddinati in forum C++ Programming
    Replies: 8
    Last Post: 06-19-2008, 05:05 PM
  2. Binary Tree Problem
    By noodle24 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2007, 02:20 PM
  3. binary tree problem
    By spank in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:27 AM
  4. Binary tree problem
    By carrja99 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 09:36 PM
  5. binary tree problem??
    By fergie in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2002, 12:17 PM