数据结构:树
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
typedef enum PointerTag{Link, Thread};
typedef struct BiThrNode
{
TElemType data;
struct BiThrNode *lchild, *rchild;
PointerTag LTag, RTag;
}BiThrNode, *BiThrTree;
typedef struct CSNode
{
TElemType data;
struct CSNode *firstchild, *nextsibling;
}CSNode, *CSTree;
typedef struct
{
unsigned int weight;
unsigned int parent, lchild, rchild;
}HTNode, *HuffmanTree;
int BiTreeDepth(BiTree T)
{
int i, j;
if(!T)
return 0;
if(T->lchild)
i = BiTreeDepth(T->lchild);
else
i = 0;
if(T->rchild)
j = BiTreeDepth(T->rchild);
else
j = 0;
return i > j ? i + 1 : j + 1;
}
void PreOrder(BiTree T void(*Visit)(TElemType))
{
if(T != NULL)
{
Visit(T);
PreOrder(T->lchild);
PreOrder(T->rchlid);
}
}
void InOrderTraverse1(BiTree T, void(*Visit)(TELemType))
{
Sqstack S;
InitStack(S);
while(T || !StackEmpty(S))
{
if(T)
{
Push(S, T);
T = T ->lchild;
}
else
{
Pop(S, T);
Visit(T->data);
Push(T->rchild);
}
}
}
void InOrderTraverse2(BiTree T, void(*Visit)(TElemType))
{
SqStack S;
BiTree p;
InitStack(S);
Push(S, T)
while(!StackEmpty(S))
{
while(Getop(S, p) && p)
Push(S, p->lchild);
Pop(S, p);
if(!StackEmpty(S))
{
Pop(S, p)
Visit(p->data);
Push(S, p->rchild);
}
}
}
void PostOrder(BiTree T, void(*Visit)(TElemType))
{
SqStack S;
InitStack(S);
BiTree p;
p = T;
int Tag[];
while(p || !StackEmpty(S))
{
while(p)
{
Push(S, p);
Tag[Stack->top] = 0;
p = p->lchild;
}
while(!StackEmpty(S) && Tag[Stack->top] == 1)
{
Pop(S, p);
Visit(p->data);
}
if(!StackEmpty(S))
{
Tag[S->top] = 1;
GetTop(S, p);
p = p->rchild;
}
}
}
void levelOrder(BiTree T, void(*Visit)(TElemType))
{
LinkQueue q;
QElemType a;
if(T)
{
InitQueue(q);
EnQueue(q, T);
while(!QueueEmpty)
{
DeQueue(q, a);
Visit(a->data);
if(a->lchild != NULL)
EnQueue(q, a->lchild);
if(a->rchild != NULL)
EnQueue(q, a->rchild);
}
}
}
TElemType Nil = ' ';
TElemType Parent(BiTree T, TElemType e)
{
LinkQueue q;
QElemType a;
if(T)
{
InitQueue(q);
EnQueue(q, T);
while(!QueueEmpty(q))
{
DeQueue(q, a);
if(a->lchild && a->lchild->data = e || a->rchild && a->rchild->data = e )
return a->data;
else
{
if(a->lchild)
EnQueue(q, a->lchild);
if(a->rchild)
EnQueue(q, a->rchild);
}
}
}
}
BiThree pre;
void InThreading(BiThrTree p)
{
if(p)
{
InThreading(p->lchild);
if(!p->lchild)
{
p->LTag = Thred;
p->lchild = pre;
}
if(!pre->rchild)
{
pre->RTag = Thread;
pre->rchild = p;
}
pre = p;
InThreading(p->rchild);
}
}
Status InOrderThreading(BiTree &Thrt, BiThrTree T)
{
if(!(Thrt =(BiTree)malloc(sizeof(BiThrNode))))
exit(OVERFLOW);
Thrt->LTag = Link;
Thrt->RTag = Thread;
Thrt->rchild = Thrt;
if(!T)
Thrt->lchild = Thrt;
else
{
Thrt->lchild = T;
InThreading(T); /
pre->rchild = Thrt;
pre->RTag = Thread;
Thrt->rchild = pre;
}
return OK;
}
BiThrNode InPostPre(BiTree T, BiThrNode p)
{
BiThrNode q;
if(p->RTag == Link)
q = p->rchild;
else if(p->LTag == Link)
q = p->lchild;
else if(p->lchild == NULL)
q = NULL;
else
{
while(p->LTag == Thread && p->lchild != NULL)
p = p->lchild;
if(p->LTag == Link)
q = p->lchild;
else
q = NULL;
}
return q;
}
TElemType Parent(CSTree T, TElemType cur_e, void(*Visit)(TElemType))
{
CSTree p, t;
LinkQueue q;
if(T)
{
if(Visit(T-data) == cur_e)
return Nil;
EneQueue(q, T);
while(!QueueEmpty(q))
{
DeQueue(q, p);
if(p->firstchild)
{
if(p->firstchild->data == cur_e)
return Visit(p->data);
t = p;
p = p->firstchild;
EnQueue(p->nextsibling);
while(p->nextsibling)
{
if(Visit(p->data) == cur_e)
return Visit(p->data);
EnQueue(q, p);
}
}
}
}
return Nil;
}
Status InsertChild(CSTree &T,CSTree p,int i,CSTree c)
{
int j;
if(T)
{
if(i == 1)
{
c->nextsibling = p->firstchild;
p->firstchild = c;
}
else
{
p = p->firstchild;
j = 2;
while(p && j< i)
{
p = p->nextsibling;
j++;
}
if(j == i)
{
c->nextsibling = p->nextsibling;
p->nextsibling = c;
}
else
return ERROR;
}
return OK;
}
else
return ERROR;
}
Status DeleteChild(CSTree &T, CSTree p, int i)
{
CSTree b;
int j;
if(T)
{
if(i == 1)
{
b = p->firstchild;
p->firstchild = b->nextsibling;
b->nextsibling = NULL;
DestroyTree(b);
}
else
{
p = p->firstchild;
j = 2;
while(p && j < i)
{
p = p->nextsibling;
j++;
}
if(j == i)
{
b = p->nextsibling;
p->nextsibling = b->nextsibling;
b->nextsibling = NULL;
DestroyTree(b);
}
else
return ERROR;
}
return OK;
}
else
return ERROR;
}
typedef char **HuffmanCode;
void select(HuffmanTree t, int i, int &s1, int &s2)
{
int j;
s1 = min(t, i);
s2 = min(t, i);
if(s1 > s2)
{
j = s1;
s1 = s2;
s2 = s1;
}
}
void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n)
{
int m, i, s1, s2, start;
unsigned c, f;
HuffmanTree P;
char *cd;
if(n <= 1)
return;
m = 2 * n -1;
HT = (HuffmanTree)malloc((m+1) * sizeof(HTNode));
for(p = HT+1, i = 1; i <= n; ++i, ++p, ++w)
{
(*p).weight = *w;
(*p).parent = 0;
(*p).lchild = 0;
(*p).rchild = 0;
}
for(; i <= m; ++i, ++p)
for(i = n + 1; i <= m; ++i)
{
select(HT, i - 1, s1, s2);
HT[s1].parent = HT[s2].parent = i;
HT[i].lchild = s1;
HT[i].rchild = s2;
HT[i].weight = HT[s1].weight + HT[s2].weight;
}
HC = (HuffmanCode)malloc((n + 1) * sizeof(char *));
cd = (char*)malloc(n *sizeof(char));
cd[n - 1] = "\0";
for(i = 1; i <= n; i++)
{
strat = n - 1;
for(c = i, f = HT[i].parent; f != 0; c = f, f = HT[f].parent)
{
if(HT[f].lchild = c)
cd[--start] = '0';
else
cd[--strat] = '1';
HC[i] = (char*)malloc((n - strat)*sizeof(char));
strcpy(HC[i], &cd[strat]);
}
}
free(cd);
}
typedef struct BiTNode
{
int weight;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
int wpl_PreOrder(BiTree root, int deep)
{
int wpl =0;
if(root->lchild == NULL && root->rchild == NULL)
wpl += deep * root->weight;
if(root->lchild != NULL)
wpl_PreOrder(root->lchild, deep + 1);
if(root->rchild != NULL)
wpl_PreOrder(root->rchild, deep + 1);
return wpl;
}
#define MaxSize 100
int wpl_LevelOrder(BiTree root)
{
BiTree q[MaxSize];
int end1, end2;
end1= end2 = 0;
int wpl = 0,;
int deep = 0;
BiTree LastNode;
BiTree newLastNode;
LastNode = root;
newLastNode = NULL;
while(end1 != end2)
{
BiTree t = q[end1++];
if(t->lchild == NULL && t->rchild == NULL)
wpl += deep * t->weight;
if(t->lchild != NULL)
{
q[end2++] = t->lchild;
newLastNode = t->lchild;
}
if(root->rchild != NULL)
{
q[end2++] = t->rchild;
newLastNode = t->rchild;
}
if(t == LastNode)
{
LastNode = newLastNode;
deep += 1;
}
}
return wpl;
}