【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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM