c语言如何处理二叉树编码,关于二叉树的基本操作和输出哈夫曼编码的C语言程序.doc...

注:本程序主要是关于二叉树的一些基本操作,包括前序遍历,中序遍历,后序遍历以及求出总结点以及叶子结点的数目(本程序在输入时是默认以先序序列输入各个结点的数值。如果是其它方式,则三个遍历程序会有略微变化)以及哈夫曼编码的输出。

以下程序除汉字外均为在英文环境中编写,可直接复制到VC编程软件中运行。

#include

#include

#include

#include //getch()所需

int num;

typedef struct node //定义二叉树的结构体

{

char data;

struct node *lchild,*rchild;

}BinTNode,*BinTree;

BinTree T;

typedef struct //定义哈夫曼的结构体

{

int weight;

int parent,lchild,rchild;

}HTNode,*HuffmanTree;

HuffmanTree p;

typedef char * *HuffmanCode;

void CreateBinTree(BinTree &T) //建树

{

char ch;

ch=getch();

if (ch==' ')

{

printf("_");

T=NULL;

}

else

{

printf("%c",ch);

T=(BinTNode *)malloc(sizeof(BinTNode));

T->data=ch;

CreateBinTree(T->lchild );

CreateBinTree(T->rchild );

}

}

void Preorder(BinTree &T) //先序遍历

{

if (T)

{

printf("%c",T->data);

Preorder(T->lchild);

Preorder(T->rchild);

}

}

void P() //先序遍历的调用

{

system("cls");

printf("先序遍历的结果如下:\n\n\n\n");

Preorder(T);

}

void Inorder(BinTree &T) //中序遍历

{

if(T)

{

Inorder(T->lchild);

printf("%c",T->data);

Inorder(T->rchild);

}

}

void I() //中序遍历的调用

{

system("cls");

printf("中序遍历的结果如下:\n\n\n\n");

Inorder(T);

}

void Postorder(BinTree &T) //后序遍历

{

if(T)

{

Postorder(T->lchild);

Postorder(T->rchild);

printf("%c",T->data);

}

}

void Po() //后序遍历的调用

{

system("cls");

printf("后序遍历的结果如下:\n\n\n\n");

Postorder(T);

}

int Ylen(BinTree &T) //求叶子结点个数

{

if (T)

{

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

num++;

Ylen(T->lchild);

Ylen(T->rchild);

}

return num;

}

int Slen(BinTree &T) //求总结点个数

{

if (T)

{

num++;

Slen(T->lchild);

Slen(T->rchild);

}

return num;

}

void l() //将总结点以及叶子结点的数目进行输出

{

system("cls");

num=0;

printf("叶子结点的个数为:%d\n\n\n\n",Ylen(T));

num=0;

printf("总结点的个数为:%d\n\n\n\n",Slen(T));

}

void m() //关于二叉树的操作总函数

{

int h;

system("cls");

printf("创造一个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值