主题:求助郝夫曼编码算法(程序里就一部分写的不好)
//--------------------------part 1
#include "stdafx.h"
# include <stdio.h>
# include <malloc.h>
#include <string.h>
# define ERROR -1
# define OK 1
//--------以下用户按照具体问题设定--------------------
typedef char TElemType; //指定char为二叉树结点元素类型
void visit (char e)
{ printf ("---%c---\n",e); }
//----------------------------------------------------
//-----------以下实现二叉树二叉链表的几个基本操作------------
typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode, *BiTree;
typedef struct { char *ch;
int length;
} HString;
void CreateBiTree(BiTree &T)
{ char ch;
scanf ("%c",&ch);
if (ch=='#') T=NULL;
else {
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
};
}
void CountLeaf1 (BiTree T,int & count)
{ if (T) {
if ((!T->lchild)&&(!T->rchild)) count++;
CountLeaf1 (T->lchild,count);
CountLeaf1 (T->rchild,count);
};
}
int CountLeaf2 (BiTree T)
{
if (!T) return 0;
if ((!T->lchild)&&(!T->rchild)) return 1;
return CountLeaf2(T->lchild)+CountLeaf2(T->rchild);
}
void CountNode (BiTree T,int & count)
{
if (T!=NULL)
{
count++;
CountNode (T->lchild,count);
CountNode (T->rchild,count);
};
}
int CountDepth (BiTree T)
{
int templ,tempr;
if (!T) return 0;
else { templ=CountDepth(T->lchild);
tempr=CountDepth(T->rchild);
return (templ>=tempr)?templ+1:tempr+1;
};
}
int PreOrderTraverse (BiTree T, void (*visit)(TElemType e))
{ if (T) { visit (T->data);
PreOrderTraverse (T->lchild, visit);
PreOrderTraverse (T->rchild, visit);
};
return OK;
}
int InOrderTraverse (BiTree T, void (*visit)(TElemType e))
{ if (T) { InOrderTraverse (T->lchild, visit);
visit (T->data);
InOrderTraverse (T->rchild, visit);
};
return OK;
}
int PostOrderTraverse (BiTree T, void (*visit)(TElemType e))
{ if (T) { PostOrderTraverse (T->lchild, visit);
PostOrderTraverse (T->rchild, visit);
visit (T->data);
};
return OK;
}
int IsFullTree(BiTree T)//判断是否是满二叉树
{
int i=CountDepth (T);
int j=CountLeaf2 (T);
if(j==i*i-1)return OK;
else return ERROR;
}
#include "stdafx.h"
# include <stdio.h>
# include <malloc.h>
#include <string.h>
# define ERROR -1
# define OK 1
//--------以下用户按照具体问题设定--------------------
typedef char TElemType; //指定char为二叉树结点元素类型
void visit (char e)
{ printf ("---%c---\n",e); }
//----------------------------------------------------
//-----------以下实现二叉树二叉链表的几个基本操作------------
typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode, *BiTree;
typedef struct { char *ch;
int length;
} HString;
void CreateBiTree(BiTree &T)
{ char ch;
scanf ("%c",&ch);
if (ch=='#') T=NULL;
else {
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
};
}
void CountLeaf1 (BiTree T,int & count)
{ if (T) {
if ((!T->lchild)&&(!T->rchild)) count++;
CountLeaf1 (T->lchild,count);
CountLeaf1 (T->rchild,count);
};
}
int CountLeaf2 (BiTree T)
{
if (!T) return 0;
if ((!T->lchild)&&(!T->rchild)) return 1;
return CountLeaf2(T->lchild)+CountLeaf2(T->rchild);
}
void CountNode (BiTree T,int & count)
{
if (T!=NULL)
{
count++;
CountNode (T->lchild,count);
CountNode (T->rchild,count);
};
}
int CountDepth (BiTree T)
{
int templ,tempr;
if (!T) return 0;
else { templ=CountDepth(T->lchild);
tempr=CountDepth(T->rchild);
return (templ>=tempr)?templ+1:tempr+1;
};
}
int PreOrderTraverse (BiTree T, void (*visit)(TElemType e))
{ if (T) { visit (T->data);
PreOrderTraverse (T->lchild, visit);
PreOrderTraverse (T->rchild, visit);
};
return OK;
}
int InOrderTraverse (BiTree T, void (*visit)(TElemType e))
{ if (T) { InOrderTraverse (T->lchild, visit);
visit (T->data);
InOrderTraverse (T->rchild, visit);
};
return OK;
}
int PostOrderTraverse (BiTree T, void (*visit)(TElemType e))
{ if (T) { PostOrderTraverse (T->lchild, visit);
PostOrderTraverse (T->rchild, visit);
visit (T->data);
};
return OK;
}
int IsFullTree(BiTree T)//判断是否是满二叉树
{
int i=CountDepth (T);
int j=CountLeaf2 (T);
if(j==i*i-1)return OK;
else return ERROR;
}