creation of binary tree

This is a discussion on creation of binary tree within the C Programming forums, part of the General Programming Boards category; can anyone tell what is wrong with this program,please. #include<stdio.h> #include<stdlib.h> #define NULL 0 struct binary { int number; struct ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    6

    creation of binary tree

    can anyone tell what is wrong with this program,please.
    #include<stdio.h>
    #include<stdlib.h>
    #define NULL 0
    struct binary
    {
    int number;
    struct binary *parent;
    struct binary *rightchild;
    struct binary *leftchild;
    };
    typedef struct binary node;
    main()
    {
    void create(node *head);
    void inorder(node *head);
    node *head;
    head=(node *)malloc(sizeof(node));
    printf("enter number and press -999 at the end");
    scanf("%d",&head->number);
    head->parent=NULL;
    head->rightchild=NULL;
    head->leftchild=NULL;
    create(head);
    inorder(head);
    }
    void create(node *list1)
    {
    node *list,*head1;
    int x;
    head1=list1;
    printf("enter number and press -999 at the end");
    scanf("%d",&x);
    if(x==-999)
    return;
    else
    {
    while(head1!=NULL)
    {
    list=head1;
    if(x<head1->number)
    {
    if(head1->leftchild==NULL)
    {
    list->leftchild=(node *)malloc(sizeof(node));
    list=list->leftchild;
    }
    head1=head1->leftchild;
    continue;
    }
    if(x>=head1->number)
    {
    if(head1->rightchild==NULL)
    {
    list->rightchild=(node *)malloc(sizeof(node));
    list=list->rightchild;
    }
    head1=head1->rightchild;
    }
    }
    list->number=x;
    list->parent=list;
    list->rightchild=NULL;
    list->leftchild=NULL;
    create(list1);
    }
    return;
    }
    void inorder(node *head1)
    {
    if(head1!=NULL)
    {
    inorder(head1->leftchild);
    printf("%d",head1->number);
    inorder(head1->rightchild);
    }
    return;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21