이진 트리 순회 (코드, 분석, 어셈블리)

암호:

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));//输出:ABDEFC
    
    printf("\n");
    
    printf("Middle Order Traversal:\n");
    
    middle_order_traversal(BTree_Root(tree));//输出:DBFEAC
    
    printf("\n");
    
    printf("Post Order Traversal:\n");
    
    post_order_traversal(BTree_Root(tree));//输出:DFEBCA
    
    printf("\n");
    
    printf("Level Order Traversal:\n");
    
    level_order_traversal(BTree_Root(tree));//输出:ABCDEF
    
    printf("\n");
    
    BTree_Destroy(tree);
    
	getchar();
	return 0;
}


분석:

LinkQueue :
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입

여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입

main의 호출 함수 프로세스 :
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입

편집:

여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입
여기에 사진 설명 삽입

추천

출처blog.csdn.net/m0_37599645/article/details/112032148