二叉树
1准备工作
typedef char ElemType;
struct BTNode
{
ElemType data;
struct BTNode * pL_child;
struct BTNode * pR_child;
};
struct Node
{
struct BTNode * data;
struct Node * pNext;
};
struct LinkQueue
{
struct Node * front;
struct Node * rear;
};
void init_queueLink(struct LinkQueue * pLQ)
{
pLQ->front = pLQ->rear = (struct Node *) malloc(sizeof (struct Node));
if (!pLQ->front)
exit(-1);
pLQ->front->pNext = NULL;
}
void destroy_queueLink(struct LinkQueue * pLQ)
{
while (pLQ->front)
{
pLQ->rear = pLQ->front->pNext;
free(pLQ->front);
pLQ->front = pLQ->rear;
}
}
void en_queueLink(struct LinkQueue * pLQ, struct BTNode * val)
{
struct Node * p;
p = (struct Node *) malloc(sizeof (struct Node));
if (!p)
exit(-1);
p->data = val;
p->pNext = NULL;
pLQ->rear->pNext = p;
pLQ->rear = p;
}
bool is_emptyLink(struct LinkQueue * pLQ)
{
if (pLQ->front->pNext == NULL)
return true;
else
return false;
}
bool out_queueLink(struct LinkQueue * pLQ, struct BTNode ** val)
{
if (is_emptyLink(pLQ))
return false;
struct Node * p = pLQ->front->pNext;
struct Node * q = p->pNext;
*val = p->data;
pLQ->front->pNext = q;
if (pLQ->rear == p)
pLQ->rear = pLQ->front;
free(p);
return true;
}
2创建二叉树及遍历输出
struct BTNode * create_biTree()
{
ElemType data;
struct BTNode * T;
fflush(stdout);
scanf("%c",&data);
getchar();
if(data == '#')
{
return NULL;
}
else
{
T = (struct BTNode *)malloc(sizeof(struct BTNode));
T->data = data;
printf("请输入%c的左子树: ",data);
T->pL_child = create_biTree();
printf("请输入%c的右子树: ",data);
T->pR_child = create_biTree();
return T;
}
}
void destroy_biTree(struct BTNode ** pT)
{
if (*pT)
{
destroy_biTree(&(*pT)->pL_child);
destroy_biTree(&(*pT)->pR_child);
free(*pT);
*pT = NULL;
}
}
void preOrderTraverse(struct BTNode * pT)
{
if(pT==NULL)
{
return;
}
printf("%c ", pT->data);
preOrderTraverse(pT->pL_child);
preOrderTraverse(pT->pR_child);
}
void inOrderTraverse(struct BTNode * pT)
{
if (pT)
{
inOrderTraverse(pT->pL_child);
printf("%c ", pT->data);
inOrderTraverse(pT->pR_child);
}
}
void postOrderTraverse(struct BTNode * pT)
{
if (pT)
{
postOrderTraverse(pT->pL_child);
postOrderTraverse(pT->pR_child);
printf("%c ", pT->data);
}
}
void levelOrderTraverse(struct BTNode * pT)
{
struct LinkQueue q;
struct BTNode * a;
if (pT)
{
init_queueLink(&q);
en_queueLink(&q, pT);
while (!is_emptyLink(&q))
{
out_queueLink(&q, &a);
printf("%c ", a->data);
if (a->pL_child != NULL)
en_queueLink(&q, a->pL_child);
if (a->pR_child != NULL)
en_queueLink(&q, a->pR_child);
}
printf("\n");
destroy_queueLink(&q);
}
}
3基本操作
bool is_empty_biTree(struct BTNode * pT)
{
if (pT)
return false;
else
return true;
}
int biTree_depth(struct BTNode * pT)
{
int i, j;
if (!pT)
return 0;
i = biTree_depth(pT->pL_child);
j = biTree_depth(pT->pR_child);
return i > j ? i + 1 : j + 1;
}
ElemType root_val(struct BTNode * pT)
{
if (is_empty_biTree(pT))
return '#';
else
{
return pT->data;
}
}
struct BTNode * pointer(struct BTNode * pT, ElemType val)
{
struct LinkQueue q;
struct BTNode * a;
if (pT)
{
init_queueLink(&q);
en_queueLink(&q, pT);
while (!is_emptyLink(&q))
{
out_queueLink(&q, &a);
if (a->data == val)
{
destroy_queueLink(&q);
return a;
}
if (a->pL_child)
en_queueLink(&q, a->pL_child);
if (a->pR_child)
en_queueLink(&q, a->pR_child);
}
}
return NULL;
}
ElemType value(struct BTNode * p)
{
return p->data;
}
ElemType left_child(struct BTNode * pT, ElemType val)
{
struct BTNode * a;
if (pT)
{
a = pointer(pT, val);
if (a && a->pL_child)
return a->pL_child->data;
}
return '#';
}
ElemType right_child(struct BTNode * pT, ElemType val)
{
struct BTNode * a;
if (pT)
{
a = pointer(pT, val);
if (a && a->pR_child)
return a->pR_child->data;
}
return '#';
}
void delete_child(struct BTNode * p, int LR)
{
if (p)
{
if (LR == 0)
destroy_biTree(&p->pL_child);
else if(LR == 1)
destroy_biTree(&p->pR_child);
}
}
ElemType parent(struct BTNode * pT, ElemType val)
{
struct LinkQueue q;
struct BTNode * a;
if (pT)
{
init_queueLink(&q);
en_queueLink(&q, pT);
while (!is_emptyLink(&q))
{
out_queueLink(&q, &a);
if (a->pL_child && a->pL_child->data == val || a->pR_child && a->pR_child->data == val)
return a->data;
else
{
if (a->pL_child)
en_queueLink(&q, a->pL_child);
if (a->pR_child)
en_queueLink(&q, a->pR_child);
}
}
}
return '#';
}
ElemType left_sibling(struct BTNode * pT, ElemType val)
{
ElemType a;
struct BTNode * p;
if (pT)
{
a = parent(pT, val);
if (a != '#')
{
p = pointer(pT, a);
if (p->pL_child && p->pR_child && p->pR_child->data == val)
return p->pL_child->data;
}
}
return '#';
}
ElemType right_sibling(struct BTNode * pT, ElemType val)
{
ElemType a;
struct BTNode * p;
if (pT)
{
a = parent(pT, val);
if (a != '#')
{
p = pointer(pT, a);
if (p->pL_child && p->pR_child && p->pL_child->data == val)
return p->pR_child->data;
}
}
return '#';
}
bool insert_child(struct BTNode * p, int LR, struct BTNode * c)
{
if (p)
{
if (LR == 0)
{
c->pR_child = p->pL_child;
p->pL_child = c;
}
else if (LR == 1)
{
c->pR_child = p->pR_child;
p->pR_child = c;
}
return true;
}
return false;
}
4测试
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdlib.h>
int main() {
struct BTNode * T;
struct BTNode * c;
T = create_biTree();
c = create_biTree();
preOrderTraverse(T);
printf("\n");
levelOrderTraverse(T);
struct BTNode * r = pointer(T, 'B');
insert_child(r, 1, c);
preOrderTraverse(T);
int depth;
depth = biTree_depth(T);
printf("\n树的深度为:%d\n", depth);
ElemType val_root;
val_root = root_val(T);
printf("root_val = %c\n", val_root);
struct BTNode * p = pointer(T, 'C');
ElemType val;
val = value(p);
printf("val_p = %c\n", val);
char val_r;
char val_l;
val_l = left_child(T, 'E');
printf("E的左子节点:%c\n", val_l);
val_r = right_child(T, 'E');
printf("E的右子节点:%c\n", val_r);
struct BTNode * q = pointer(T, 'E');
delete_child(q, 1);
preOrderTraverse(T);
ElemType val_par;
val_par = parent(T, 'D');
printf("val_par = %c\n", val_par);
return 0;
}