程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C語言二叉樹的建立,遍歷(遞歸,非遞歸)

C語言二叉樹的建立,遍歷(遞歸,非遞歸)

編輯:關於C語言

 

#include<stdio.h>

#include<stdlib.h>

#define MAXSIZE 20

typedef struct BiTnode{

 char data;

 struct BiTnode *lchild,*rchild;

}BiTnode,*BiTree;

 

//浜屽弶鏍戠殑閫掑綊寤虹珛

int i = 0;

BiTree Create(BiTree t,char s[])

{

 char ch;

 ch = s[i++];

 

 if(ch == ' ')

 {

  t = NULL;

 }

 else

 {

  if(!(t = (BiTree)malloc(sizeof(BiTnode))))

  {

   printf("fail to malloc!\n");

   exit(0);

  }

  else

  {

   t->data = ch;

   t->lchild = Create(t->lchild,s);

   t->rchild = Create(t->rchild,s);

  }

 }

 return t;

}

 

//涓簭閬嶅巻

/*

void display(BiTree t)

{

 BiTree stack[MAXSIZE],p;

 int top = -1;

 if(t)

 {

  p = t;

  while(top > -1 || p)

  {

   while(p)

   {   

    stack[++top] = p;

    p = p->lchild;

   }

   if(top > -1)

   {

    p = stack[top--];   

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

    p = p->rchild;

   }

  }

  printf("\n");

 }

}

/*/

 

//鍓嶅簭閬嶅巻

/*

void display(BiTree t)

{

 BiTree stack[MAXSIZE],p;

 int top = -1;

 if(t)

 {

  p = t;

  while(top > -1 || p)

  {

   while(p)

   {

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

    stack[++top] = p;

    p = p->lchild;

   }

   if(top > -1)

   {

    p = stack[top--];       

    p = p->rchild;

   

   }

  }

  printf("\n");

 }

}

*//*

 

 

//鍓嶅簭閬嶅巻

void display(BiTree t)

{

     BiTree stack[MAXSIZE], p;

     int top = -1;

     if (t)

     {       

         stack[++top] = t;

         while (top > -1)

         {             

             p = stack[top--];

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

             if (p->rchild)

              {                 

                  stack[++top] = p->rchild;

              }             

             if (p->lchild)

              {                

                  stack[++top] = p->lchild;

              }

         }

         printf("\n");

     }

}*/

/*

//闈為€掑綊鍚庡簭閬嶅巻

void display(BiTree t)

{

 BiTree m,stack[MAXSIZE];

 int top = -1;

 int flag;

 if(t)

 {

loop:

  flag = 1;

  m = NULL;

  while(t)

  {

   stack[++top] = t;

   t = t->lchild;

  }

  while(flag)

  {

   t = stack[top];

   if(t->rchild == m)

   {

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

    top--;

    m = t;

   }

   else

   {

    flag = 0;

    t = t->rchild;

   }

  }

  if(top>-1)

   goto loop;

 }

 

 printf("\n");

}

*/

 

//闈為€掑綊鍚庡簭閬嶅巻

void display(BiTree t)

{

    BiTree p = t, stack[MAXSIZE];

    int tag[MAXSIZE];

    int top = -1;

   

 do

     {

         while(p != NULL)

         {

             stack[++top] = p;

             tag[top] = 0;

             p = p->lchild;

         }            

         if(top > -1)

         {

             if(!tag[top]) 

              {

                  p = stack[top];

                  p = p->rchild;

                  tag[top] = 1;

              }

             else

              {           

                   printf("%c ", stack[top--]->data);

              }

         }

     }while((p != NULL)||(top > -1));

 printf("\n");

}    

 

//閫掑綊鍓嶅簭閬嶅巻

/*void display(BiTree t)

{

 if(t)

 {

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

  display(t->lchild);

  display(t->rchild)锛?

 }

}*/

//閫掑綊涓簭閬嶅巻

/*void display(BiTree t)

{

 if(t)

 {

  display(t->lchild);

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

  display(t->rchild)锛?

 }

}*/

 //閫掑綊鍚庡簭閬嶅巻

/*void display(BiTree t)

{

 if(t)

 {

 

  display(t->lchild);

  display(t->rchild);

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

 }

}*/

int main(int argc,char *argv[])

{

 BiTree t;

 char s[] = "abc  de f  g    ";

 t = Create(t,s);

 display(t);

 

 return 0;

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved