作业8——二叉树

1. 二叉树基本操作
#include <stdio.h>

#include <string.h>

#include<malloc.h>

typedef struct node         //二叉树结点类型的定义

{

    char data;

    struct node *lchild;      //定义结点的左孩子指针

    struct node *rchild;      //定义结点的右孩子指针

} BinTNode;


BinTNode *CreateBinTree()  //输入二叉树的先序遍历序列,创建二叉链表

{

    BinTNode *t;

    char ch;

    ch = getchar();

    if (ch == '0')             //如果读入0,创建空树

        t = NULL;

    else
    {
		t = (BinTNode*)malloc(sizeof(BinTNode));
		//if(!t)	exit(-1);
		t->data = ch;
		t->lchild = CreateBinTree();
		t->rchild = CreateBinTree(); 

    }


    return t;

}


void ListBinTree(BinTNode *t)                  //用广义表表示二叉树

{

    if (t != NULL)
    {

        printf("%c", t->data);                //打印数据

        if (t->lchild != NULL || t->rchild != NULL)
        {

            printf("(");                      //打印广义表的左括号

            ListBinTree(t->lchild);           //用广义表表示左子树

            if (t->rchild != NULL)

                printf(",");                    //打印左子树和右子树之间的逗号

            ListBinTree(t->rchild);         //用广义表表示右子树

            printf(")");                    //打印广义表的右括号

        }
    }
}

void preorder(BinTNode *t)   //对二叉树进行先序遍历  注意: 课件中的Visit(T->data)在此

//处是 printf("%3c",t->data),下同。
{
 	if(t)
	{
		printf("%3c",t->data);
		preorder(t->lchild);
		preorder(t->rchild);
	}
}

void inorder(BinTNode *t) //对二叉树进行中序遍历

{
	if(t)
	{
		inorder(t->lchild);
		printf("%3c",t->data);
		inorder(t->rchild);
	}

}


void postorder(BinTNode *t) //对二叉树进行后序遍历

{
	if(t)
	{
		postorder(t->lchild);
		postorder(t->rchild);
		printf("%3c",t->data);
	}



}


int Height(BinTNode *t)//节点深度

{
    if(!t)	return 0;
    return (Height(t->lchild) > Height(t->rchild) ? Height(t->lchild) : Height(t->rchild)) + 1;


}

//二叉树的总结点数

int Size(BinTNode *t)
{


    if(!t)	return 0;
	return 1 + Size(t->lchild) + Size(t->rchild);

}

//二叉树的叶子结点数

int Leaf(BinTNode *t)
{

	if(!t)	return 0;
	if(t->lchild == NULL && NULL == t->rchild)	return 1;
	return 	Leaf(t->lchild) + Leaf(t->rchild);
    
}

//二叉树的最大值

int Max(BinTNode *t)
{

    if (t == NULL) return 0;

    if (t->lchild == NULL && t->rchild == NULL)

        return t->data;

    int l = Max(t->lchild);

    int r = Max(t->rchild);

    return l > r ? l : r;

}


int main()
{

    BinTNode *t = NULL;

    //int TreeHeight;

    printf("请输入先序序列,虚结点用0表示:\n");

    t = CreateBinTree();

    printf("广义表表示的二叉树的输出:\n");

    ListBinTree(t);                          //调用二叉树的广义表表示函数

    printf("\n二叉树的前序遍历结果为:\n");

    preorder(t);                             //调用二叉树先序遍历函数

    printf("\n二叉树的中序遍历结果为:\n");

    inorder(t);                              //调用二叉树中序遍历函数

    printf("\n二叉树的后序遍历结果为:\n");

    postorder(t);                            //调用二叉树后序遍历函数

    printf("\n");

    printf("二叉树的高度为:%d\n", Height(t));

    printf("二叉树的总结点数为:%d\n", Size(t));

    printf("二叉树的叶子结点数为:%d\n", Leaf(t));

    printf("二叉树的最大值为:%c\n", Max(t));
    
    return 0;

}
2. 二叉树的遍历与应用
#include <stdio.h>

#include <malloc.h>

#include <stdlib.h>

#define OVERFLOW 0

#define OK 1

typedef struct node         //二叉树结点类型的定义

{

    char data;

    struct node *lchild; //定义结点的左孩子指针

    struct node *rchild; //定义结点的右孩子指针

} BinTNode, *BTNode;


BinTNode *CreateBinTree()  //输入二叉树的先序遍历序列,创建二叉链表

{

    BinTNode *t;

    char ch;

    ch = getchar();

    if (ch == '0')             //如果读入0,创建空树

        t = NULL;

    else
    {

        t = (BinTNode *) malloc(sizeof(BinTNode)); //申请根结点*t空间

        if (!t) exit(OVERFLOW);

        t->data = ch;                                  //将结点数据ch放入跟结点的数据域

        t->lchild = CreateBinTree();                        //建左子树

        t->rchild = CreateBinTree();                        //建右子树

    }

    return t;

}


void preorder(BTNode t)   //对二叉树进行先序遍历。提示:打印采用 %3c

{

    if (t != NULL)
    {

        printf("%3c", t->data);  //打印节点数据

        preorder(t->lchild);   //先序遍历左子树

        preorder(t->rchild);   //先序遍历右子树

    }

}


void inorder(BTNode t) //对二叉树进行中序遍历。提示:打印采用 %3c

{

    //★★★请补全代码
    if (t != NULL)
    {

        inorder(t->lchild);   //先序遍历左子树 
        
        printf("%3c", t->data);  //打印节点数据

        inorder(t->rchild);   //先序遍历右子树

    }

}

//二叉树的叶子结点数

int Leaf(BTNode t)
{

    //★★★请补全代码
    if(!t)	return 0;
    if(t->lchild == NULL && NULL == t->rchild)		return 1;
	return Leaf(t->lchild) + Leaf(t->rchild);
}



//二叉树的最大值

int Max(BTNode t)
{

    //★★★请补全代码
    if(!t)	return 0;
    if(t->lchild == NULL && NULL == t->rchild)	return t->data;
    return Max(t->lchild) > Max(t->rchild) ? Max(t->lchild) : Max(t->rchild);

}


    int main()
    {

        BTNode t = NULL;

        //int TreeHeight;

        t = CreateBinTree();

        preorder(t);                             //调用二叉树先序遍历函数

        printf("\n");

        inorder(t);                              //调用二叉树中序遍历函数

        printf("\n");

        printf("%d\n", Leaf(t));

        printf("%c\n", Max(t));


    }
3.打印二叉树中的所有从根结点到叶子结点的路径
#include<stdio.h>
#include<malloc.h>

#define OK 1
#define ERROR 0

typedef int TElemType;
typedef int Status;

typedef struct Tnode{
    TElemType data;
    struct Tnode *leftchild;
    struct Tnode *rightchild;
}TNode, *BiTree;  


Status CreateBiTree1(BiTree *T, char a[], int n)
{
    static int count = 0; // 静态变量,用于记录当前处理的序列下标
    if(count >= n) return ERROR; 
    if(a[count] == '#')
    {
        *T = NULL;
        count++;
    }
    else
    {
        *T = (BiTree)malloc(sizeof(TNode)); // 创建新结点
        (*T)->data = a[count++]; // 读取结点的值
        CreateBiTree1(&((*T)->leftchild), a, n); // 递归构建左子树
        CreateBiTree1(&((*T)->rightchild), a, n); // 递归构建右子树
    }
    return OK;
} 

// 打印二叉树
void PrintBiTree(BiTree T)
{
    if(T)
    {
        printf("%c", T->data); // 打印当前结点的值
        PrintBiTree(T->leftchild); // 递归打印左子树
        PrintBiTree(T->rightchild); // 递归打印右子树
    }
}

// 辅助函数:打印路径
void printPath(char path[], int pathLen)
{
    for(int i = 0; i < pathLen; ++i)
        printf("%c", path[i]); // 打印路径
    printf("\n");
}

// 打印从根结点到叶子结点的所有路径
void printAllPaths(BiTree T, char path[], int pathLen)
{
    if(T == NULL) return; 

    path[pathLen++] = T->data; 
    if(T->leftchild == NULL && T->rightchild == NULL) 
    {
        printPath(path, pathLen);
    }
    else 
    {
        printAllPaths(T->leftchild, path, pathLen); // 递归遍历左子树
        printAllPaths(T->rightchild, path, pathLen); // 递归遍历右子树
    }
}

int main()
{
    char arr[1000], ch;
    int len = 0;
    while((ch = getchar()) != EOF)
    {
        if(ch == '\n') 
            break;
        arr[len++] = ch;
    }

    BiTree T = NULL;
    CreateBiTree1(&T, arr, len);

    char path[1000]; // 存储当前路径
    int pathLen = 0; // 当前路径长度
    printAllPaths(T, path, pathLen);

    return 0;
}

4NR010:求任意二叉树最长路径
#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef struct TNode
{
	char data;
	struct TNode *lchild;
	struct TNode *rchild;
}TNode, *BiTree;

void CreateBiTree(BiTree *T)
{
	char ch;
	ch = getchar();
	if(ch == '0')	*T = NULL;
	else
	{
		*T = (BiTree)malloc(sizeof(TNode));
		(*T)->data = ch;
		CreateBiTree(&(*T)->lchild);
		CreateBiTree(&(*T)->rchild);
	}
}

int Max(int a, int b)
{
	return a > b ? a : b;
}

int Height_BiTree(BiTree T)
{
	if(!T)	return 0;
	return Max(Height_BiTree(T->lchild), Height_BiTree(T->rchild))+ 1;
	
}

void PrintBiTree(BiTree T)
{
    if(T)
    {    
        PrintBiTree(T->lchild); // 递归打印左子树
        printf("%c", T->data); // 打印当前结点的值
        PrintBiTree(T->rchild); // 递归打印右子树
    }
}

void PrintPath(char *Path, int Pathlen)
{
	int i = 0;
	//printf("%d",Pathlen); 
	for(i = 0;i < Pathlen;i ++)
	{
		printf("%c->",Path[i]);
	}
	printf("NULL");
}


void printLongestPath(BiTree T,char* path, int pathlen, int count)
{
	if(!T)	return ;
	path[count ++] = T->data;
	if(T->lchild == NULL && NULL == T->rchild && count == pathlen)
	{
		PrintPath(path,pathlen);
		exit(1);
	}
	else
	{
		printLongestPath(T->lchild, path, pathlen, count);
		printLongestPath(T->rchild, path, pathlen, count);
	}
}

void Destroy_BiTree(BiTree *T)
{
	if(*T)
	{
		Destroy_BiTree(&(*T)->lchild);
		Destroy_BiTree(&(*T)->rchild);
		free(*T);
		*T = NULL;
	}
}

int main()
{
	BiTree T;
	
	CreateBiTree(&T);
	
	int pathlen = Height_BiTree(T);
	
	char path[1000];
	
	printLongestPath(T, path, pathlen, 0);
	
	Destroy_BiTree(&T);
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值