post #11 of 11
Thread Starter 
Quote:
Originally Posted by selectstriker2 View Post

Tree traversals look fine. His main function is a disaster however. Firstly it needs to be of type int, not void. Secondly the curly brackets do not match up so it will not compile.
ok i changed it and fixed the curly brackets Warning: Spoiler! (Click to show)
Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct tree*node;
node insert(int,node T);
void inorder(node T);
void preorder(node T);
void postorder(node T);
struct tree
{
        int data;
        struct tree*right,*left;
}*root;

int main()
{
        node T= NULL;
        int data,ch,i=0,n;
        clrscr();
        printf("\nEnter the number of elements in the Tree:");
        scanf("%d",&n);
        printf("\n The elements are :\n");
        while(i<n);
        {
                scanf("%d",&data);
                T=insert(data,T);
                i++;
        }
        printf("1. INORDER\t2.PREORDER\t3.POSTOTRDER\t4.EXIT");
        do
        {
                printf("\nEnter your choide:");
                scanf("%d",&ch);
                switch (ch)
                {
                        case1: printf ("Inorder traversal of the given Tree\n");
                        inorder(T);
                        break;
                        case2: printf("Preoroder traversal of the given Tree\n");
                        preorder(T);
                        break;
                        case3: printf("Postorder traversal of the given Tree\n");
                        posotorder(T);
                        break;
                        default:printf("Exit");
                        exit(0);
                }

}
while(ch<4);
getch();
node insert(intX, node T)
{
        struct tree*newnode;
        newnode=malloc(sizeof(struct tree));
        if(newnode==NULL)
        printf("Out of space\n");
        else
        {
                if(T==NULL)
                {
                        newnode->data=X;
                        newnode->left=NULL;
                        newnode->right=NULL;
                        T=newnode;
                }
                else
                {
                        if(X<T->data)
                        T->left=insert(X,T->left);
                        else
                        T->right=insert(X,T->right);
                }
        }
        return T;
}
void inorder(node T)
{
        if(T!=NULL)
        {
                inorder(T->left);
                printf("%d\t",T->ddata);
                inorder(T->right);
        }
}
void preorder(node T)
{
        if(T!=NULL)
        {
                printf("%d\t",T->data);
                preorder(T->left);
                preorder(T->right);
        }
}
void postoroder(node T)
{
        if(T!=NULL);
        {
                postorder(T->left);
                postorder(T->right);
                printf("%d\t",T->data);
        }
}
}
The Prodigy
(16 items)
 
 
Dream Rig Contest
(11 items)
 
CPUMotherboardGraphicsRAM
[AMD] Athlon II x2 255 [Gigabyte] GA-78LMT-SP2 [Onboard] ATI Radeon 3000 [Crucial] Ballistix 8GB (2x4GB) 
Hard DriveHard DriveHard DriveOptical Drive
[Western Digital] Blue 320GB [Maxtor] 320GB soon to be adding 4TB from WD Red [Lite-On] DVD Player/Burner 
CoolingOS
[Stock] [Mircrosoft] Windows 7/ Home server 2011 
  hide details  
Reply
The Prodigy
(16 items)
 
 
Dream Rig Contest
(11 items)
 
CPUMotherboardGraphicsRAM
[AMD] Athlon II x2 255 [Gigabyte] GA-78LMT-SP2 [Onboard] ATI Radeon 3000 [Crucial] Ballistix 8GB (2x4GB) 
Hard DriveHard DriveHard DriveOptical Drive
[Western Digital] Blue 320GB [Maxtor] 320GB soon to be adding 4TB from WD Red [Lite-On] DVD Player/Burner 
CoolingOS
[Stock] [Mircrosoft] Windows 7/ Home server 2011 
  hide details  
Reply