樹的雙親表存儲

 /* c6-4.h 樹的雙親表存儲表示 */
 #define MAX_TREE_SIZE 100
 typedef struct
 {
   TElemType data;
   int parent; /* 雙親位置域 */
 } PTNode;
 typedef struct
 {
   PTNode nodes[MAX_TREE_SIZE];
   int n; /* 結點數 */
 } PTree;
 /* bo6-4.c 樹的雙親表存儲(存儲結構由c6-4.h定義)的基本操做(14個) */
 Status InitTree(PTree *T)
 { /* 操做結果: 構造空樹T */
   (*T).n=0;
   return OK;
 }

 void DestroyTree()
 { /* 因爲PTree是定長類型,沒法銷燬 */
 }

 typedef struct
 {
   int num;
   TElemType name;
 }QElemType; /* 定義隊列元素類型 */
 #include"c3-2.h" /* 定義LinkQueue類型 */
 #include"bo3-2.c" /* LinkQueue類型的基本操做 */
 Status CreateTree(PTree *T)
 { /* 操做結果: 構造樹T */
   LinkQueue q;
   QElemType p,qq;
   int i=1,j,l;
   char c[MAX_TREE_SIZE]; /* 臨時存放孩子結點數組 */
   InitQueue(&q); /* 初始化隊列 */
   printf("請輸入根結點(字符型,空格爲空): ");
   scanf("%c%*c",&(*T).nodes[0].data); /* 根結點序號爲0,%*c吃掉回車符 */
   if((*T).nodes[0].data!=Nil) /* 非空樹 */
   {
     (*T).nodes[0].parent=-1; /* 根結點無雙親 */
     qq.name=(*T).nodes[0].data;
     qq.num=0;
     EnQueue(&q,qq); /* 入隊此結點 */
     while(i<MAX_TREE_SIZE&&!QueueEmpty(q)) /* 數組未滿且隊不空 */
     {
       DeQueue(&q,&qq); /* 出隊一個結點 */
       printf("請按長幼順序輸入結點%c的全部孩子: ",qq.name);
       gets(c);
       l=strlen(c);
       for(j=0;j<l;j++)
       {
         (*T).nodes[i].data=c[j];
         (*T).nodes[i].parent=qq.num;
         p.name=c[j];
         p.num=i;
         EnQueue(&q,p); /* 入隊此結點 */
         i++;
       }
     }
     if(i>MAX_TREE_SIZE)
     {
       printf("結點數超過數組容量\n");
       exit(OVERFLOW);
     }
     (*T).n=i;
   }
   else
     (*T).n=0;
   return OK;
 }

 #define ClearTree InitTree /* 兩者操做相同 */

 Status TreeEmpty(PTree T)
 { /* 初始條件: 樹T存在。操做結果: 若T爲空樹,則返回TRUE,不然返回FALSE */
   if(T.n)
     return FALSE;
   else
     return TRUE;
 }

 int TreeDepth(PTree T)
 { /* 初始條件: 樹T存在。操做結果: 返回T的深度 */
   int k,m,def,max=0;
   for(k=0;k<T.n;++k)
   {
     def=1; /* 初始化本際點的深度 */
     m=T.nodes[k].parent;
     while(m!=-1)
     {
       m=T.nodes[m].parent;
       def++;
     }
     if(max<def)
       max=def;
   }
   return max; /* 最大深度 */
 }

 TElemType Root(PTree T)
 { /* 初始條件: 樹T存在。操做結果: 返回T的根 */
   int i;
   for(i=0;i<T.n;i++)
     if(T.nodes[i].parent<0)
       return T.nodes[i].data;
   return Nil;
 }

 TElemType Value(PTree T,int i)
 { /* 初始條件: 樹T存在,i是樹T中結點的序號。操做結果: 返回第i個結點的值 */
   if(i<T.n)
     return T.nodes[i].data;
   else
     return Nil;
 }

 Status Assign(PTree *T,TElemType cur_e,TElemType value)
 { /* 初始條件: 樹T存在,cur_e是樹T中結點的值。操做結果: 改cur_e爲value */
   int j;
   for(j=0;j<(*T).n;j++)
   {
     if((*T).nodes[j].data==cur_e)
     {
       (*T).nodes[j].data=value;
       return OK;
     }
   }
   return ERROR;
 }

 TElemType Parent(PTree T,TElemType cur_e)
 { /* 初始條件: 樹T存在,cur_e是T中某個結點 */
   /* 操做結果: 若cur_e是T的非根結點,則返回它的雙親,不然函數值爲"空" */
   int j;
   for(j=1;j<T.n;j++) /* 根結點序號爲0 */
     if(T.nodes[j].data==cur_e)
       return T.nodes[T.nodes[j].parent].data;
   return Nil;
 }

 TElemType LeftChild(PTree T,TElemType cur_e)
 { /* 初始條件: 樹T存在,cur_e是T中某個結點 */
   /* 操做結果: 若cur_e是T的非葉子結點,則返回它的最左孩子,不然返回"空" */
   int i,j;
   for(i=0;i<T.n;i++)
     if(T.nodes[i].data==cur_e) /* 找到cur_e,其序號爲i */
       break;
   for(j=i+1;j<T.n;j++) /* 根據樹的構造函數,孩子的序號>其雙親的序號 */
     if(T.nodes[j].parent==i) /* 根據樹的構造函數,最左孩子(長子)的序號<其它孩子的序號 */
       return T.nodes[j].data;
   return Nil;
 }

 TElemType RightSibling(PTree T,TElemType cur_e)
 { /* 初始條件: 樹T存在,cur_e是T中某個結點 */
   /* 操做結果: 若cur_e有右(下一個)兄弟,則返回它的右兄弟,不然返回"空" */
   int i;
   for(i=0;i<T.n;i++)
     if(T.nodes[i].data==cur_e) /* 找到cur_e,其序號爲i */
       break;
   if(T.nodes[i+1].parent==T.nodes[i].parent)
   /* 根據樹的構造函數,若cur_e有右兄弟的話則右兄弟緊接其後 */
     return T.nodes[i+1].data;
   return Nil;
 }

 Status Print(PTree T)
 { /* 輸出樹T。加 */
   int i;
   printf("結點個數=%d\n",T.n);
   printf(" 結點 雙親\n");
   for(i=0;i<T.n;i++)
   {
     printf("    %c",Value(T,i)); /* 結點 */
     if(T.nodes[i].parent>=0) /* 有雙親 */
       printf("    %c",Value(T,T.nodes[i].parent)); /* 雙親 */
     printf("\n");
   }
   return OK;
 }

 Status InsertChild(PTree *T,TElemType p,int i,PTree c)
 { /* 初始條件: 樹T存在,p是T中某個結點,1≤i≤p所指結點的度+1,非空樹c與T不相交 */
   /* 操做結果: 插入c爲T中p結點的第i棵子樹 */
   int j,k,l,f=1,n=0; /* 設交換標誌f的初值爲1,p的孩子數n的初值爲0 */
   PTNode t;
   if(!TreeEmpty(*T)) /* T不空 */
   {
     for(j=0;j<(*T).n;j++) /* 在T中找p的序號 */
       if((*T).nodes[j].data==p) /* p的序號爲j */
         break;
     l=j+1; /* 若是c是p的第1棵子樹,則插在j+1處 */
     if(i>1) /* c不是p的第1棵子樹 */
     {
       for(k=j+1;k<(*T).n;k++) /* 從j+1開始找p的前i-1個孩子 */
         if((*T).nodes[k].parent==j) /* 當前結點是p的孩子 */
         {
           n++; /* 孩子數加1 */
           if(n==i-1) /* 找到p的第i-1個孩子,其序號爲k1 */
             break;
         }
       l=k+1; /* c插在k+1處 */
     } /* p的序號爲j,c插在l處 */
     if(l<(*T).n) /* 插入點l不在最後 */
       for(k=(*T).n-1;k>=l;k--) /* 依次將序號l之後的結點向後移c.n個位置 */
       {
         (*T).nodes[k+c.n]=(*T).nodes[k];
         if((*T).nodes[k].parent>=l)
           (*T).nodes[k+c.n].parent+=c.n;
       }
     for(k=0;k<c.n;k++)
     {
       (*T).nodes[l+k].data=c.nodes[k].data; /* 依次將樹c的全部結點插於此處 */
       (*T).nodes[l+k].parent=c.nodes[k].parent+l;
     }
     (*T).nodes[l].parent=j; /* 樹c的根結點的雙親爲p */
     (*T).n+=c.n; /* 樹T的結點數加c.n個 */
     while(f)
     { /* 從插入點以後,將結點仍按層序排列 */
       f=0; /* 交換標誌置0 */
       for(j=l;j<(*T).n-1;j++)
         if((*T).nodes[j].parent>(*T).nodes[j+1].parent)
         {/* 若是結點j的雙親排在結點j+1的雙親以後(樹沒有按層序排列),交換兩結點*/
           t=(*T).nodes[j];
           (*T).nodes[j]=(*T).nodes[j+1];
           (*T).nodes[j+1]=t;
           f=1; /* 交換標誌置1 */
           for(k=j;k<(*T).n;k++) /* 改變雙親序號 */
             if((*T).nodes[k].parent==j)
               (*T).nodes[k].parent++; /* 雙親序號改成j+1 */
             else if((*T).nodes[k].parent==j+1)
               (*T).nodes[k].parent--; /* 雙親序號改成j */
         }
     }
     return OK;
   }
   else /* 樹T不存在 */
     return ERROR;
 }

 Status deleted[MAX_TREE_SIZE+1]; /* 刪除標誌數組(全局量) */
 void DeleteChild(PTree *T,TElemType p,int i)
 { /* 初始條件: 樹T存在,p是T中某個結點,1≤i≤p所指結點的度 */
   /* 操做結果: 刪除T中結點p的第i棵子樹 */
   int j,k,n=0;
   LinkQueue q;
   QElemType pq,qq;
   for(j=0;j<=(*T).n;j++)
     deleted[j]=0; /* 置初值爲0(不刪除標記) */
   pq.name='a'; /* 此成員不用 */
   InitQueue(&q); /* 初始化隊列 */
   for(j=0;j<(*T).n;j++)
     if((*T).nodes[j].data==p)
       break; /* j爲結點p的序號 */
   for(k=j+1;k<(*T).n;k++)
   {
     if((*T).nodes[k].parent==j)
       n++;
     if(n==i)
       break; /* k爲p的第i棵子樹結點的序號 */
   }
   if(k<(*T).n) /* p的第i棵子樹結點存在 */
   {
     n=0;
     pq.num=k;
     deleted[k]=1; /* 置刪除標記 */
     n++;
     EnQueue(&q,pq);
     while(!QueueEmpty(q))
     {
       DeQueue(&q,&qq);
       for(j=qq.num+1;j<(*T).n;j++)
         if((*T).nodes[j].parent==qq.num)
         {
           pq.num=j;
           deleted[j]=1; /* 置刪除標記 */
           n++;
           EnQueue(&q,pq);
         }
     }
     for(j=0;j<(*T).n;j++)
       if(deleted[j]==1)
       {
         for(k=j+1;k<=(*T).n;k++)
         {
           deleted[k-1]=deleted[k];
           (*T).nodes[k-1]=(*T).nodes[k];
           if((*T).nodes[k].parent>j)
             (*T).nodes[k-1].parent--;
         }
         j--;
       }
     (*T).n-=n; /* n爲待刪除結點數 */
   }
 }

 void TraverseTree(PTree T,void(*Visit)(TElemType))
 { /* 初始條件:二叉樹T存在,Visit是對結點操做的應用函數 */
   /* 操做結果:層序遍歷樹T,對每一個結點調用函數Visit一次且僅一次 */
   int i;
   for(i=0;i<T.n;i++)
     Visit(T.nodes[i].data);
   printf("\n");
 }
 /* main6-4.c 檢驗bo6-4.c的主程序 */
 #include"c1.h"
 typedef char TElemType;
 TElemType Nil=' '; /* 以空格符爲空 */
 #include"c6-4.h"
 #include"bo6-4.c"

 void vi(TElemType c)
 {
   printf("%c ",c);
 }

 void main()
 {
   int i;
   PTree T,p;
   TElemType e,e1;
   InitTree(&T);
   printf("構造空樹後,樹空否? %d(1:是 0:否) 樹根爲%c 樹的深度爲%d\n",TreeEmpty(T),Root(T),TreeDepth(T));
   CreateTree(&T);
   printf("構造樹T後,樹空否? %d(1:是 0:否) 樹根爲%c 樹的深度爲%d\n",TreeEmpty(T),Root(T),TreeDepth(T));
   printf("層序遍歷樹T:\n");
   TraverseTree(T,vi);
   printf("請輸入待修改的結點的值 新值: ");
   scanf("%c%*c%c%*c",&e,&e1);
   Assign(&T,e,e1);
   printf("層序遍歷修改後的樹T:\n");
   TraverseTree(T,vi);
   printf("%c的雙親是%c,長子是%c,下一個兄弟是%c\n",e1,Parent(T,e1),LeftChild(T,e1),RightSibling(T,e1));
   printf("創建樹p:\n");
   InitTree(&p);
   CreateTree(&p);
   printf("層序遍歷樹p:\n");
   TraverseTree(p,vi);
   printf("將樹p插到樹T中,請輸入T中p的雙親結點 子樹序號: ");
   scanf("%c%d%*c",&e,&i);
   InsertChild(&T,e,i,p);
   Print(T);
   printf("刪除樹T中結點e的第i棵子樹,請輸入e i: ");
   scanf("%c%d",&e,&i);
   DeleteChild(&T,e,i);
   Print(T);
 }
相關文章
相關標籤/搜索