数据结构:树形结构完整代码,各种遍历方法,直接能跑
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <stdio.h>
#include <stdlib.h>
#define TElemType int
typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
typedef BiTree DataType;
typedef struct queuenode{
DataType data;
struct queuenode *next;
} QueueNode;
//LINKQUEUE
//HEAD POINTER, AND REAR POINTER ARE A V ALIBALE typedef struct {
QueueNode *front;
QueueNode *rear;
} LinkQueue;
int InitQueue(LinkQueue *Q);
int DestroyQueue(LinkQueue *Q);
int QueueEmpty(LinkQueue Q);
int EnQueue(LinkQueue *Q, DataType e);
DataType DeQueue(LinkQueue *Q);
int CreateBiTree(BiTree *T);
int PreOrderTraverse(BiTree T, int (*visit)(TElemType e));
int PreOrderTraverse2(BiTree T, int (*visit)(TElemType e));
int InOrderTraverse(BiTree T, int (*visit)(TElemType e));
int InOrderTraverse2(BiTree T, int (*visit)(TElemType e));
int PostOrderTraverse(BiTree T, int (*visit)(TElemType e));
int PostOrderTraverse2(BiTree T, int (*visit)(TElemType e)); int LevelOrderTraverse(BiTree T, int (*visit)(TElemType e)); int printElem(TElemType e);
int InitBiTree(BiTree *T);
int DestroyBiTree(BiTree *T);
int ClearBiTree(BiTree *T);
int BiTreeEmpty(BiTree T);
int BiTreeDepth(BiTree T);
BiTNode *Parent(BiTree T, BiTNode *e);
BiTNode *LeftChild(BiTree T, BiTNode *e);
BiTNode *RightChild(BiTree T, BiTNode *e);
BiTNode *LeftSibling(BiTree T, BiTNode *e);
BiTNode *RightSibling(BiTree T, BiTNode *e);
int InsertChild(BiTree T, BiTNode *p, int LR, BiTree C);
int DeleteChild(BiTree T, BiTNode *p, int LR);
int CountLeaf(BiTree T);
int CreateBiTreeByPreInOrder(BiTree *T, int preorder[], int *idx,
int inorder[], int low, int high,
int length);
int main()
{
BiTree bitree,bitree2;
InitBiTree(&bitree);
InitBiTree(&bitree2);
printf("input tree 1:\n");
CreateBiTree2(&bitree);
printf("PreOrderTraverse:\n");
PreOrderTraverse(bitree, printElem);
printf("\n");
printf("InOrderTraverse:\n");
InOrderTraverse(bitree, printElem);
printf("\n");
printf("PostOrderTraverse:\n");
PostOrderTraverse(bitree, printElem);
printf("\n");
printf("LevelOrderTraverse:\n");
LevelOrderTraverse(bitree, printElem);
printf("\n");
printf("Depth is %d.\n", BiTreeDepth(bitree));
printf("Count leaf is %d.\n", CountLeaf(bitree));
DestroyBiTree(&bitree);
DestroyBiTree(&bitree2);
}
int CreateBiTree(BiTree *T)
{
int num;
scanf("%d", &num);
if (num == 0)
*T = NULL;
else {
*T = malloc(sizeof(BiTNode));
if (*T == NULL)
return 0;
(*T)->data = num;
if (!CreateBiTree(&(*T)->lchild)) return 0;
if (!CreateBiTree(&(*T)->rchild)) return 0;
}
return 1;
}
int PreOrderTraverse(BiTree T, int (*visit)(TElemType e)) {
if (T) {
//visit root
if (!(*visit)(T->data)) return 0;
//visit left tree
if (!PreOrderTraverse(T->lchild, visit)) return 0;
//visit right tree
if (!PreOrderTraverse(T->rchild, visit)) return 0;
}
return 1;
}
int PreOrderTraverse2(BiTree T, int (*visit)(TElemType e)) {
//visit root
if (!(*visit)(T->data)) return 0;
if (T->lchild)
//visit left tree
if (!PreOrderTraverse(T->lchild, visit)) return 0;
if (T->rchild)
//visit right tree
if (!PreOrderTraverse(T->rchild, visit)) return 0;
return 1;
int InOrderTraverse(BiTree T, int (*visit)(TElemType e)) {
if (T) {
//visit left tree
if (!InOrderTraverse(T->lchild, visit)) return 0;
//visit root
if (!(*visit)(T->data)) return 0;
//visit right tree
if (!InOrderTraverse(T->rchild, visit)) return 0;
}
return 1;
}
int PostOrderTraverse(BiTree T, int (*visit)(TElemType e)) {
if (T) {
//visit left tree
if (!PostOrderTraverse(T->lchild, visit)) return 0;
//visit right tree
if (!PostOrderTraverse(T->rchild, visit)) return 0;
//visit root
if (!(*visit)(T->data)) return 0;
}
return 1;
}
int LevelOrderTraverse(BiTree T, int (*visit)(TElemType e)) {
//define
LinkQueue queue;
//init
InitQueue(&queue);
//root enqueue
EnQueue(&queue, T);
while(!QueueEmpty(queue)) {
//dequeue
BiTree node = DeQueue(&queue);
if (node) {
//visit
if (!(*visit)(node->data)) return 0;
//left child enqueue
EnQueue(&queue, node->lchild);
//right child enqueue
EnQueue(&queue, node->rchild);
}
}
DestroyQueue(&queue);
return 1;
}
int printElem(TElemType e)
{
printf("%d ", e);
return 1;
}
int InitBiTree(BiTree *T)
{
*T = NULL;
return 1;
}
int DestroyBiTree(BiTree *T)
{
if (*T) {
//destroy left tree
DestroyBiTree(&(*T)->lchild);
//destroy right tree
DestroyBiTree(&(*T)->rchild);
//destroy root
free(*T); *T = NULL;
}
return 1;
}
int ClearBiTree(BiTree *T)
{
return DestroyBiTree(T);
}
int BiTreeEmpty(BiTree T)
{
if (T)
return 0;
else
return 1;
}
int BiTreeDepth(BiTree T)
{
if (!T)
return 0;
int ldepth = BiTreeDepth(T->lchild);
int rdepth = BiTreeDepth(T->rchild);
return (ldepth > rdepth ? ldepth : rdepth) + 1;
}
BiTNode *Parent(BiTree T, BiTNode *e)
{
if (e == T) return NULL;
//define
LinkQueue queue;
//init
InitQueue(&queue);
//root enqueue
EnQueue(&queue, T);
BiTNode *node = NULL;
while(!QueueEmpty(queue)) {
//dequeue
node = DeQueue(&queue);
if (node) {
//compare
if (e == node->lchild || e == node->rchild)
break;
//left child enqueue
EnQueue(&queue, node->lchild);
//right child enqueue
EnQueue(&queue, node->rchild);
}
}
DestroyQueue(&queue);
return node;
}
BiTNode *LeftChild(BiTree T, BiTNode *e) {
/* //define
LinkQueue queue;
//init
InitQueue(&queue);
//root enqueue
EnQueue(&queue, T);
BiTNode *node = NULL;
while(!QueueEmpty(queue)) {
//dequeue
node = DeQueue(&queue);
if (node) {
//compare
if (e == node)
break;
//left child enqueue
EnQueue(&queue, node->lchild);
//right child enqueue
EnQueue(&queue, node->rchild);
}
}
DestroyQueue(&queue);
*/
return e->lchild;
}
BiTNode *RightChild(BiTree T, BiTNode *e) {
/* //define
LinkQueue queue;
//init
InitQueue(&queue);
//root enqueue
EnQueue(&queue, T);
BiTNode *node = NULL;
while(!QueueEmpty(queue)) {
//dequeue
node = DeQueue(&queue);
if (node) {
//compare
if (e == node)
break;
//left child enqueue
EnQueue(&queue, node->lchild);
//right child enqueue
EnQueue(&queue, node->rchild);
}
}
DestroyQueue(&queue);
*/
return e->rchild;
}
BiTNode *LeftSibling(BiTree T, BiTNode *e)
{
BiTNode *parent, *lchild;
if ((parent = Parent(T, e)) == NULL) return NULL;
lchild = LeftChild(T, parent);
if (lchild == e) return NULL;
else return lchild;
}
BiTNode *RightSibling(BiTree T, BiTNode *e)
{
BiTNode *parent, *rchild;
if ((parent = Parent(T, e)) == NULL) return NULL;
rchild = RightChild(T, parent);
if (rchild == e) return NULL;
else return rchild;
}
int InsertChild(BiTree T, BiTNode *p, int LR, BiTree C) {
if (LR == 0) {
C->rchild = p->lchild;
p->lchild = C;
} else {
C->rchild = p->rchild;
p->rchild = C;
}
return 1;
}
int DeleteChild(BiTree T, BiTNode *p, int LR)
{
if (LR == 0)
if (!DestroyBiTree(&p->lchild)) return 0;
else
if (!DestroyBiTree(&p->rchild)) return 0;
return 1;
}
int InitQueue(LinkQueue *Q)
{
Q->front = Q->rear = malloc(sizeof(QueueNode));
Q->front->next = NULL;
}
int DestroyQueue(LinkQueue *Q)
{
QueueNode *p = Q->front, *q;
do {
q = p->next;
free(p);
p = q;
} while (p!=NULL);
Q->front = NULL;
Q->rear = NULL;
}
int QueueEmpty(LinkQueue Q)
{
return Q.front == Q.rear;
}
int EnQueue(LinkQueue *Q, DataType e)
{
QueueNode *temp = malloc(sizeof(QueueNode));
if (!temp) {
printf("Memory is out! Cannot malloc.\n");
return 0;
}
temp->data = e;
temp->next = NULL;
Q->rear->next = temp;
Q->rear = temp;
return 1;
}
DataType DeQueue(LinkQueue *Q)
{
DataType ret = Q->front->next->data;
QueueNode *p = Q->front->next;
Q->front->next = Q->front->next->next;
free(p);
if (Q->front->next == NULL) Q->rear = Q->front;
return ret;
}
int CountLeaf(BiTree T)
{
if (T == NULL) return 0;
if (T->lchild == NULL && T->rchild == NULL) return 1;
return CountLeaf(T->lchild) + CountLeaf(T->rchild);
}
int CreateBiTree2(BiTree *T)
{
int num, inorder[100], preorder[100], i = 0;
printf("please input preorder:\n");
while(scanf("%d", &num) != EOF) {
preorder[i++] = num;
}
i = 0;
printf("please input inorder:\n");
while(scanf("%d", &num) != EOF) {
inorder[i++] = num;
}
int index = -1;
CreateBiTreeByPreInOrder(T, preorder, &index, inorder, 0, i-1, i);
return 1;
}
int CreateBiTreeByPreInOrder(BiTree *T, int preorder[], int *idx,
int inorder[], int low, int high, int length)
{
(*idx)++;
if (*idx >= length) {*T = NULL; return 1;}
int i;
for(i=low;i<=high;i++)
if (inorder[i] == preorder[*idx]) break;
if (i > high) {(*idx)--; *T = NULL; return 1;}
*T = malloc(sizeof(BiTNode));
if (*T == NULL)
return 0;
(*T)->data = preorder[*idx];
if (!CreateBiTreeByPreInOrder(&(*T)->lchild, preorder, idx, inorder, low, i-1, length)) return 0;
if (!CreateBiTreeByPreInOrder(&(*T)->rchild, preorder, idx, inorder, i+1, high, length)) return 0;
return 1;
}。