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

【C語言】資料結構C語言版 實驗7 二叉樹

/*
編寫演算法函式void preorder1(bintree t)實現二叉樹t的非遞迴前序遍歷。
*/
#include "bintree.h"
char *a="ABC##D#E##F##";  /*擴充二叉樹序樹t的前序序列*/
/*函式preorder1()的功能是非遞迴前序遍歷二叉樹t,請將函式補充完整並除錯執行*/
void preorder1(bintree t)
{
    seqstack s;//順序棧s 
    s.top=0;
    while((t) || (s.top!=0)) //當前處理的子樹不為空或棧不為空則迴圈 
    {
        if(t)
        {
            printf(
"%c ",t->data); push(&s,t); t=t->lchild; } else { t=pop(&s); t=t->rchild; } } } int main() { bintree t; t=creatbintree(); /*建立二叉樹t的儲存結構*/ printf("二叉樹的前序序列為:\n"); preorder1(t); /*前序非遞迴遍歷二叉樹
*/ return 0; }
/*
編寫演算法函式void levelbintree(bintree t),實現二叉樹的層次遍歷。
*/
#include "bintree.h"
char *a="ABC##D#E##F##";              /*擴充二叉樹序樹t的前序序列*/
void levelbintree(bintree t)
{
    bintree queue[100];
    int f=0,r=1;
    bintree p;
    queue[0]=t;
    while(f<r)
    {
        p=queue[f]; f++; printf("
%c",p->data); if(p->lchild) queue[r++]=p->lchild; if(p->rchild) queue[r++]=p->rchild; } } int main() { bintree t; t=creatbintree(); /*建立二叉樹t的儲存結構*/ printf("二叉樹的層次序列為:\n"); levelbintree(t); /*層次遍歷二叉樹*/ return 0; }
/*
編寫函式bintree prelist(bintree t),bintree postfirst(bintree t),
分別返回二叉樹t在前序遍歷下的最後一個結點地址和後序遍歷下的第一個結點地址。
*/
#include "bintree.h"
char *a="ABC##D##EF#G###";  /*擴充二叉樹序樹t的前序序列*/
bintree prelast(bintree t)
{    //右下葉子結點                                                
    bintree p;
    if(t)
    {
        p=t;
        while(p&&p->lchild||p->rchild)
        {
            if(p->rchild)
            {
                p=p->rchild;
            }
            else
            {
                p=p->lchild;
            }
        }
    }
    return p; //返回前序序列最後一個結點G 
}
bintree postfirst(bintree t)
{                //後序遍歷是左子樹-右子樹-根結點 ,二叉樹的左下葉子結點是第一個 
    bintree p;
    if(t)
    {
        while(p&&p->lchild||p->rchild)
        {
            if(p->lchild)
            {
                p=p->lchild;
            }
            else
            {
                p=p->rchild;
            }
        }
    }
    return p;//返回後序序列第一個結點 C
}
int main()
{   bintree t,p,q;
    t=creatbintree();       /*建立二叉樹t的儲存結構*/
     p=prelast(t);
    //q=postfirst(t);
    if (t!=NULL)
            { printf("前序遍歷最後一個結點為:%c\n",p->data);
              // printf("後序遍歷第一個結點為:%c\n",q->data);
            }
     else    printf("二叉樹為空!");
    return 0;
}
/*
假設二叉樹採用鏈式方式儲存,t為其根結點,編寫一個函式int Depth(bintree t, char x),求值為x的結點在二叉樹中的層次。
*/
#include "bintree.h"
char *a="ABC##D##EF#G###";          /*擴充二叉樹序樹t的前序序列*/
/*
     函式Depth,功能:求結點x所在的層次
*/
int Depth(bintree t,char x)
{
    int num1,num2,n;//num1,num2分別記錄在左子樹,右子樹中查詢到x的層數,n記錄最終返回的結果層數
    if(t==NULL)
    {
        return 0;
    }
    else
    {
        if(t->data==x)
        {
            return 1;
        }
        num1=Depth(t->lchild,x);
        num2=Depth(t->rchild,x);
        n=num1+num2; //num1和num2之中必有一個為0 
        if(num1!=0||num2!=0) //找到了x ,往回數 
        {
            n++;
        }
    }
    return n;
}
int main()
{  bintree root;
   char x;
   int k=0;
   root=creatbintree();
   printf("請輸入樹中的1個結點值:\n");
   scanf("%c",&x);
   k=Depth(root,x);
   printf("%c結點的層次為%d\n",x,k);
}
 
/*
   試編寫一個函式,將一棵給定二叉樹中所有結點的左、右子女互換。
*/
#include "bintree.h"
char *a="ABC##D##EF#G###";          /*擴充二叉樹序樹t的前序序列*/
/*請將本函式補充完整,並進行測試*/
void change(bintree t)
{
    bintree p;
    if(t)
    {
        p=t->lchild; //交換 
        t->lchild=t->rchild;
        t->rchild=p;
        change(t->lchild); //繼續遞迴 
        change(t->rchild);
    }
}
int main()
{  bintree root;
   root=creatbintree();
   change(root);
   preorder(root);
}
/*
試編寫一個遞迴函式bintree buildBintree(char *pre, char *mid, int length),
根據二叉樹的前序序列pre、中序序列mid和前序序列長度length,構造二叉樹的二叉連結串列儲存結構,
函式返回二叉樹的樹根地址。
*/
#include "bintree.h"
#include <string.h>
char *a="";
/*請將本函式補充完整,並進行測試*/
bintree buildBintree(char *pre, char *mid,int length)
{
    bintree t;
    int i=0;
    if(length)
    {
        t=(bintree)malloc(sizeof(binnode)); //生成新結點
        t->data=pre[i];
        while(i<length&&mid[i]!=pre[0])     //在中序遍歷中查詢根結點的位置
            i++;
        t->lchild=buildBintree(pre+1,mid,i);
        t->rchild=buildBintree(pre+i+1,mid+i+1,length-i-1);
    }
    else
        return NULL;
    return t;
}
int main()
{   bintree root;
    char pre[100],mid[100];
    puts("請輸入前序序列:");
    gets(pre);
    puts("請輸入中序序列:");
    gets(mid);
    root=buildBintree(pre,mid,strlen(pre));
    puts("後序序列是:");
    postorder(root);
}
/*bintree.h標頭檔案*/
#include<stdio.h>
#include<stdlib.h>
#define N 100
extern char *a;
typedef struct node
{    
    char data;
    struct node *lchild, *rchild;
}binnode;
typedef binnode *bintree;
/*函式creatbintree(根據擴充二叉樹的前序序列(字元a)建立二叉樹t的儲存結構)*/
binnode  creatbintree()
{
    char ch = *a++;
    bintree t;
    if (ch == '#') t = NULL;
    else
    {
        t = (bintree)malloc(sizeof(binnode));
        t->data = ch;
        t->lchild = creatbintree();
        t->rchild = creatbintree();
    }
    return t;
}