目录:
- 代码:
- 分析:
- 汇编:
代码:
BTree.h
BTree.c
二叉树(多路平衡搜索树)
LinkQueue.h
#ifndef _LINKQUEUE_H_
#define _LINKQUEUE_H_typedef void LinkQueue;//定义队列类型LinkQueue* LinkQueue_Create();//声明创建队列函数void LinkQueue_Destroy(LinkQueue* queue);//声明销毁队列函数void LinkQueue_Clear(LinkQueue* queue);//声明清空队列函数int LinkQueue_Append(LinkQueue* queue, void* item);//声明添加进队函数void* LinkQueue_Retrieve(LinkQueue* queue);//声明出队函数void* LinkQueue_Header(LinkQueue* queue);//声明获取头元素函数int LinkQueue_Length(LinkQueue* queue);//声明获取长度函数#endif
LinkQueue.c
#include <malloc.h>
#include <stdio.h>
#include "LinkQueue.h"typedef struct _tag_LinkQueueNode TLinkQueueNode;//定义队列节点类型
struct _tag_LinkQueueNode
{TLinkQueueNode* next;//下节点指针void* item;//节点存储数据指针
};typedef struct _tag_LinkQueue//定义实际使用队列类型
{TLinkQueueNode* front;//前节点(头节点)一直指向第一个节点用于出队列TLinkQueueNode* rear;//后节点int length;//长度
} TLinkQueue;LinkQueue* LinkQueue_Create() // 定义创建队列函数
{TLinkQueue* ret = (TLinkQueue*)malloc(sizeof(TLinkQueue));if( ret != NULL )//创建成功{ret->front = NULL;//前节点指向空ret->rear = NULL;//后节点指向空ret->length = 0;//长度设空}return ret;//返回创建队列
}void LinkQueue_Destroy(LinkQueue* queue) //定义销毁队列函数
{LinkQueue_Clear(queue);//先清空队列free(queue);//释放队列空间
}void LinkQueue_Clear(LinkQueue* queue) // 定义清空队列函数
{while( LinkQueue_Length(queue) > 0 )//将所有节点出队列操作{LinkQueue_Retrieve(queue);}
}int LinkQueue_Append(LinkQueue* queue, void* item) //定义添加进队列函数
{TLinkQueue* sQueue = (TLinkQueue*)queue;//取得队列TLinkQueueNode* node = (TLinkQueueNode*)malloc(sizeof(TLinkQueueNode));//创建节点int ret = (sQueue != NULL ) && (item != NULL) && (node != NULL);//如果队列与数据不为空和创建节点成功if( ret ){node->item = item;//给新建节点赋值存储数据if( sQueue->length > 0 )//如果队列当前长度大于0{sQueue->rear->next = node;//树的后节点的下一个节点指向新建节点sQueue->rear = node;//将新建节点设为树的后节点,实现每次添加都往连接node->next = NULL;//新建节点的下一个节点设空}else//否则是第一个节点{sQueue->front = node;//树的前节点指向新建节点sQueue->rear = node;//树的后节点指向新建节点node->next = NULL;//新节点的下一个节点为空}sQueue->length++;//长度增加}if( !ret )//如果条件不成功{free(node);//释放新建节点空间}return ret;
}void* LinkQueue_Retrieve(LinkQueue* queue) //定义出队列函数
{TLinkQueue* sQueue = (TLinkQueue*)queue;//取得队列TLinkQueueNode* node = NULL;void* ret = NULL;if( (sQueue != NULL) && (sQueue->length > 0) )//如果队列不为空与长度大于0{node = sQueue->front;//取得出队列节点 sQueue->front = node->next;//将前节点指向出队列节点的下一个节点ret = node->item;//取得节点存储数据free(node);//释放该节点sQueue->length--;//长度减少if( sQueue->length == 0 )//如果是最后一个节点,将队列重置都为空{sQueue->front = NULL;sQueue->rear = NULL;}}return ret;//返回节点数据
}void* LinkQueue_Header(LinkQueue* queue) // 定义获取头节点函数
{TLinkQueue* sQueue = (TLinkQueue*)queue;//取得队列void* ret = NULL;if( (sQueue != NULL) && (sQueue->length > 0) )//如果队列不为空与长度大于0{ret = sQueue->front->item;//取得第一个节点存储数据}return ret;//返回数据
}int LinkQueue_Length(LinkQueue* queue) // 定义获取长度函数
{TLinkQueue* sQueue = (TLinkQueue*)queue;//取得树int ret = -1;if( sQueue != NULL ){ret = sQueue->length;//取得长度}return ret;//返回长度
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "BTree.h"
#include "LinkQueue.h"struct Node//定义节点
{BTreeNode header;char v;
};void printf_data(BTreeNode* node)//将节点内容输出函数
{if( node != NULL ){printf("%c", ((struct Node*)node)->v);}
}void pre_order_traversal(BTreeNode* root)//
{if( root != NULL ){printf("%c, ", ((struct Node*)root)->v);pre_order_traversal(root->left);pre_order_traversal(root->right);}
}void middle_order_traversal(BTreeNode* root)
{if( root != NULL ){middle_order_traversal(root->left);printf("%c, ", ((struct Node*)root)->v);middle_order_traversal(root->right);}
}void post_order_traversal(BTreeNode* root)
{if( root != NULL ){post_order_traversal(root->left);post_order_traversal(root->right);printf("%c, ", ((struct Node*)root)->v);}
}void level_order_traversal(BTreeNode* root)
{printf("==%c\n", ((struct Node*)root)->v);if( root != NULL ){LinkQueue* queue = LinkQueue_Create();//创建队列if( queue != NULL )//创建成功{LinkQueue_Append(queue, root);//将节点进队列while( LinkQueue_Length(queue) > 0 ){struct Node* node = (struct Node*)LinkQueue_Retrieve(queue);//出队列printf("%c, ", node->v);//输出节点存储的数据LinkQueue_Append(queue, node->header.left);//将左子节点进队列LinkQueue_Append(queue, node->header.right);//将右子节点进队列}}LinkQueue_Destroy(queue);}
}int main(int argc, char *argv[])
{BTree* tree = BTree_Create();struct Node n1 = {{NULL, NULL}, 'A'};struct Node n2 = {{NULL, NULL}, 'B'};struct Node n3 = {{NULL, NULL}, 'C'};struct Node n4 = {{NULL, NULL}, 'D'};struct Node n5 = {{NULL, NULL}, 'E'};struct Node n6 = {{NULL, NULL}, 'F'};BTree_Insert(tree, (BTreeNode*)&n1, 0, 0, 0);BTree_Insert(tree, (BTreeNode*)&n2, 0x00, 1, 0);BTree_Insert(tree, (BTreeNode*)&n3, 0x01, 1, 0);BTree_Insert(tree, (BTreeNode*)&n4, 0x00, 2, 0);BTree_Insert(tree, (BTreeNode*)&n5, 0x02, 2, 0);BTree_Insert(tree, (BTreeNode*)&n6, 0x02, 3, 0);printf("Full Tree: \n");BTree_Display(tree, printf_data, 4, '-');printf("Pre Order Traversal:\n");pre_order_traversal(BTree_Root(tree));//输出:ABDEFCprintf("\n");printf("Middle Order Traversal:\n");middle_order_traversal(BTree_Root(tree));//输出:DBFEACprintf("\n");printf("Post Order Traversal:\n");post_order_traversal(BTree_Root(tree));//输出:DFEBCAprintf("\n");printf("Level Order Traversal:\n");level_order_traversal(BTree_Root(tree));//输出:ABCDEFprintf("\n");BTree_Destroy(tree);getchar();return 0;
}
分析:
LinkQueue :
main中的调用函数过程:
汇编: