Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
C - binary tree       Share
2007-11-16 |  RatheeshTR  | Viewed: 330  |    0

//  Binary  tree  can  be  represented  as  an  array  using  the  method

#include <stdio.h>
#include <malloc.h>
 

struct node 

{

    struct node *left;

    
char data;
    struct node *right;

};


struct node* buildtree(int);

void inorder(struct node*);

char a[] = {'A','B','C','D','E','F','G','\0','H','\0'
            ,'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'};

           

int main()
{

    struct node *root;

    

    root = buildtree(0);

    printf("output:\n");

    inorder(root);

    

    return 0;

}


struct node* buildtree(
int index)
{

    struct node *temp;

    

    
if (a[index] != '\0')
    {

        temp = (struct node*) malloc(sizeof(struct node*));

        temp->left  = buildtree(2 * index + 1);

        temp->data  = a[index];

        temp->right = buildtree(2 * index + 2);

    }

    return temp;

}


void inorder(struct node *rNode)
{

    
if (rNode != NULL)
    {    

        inorder(rNode->left);

        printf("%c ",rNode->data);

        inorder(rNode->right);

    }

}


//    output:
//    D  B  H  E  A  F  C  G


Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
C - Static variables
C - global and internal variables
C - Global variables , static variables

Related References
c - static variables
c - local and global variables
c - register variables

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us