c語言+數據結構不固定輸入元素個數時整數的二叉存儲式

 #include
#include<stdlib.h>
#define Max 200
typedef  struct BTree{
                      long int data;
                      struct BTree *Lchild,*Rchild;
                }BinTree,*Pbtree;
Pbtree  root=NULL;
void insert(long int);
void creat_btree()
            {
              long int n,i;
              long int a[Max];
              printf("please put into node number:");
              scanf("%ld",&n);
              for(i=0;i<n;i++)
                 {
                   printf("%ld,",i);
                   scanf("%ld",a+i);
                  }
             
              for(i=0;i<n;i++)
                  {                  
                      insert(a[i]);
                   }  
             
            }
void insert(long m)
      {
       Pbtree p,q;
       if(root==NULL)
           {
            root=(Pbtree)malloc(sizeof(BinTree));
            root->data=m;
            root->Lchild=NULL;
            root->Rchild=NULL;
            }
       else
          {
           p=root;
           while(m!=p->data)
                {
                   if(m>p->data)
                      {
                       if(p->Rchild==NULL)
                         {
                           q=(Pbtree)malloc(sizeof(BinTree));
                           q->data=m;
                           q->Lchild=NULL;
                           q->Rchild=NULL;
                           p->Rchild=q;
                         }
                       else
                         {
                           p=p->Rchild;
                         }
                      }
                  else
                     {
                      if(p->Lchild==NULL)
                        {
                          q=(Pbtree)malloc(sizeof(BinTree));
                          q->data=m;
                          q->Lchild=NULL;
                           q->Rchild=NULL;
                           p->Lchild=q;
                        }
                      else
                        {
                          p=p->Lchild;
                        }
                     }
                  }
               }
            }
                   
void pre_order(Pbtree p)
       {
        if(p!=NULL)
          {
            printf("%ld ,",p->data);
            pre_order(p->Lchild);
            pre_order(p->Rchild);
          }
       }
void in_order(Pbtree p)
      {
       if(p!=NULL)
          {
           in_order(p->Lchild);
           printf("%ld ,",p->data);
           in_order(p->Rchild);
          }
      }
void post_order(Pbtree p)
       {
        if(p!=NULL)
           {
            post_order(p->Lchild);
            post_order(p->Rchild);
            printf("%ld ,",p->data);
           }
       }
      
void main()
     {  printf("The establishment of the establishment of non-recursive tree:/n");
      creat_btree();
      printf("/n");
      printf("Pre-order traversal:  ");
      pre_order(root);
      printf("/n");
      printf("in-order traversal:  ");
      in_order(root);
      printf("/n");
      printf("post-order traversal:  ");
      post_order(root);
      printf("/n");
      for(;;){}
     }
          
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章