1. 程式人生 > 實用技巧 >【C語言】資料結構C語言版 實驗6 樹

【C語言】資料結構C語言版 實驗6 樹

/*
編寫演算法函式void levelorder(tree t)實現樹的層次遍歷。
*/
#include "tree.h"
void levelorder(tree t)    /* t為指向樹根結點的指標*/
{
    tree queue[100];
    int f,r,i; //f,r分別為隊頭和隊尾指標
    tree p;
    f=0; r=1; queue[0]=t;
    while(f<r)
    {
        p=queue[f] ;f++;printf("%c",p->data);  //訪問隊頭元素
        for(i=0;i<m;i++) //將剛被訪問的元素的所有子女結點依次進隊 
{ if(p->child[i]) { queue[r]=p->child[i]; r++; } } } } int main() { tree t; printf("please input the preorder sequence of the tree:\n"); t=createtree(); printf("\nthe levelorder is:"); levelorder(t);
return 0; }
/*
假設樹採用指標方式的孩子表示法表示,試編寫一個非遞迴函式void PreOrder1(tree root),實現樹的前序遍歷演算法。
*/
#include "tree.h"
void  PreOrder1(tree root)
{
    tree stack[MAXLEN];
    int top=-1,i;
    while(top!=-1||root)
    {
        if(root)                        //子樹不為空
        {
            printf("%c",root->data);
            
for(i=m-1;i>0;i--) { if(root->child[i]!=NULL) //將不為空的子結點進棧 stack[++top]=root->child[i]; } root=root->child[0]; //從第一個子結點開始遍歷 } else //子樹為空,出棧 { root=stack[top--]; } } } int main () { tree root; printf("please input the preorder sequence of the tree:\n"); root =createtree(); printf("前序序列是:\n"); PreOrder1(root); return 0; }
/*
   假設樹採用指標方式的孩子表示法表示,試編寫一個非遞迴函式void PostOrder1(tree t),實現樹的後序遍歷演算法。
*/
#include "tree.h"
int PostOrder1(tree root)
{
    tree treeStack[MAXLEN];     //待處理結點
    tree overStack[MAXLEN];     //已處理完的結點
    int top=-1,top1=-1,i;
    if(root)
        treeStack[++top]=root; //根結點進棧
    while(top!=-1)
    {
        root=treeStack[top--];  //取一個待處理結點
        for(i=0;i<m;i++)        //將root的所有子結點進棧
        {
            if(root->child[i])
                treeStack[++top]=root->child[i];
        }
        overStack[++top1]=root; //處理完root,將root入overStack
    }
    while(top1!=-1)             //輸出
        printf("%c",overStack[top1--]->data);
}
int main ()
{
    tree root;
    printf("please input the preorder sequence of the tree:\n");
    root =createtree();
    printf("後序序列是:\n");
    PostOrder1(root);
    return 0;
}
 
/*
假設樹採用指標方式的孩子表示法表示,試編寫一個函式int equal(tree t1, tree t2),
判斷兩棵給定的樹是否等價(兩棵樹等價當且僅當其根結點的值相等且其對應的子樹均相互等價)。
*/
#include "tree.h"
#define TRUE  1
#define FALSE 0
int equal(tree t1,tree t2)
{
    int flag=TRUE,i;
    if(!t1&&!t2)                //若兩樹為空,則相等
        return TRUE;
    else if(!t1&&t2||t1&&!t2)   //若有一樹為空,則不相等
        return FALSE;
    else if(t1->data!=t2->data)
        return FALSE;
    else
    {
        for(i=0;i<m;i++)
        {
            if(t1->child[i]==t2->child[i])
                flag=flag&&equal(t1->child[i],t2->child[i]);
        }
        return flag;
    }
}
int main ()
{
    tree t1,t2;
    printf("please input the preorder sequence of the tree:\n");
    t1=createtree();
    getchar();
    printf("please input the preorder sequence of the tree:\n");
    t2=createtree();
    if ( equal(t1,t2) == TRUE)
    {
        printf ("兩樹相等\n");
    }
    else
    {
        printf ("兩樹不相等\n");
    }
    return 0;
}
/*
假設樹採用指標方式的孩子表示法儲存結構,試編寫一個函式tree Ct(char s[]),
根據輸入的樹的括號表示字串s,生成樹的儲存結構。例如,若要建立教材圖6.4所示的樹,
應輸入A(B(E,F),C,D(G(I,J,K),H))。(說明,tree.h中定義的常量m表示樹的最
大度,請根據建樹的需要自行修改m的值)
 
*/
#include "tree.h"
/*請將本函式補充完整,並進行測試*/
tree Ct(char s[MAXLEN])
{
   tree stack[MAXLEN],parent=NULL,child=NULL;
   int n=0,top=0,done=0,i;
   while(done==0&&s[n]!='\0')   //未完成並且字串不為空
   {
       if(s[n]=='(')            //遇到左括號,則其前面的結點成為父結點
       {
           n++;
           parent=child;
           stack[top++]=parent; //將父結點進棧
       }
       else if(s[n]==')')       //遇到右括號,父結點出棧
       {
           n++;
           parent=stack[--top];
           if(top==0)           //若棧頂無元素,則結束
            done=1;
           else
            parent=stack[top-1];//換下一個父結點收集子結點
       }
       else if(s[n]==',')
        n++;
       else
       {
           child=(tree)malloc(sizeof(node));
           child->data=s[n];
           for(i=0;i<m;i++)
            child->child[i]=NULL;
           if(parent!=NULL)      //若父結點不為空,則將其掛到父結點上
           {
               for(i=0;i<m;i++)
                if(parent->child[i]==NULL)
                {
                    parent->child[i]=child;
                    break;
                }
           }
           n++;
       }
   }
   return parent;
}
int main ()
{
    char s[MAXLEN];
    tree root = NULL;
    printf ("請用樹的括號表示法輸入一棵樹:\n");
    scanf ("%s",s);
    root = Ct(s);
    preorder(root);  /*前序遍歷樹*/
    return 0;
}