Binary Tree Implementation Source code

Source: Internet
Author: User
Tags printf

Binary tree Implementation of the source code is as follows:

#include <conio.h>


#include <stdio.h>


#include <stdlib.h>


#define OK 1


#define ERROR 0


#define TRUE 1


#define FALSE 0


#define OVERFLOW-2


typedef int status;


typedef struct BINODE


{


Char Data;


struct binode* lchild;


struct binode* rchild;


}binode,*pbinode;


status Createtree (binode** pTree);


status Preordertraval (binode* pTree);


status Visit (char Data);


status Display (binode* ptree,int level);


Status Clear (binode* pTree);


Binode *proot=null;


Main ()


{


CLRSCR ();


Createtree (&proot);


printf ("\npreorder:");


Preordertraval (Proot);


printf ("\ n");


printf ("\ninorder:");


Inordertraval (Proot);


printf ("\ n");


printf ("\npostorder:");


Postordertraval (Proot);


printf ("\ n");


printf ("\nshowleaves:");


showleaves (Proot);


printf ("\ n-----------------------\ n");


printf ("\ n");


Display (proot,0);


printf ("\ n");


printf ("\ndeleting tree:\n");


deltree (Proot);


printf ("Bitree Deleted.");


Getch ();


}


status Createtree (binode** pTree)/*input example:abd# #e # #cf # #g ##*/


{


Char ch;


scanf ("%c", &ch);


if (ch== ' # ')


  {


(*ptree) =null;


  }


Else


  {


if (!) ( (*ptree) = (binode*) malloc (sizeof (Binode)))


    {


exit (OVERFLOW);


    }


(*ptree)->data=ch;


Createtree (& (*ptree)->lchild));


Createtree (& (*ptree)->rchild));


  }


return OK;


}


status Preordertraval (binode* pTree)


{


if (pTree)


  {


if (Visit (ptree->data))


    {


if (Preordertraval (ptree->lchild))


      {


if (Preordertraval (ptree->rchild))


        {


return OK;


        }


      }


    }


return ERROR;


  }


Else


  {


return OK;


  }


}


Status Inordertraval (binode* pTree)


{


if (pTree)


  {


if (Inordertraval (ptree->lchild))


    {


if (Visit (ptree->data))


      {


if (Inordertraval (ptree->rchild))


        {


return OK;


        }


      }


return ERROR;


    }


return ERROR;


  }


Else


  {


return OK;


  }


}


status Postordertraval (binode* pTree)


{


if (pTree)


  {


if (Postordertraval (ptree->lchild))


    {


if (Postordertraval (ptree->rchild))


      {


if (Visit (ptree->data))


        {


return OK;


        }


return ERROR;


      }


    }


return ERROR;


  }


Else


  {


return OK;


  }


}


status Visit (char Data)


{


printf ("%c", Data);


return OK;


}


status Display (binode* ptree,int level)


{


int i;


if (ptree==null) return;


Display (ptree->lchild,level+1);


for (i=0;i<level-1;i++)


  {


printf ("");


  }


if (level>=1)


  {


printf ("--");


  }


printf ("%c\n", ptree->data);


Display (ptree->rchild,level+1);


}


status showleaves (binode* pTree)


{


if (pTree)


  {


if (showleaves (ptree->lchild))


    {


if (showleaves (ptree->rchild))


      {


if ((ptree->lchild==null) && (ptree->rchild==null))


        {


if (! Visit (ptree->data))


          {


return ERROR;


          }


        }


return OK;


      }


    }


return ERROR;


  }


Else


  {


return OK;


  }


}


status deltree (binode* pTree)


{


if (pTree)


  {


if (deltree (ptree->lchild))


    {


if (deltree (ptree->rchild))


      {


printf ("Deleting%c\n", ptree->data);


Free ((void*) pTree);


return OK;


      }


    }


return ERROR;


  }


Else


  {


return OK;


  }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.