建立B樹,動態添加節點,並使用三種遍歷算法對樹進行遍歷

ks17:algorithm apple$ cat btree_test.c
///***************************************************************
///  @Filename: btree_test.c
///  @Brief: 嘗試構建b樹,並使用三種遍歷算法對樹進行遍歷
///
///
///  @notice: 在函數裏面申請空間給指針,是不能返回的,必須包裝一層,賦值給指針的指針方式,不然函數返回後賦值結果是NULL
///  @Author: kuang17
///  @Last modified: 2019-06-17 18:51
///***************************************************************

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

///定義結構
typedef struct treeNode
{
    int data;
    struct treeNode* parent;
    struct treeNode* lch;
    struct treeNode* rch;
}TreeNode;

typedef struct BtreeData
{
    TreeNode* rootNode;
}Btree;

///定義參數
struct BtreeData* m_btree;

///定義函數
TreeNode* mallocNode();
int initTree(Btree* btree, int rootData);
int perorderTraversalTree(TreeNode* pnode);
int inorderTraversalTree(TreeNode* pnode);
int postorderTraversalTree(TreeNode* pnode);
int addNode(TreeNode* parentNode, int newData);

///主函數
int main(int argc, char** argv)
{
    int originData[100] = {0, 0, 0};
    int count = 0;

    //init tree
    m_btree = (struct BtreeData*)malloc(sizeof(struct BtreeData));
    initTree(m_btree, atoi(argv[1]));
    if(m_btree->rootNode != NULL)
        printf("root data is: %d\n", m_btree->rootNode->data);

    //create tree
    char input[12] = "{\0}";
    int is_end = 1;
    while (is_end)
    {
        gets(input);
        printf("input is %s\n", input);
        if (strcmp(input, "end") == 0){
            printf("end input.\n");
            is_end = 0;
        } else{
            //add to tree
            addNode(m_btree->rootNode, atoi(input));
            originData[count++] = atoi(input);
        }
    }
    //perorder traversal
    printf("perorder traversal :\n");
    perorderTraversalTree(m_btree->rootNode);


    //inorder traversal
    printf("inorder traversal :\n");
    inorderTraversalTree(m_btree->rootNode);

    //postorder traversal
    printf("postorder traversal: \n");
    postorderTraversalTree(m_btree->rootNode);

    printf("\norigin is :\n");
    for(int i = 0; i < count; i++)
    {
        printf("%d\n", originData[i]);
    }
    printf("end\n");
}

// *********************************************************************
/// @Brief         mallocNode 申請空間
///
/// @Returns
// *********************************************************************
TreeNode* mallocNode()
{
    TreeNode* newNode;
    newNode = (TreeNode*)malloc(sizeof(TreeNode));
    newNode->data = 0;
    newNode->parent = NULL;
    newNode->lch = NULL;
    newNode->rch = NULL;
    return newNode;
}

// *********************************************************************
/// @Brief         initTree 初始化樹
///
/// @Param         btree
/// @Param         rootData
///
/// @Returns
// *********************************************************************
int initTree(Btree* btree, int rootData)
{
    TreeNode* rootNode;
    rootNode = mallocNode();
    rootNode->data = rootData;
    btree->rootNode = rootNode;
    return 0;
}

// *********************************************************************
/// @Brief         addNode 添加節點
///
/// @Param         parentNode
/// @Param         newData
///
/// @Returns
// *********************************************************************
int addNode(TreeNode* parentNode, int newData)
{
    TreeNode* newNode;
    newNode = mallocNode();
    newNode->data = newData;

    if(newData <= parentNode->data)
    {
        printf("parent data is: %d, add lch\n", parentNode->data);
        if(parentNode->lch == NULL)
        {
            //這裏無法直接給parentNode賦值,只能給他的節點賦值
            parentNode->lch = newNode;
        }else
        {
            addNode(parentNode->lch, newData);
        }
    }

    if(newData > parentNode->data)
    {
        printf("parent data is: %d, add rch\n", parentNode->data);
        if(parentNode->rch == NULL){
            parentNode->rch = newNode;
        } else {
            addNode(parentNode->rch, newData);
        }
    }

    return 0;
}

// *********************************************************************
/// @Brief         perorderTraversalTree 先序遍歷
///
/// @Param         pnode
///
/// @Returns
// *********************************************************************
int perorderTraversalTree(TreeNode* pnode)
{
    if(pnode == NULL)
        return 0;

    perorderTraversalTree(pnode->lch);
    printf("%d\n", pnode->data);
    perorderTraversalTree(pnode->rch);
    return 1;
}


// *********************************************************************
/// @Brief         inorderTraversalTree 中序遍歷
///
/// @Param         pnode
///
/// @Returns
// *********************************************************************
int inorderTraversalTree(TreeNode* pnode)
{
    if(pnode == NULL)
        return 0;

    printf("%d\n", pnode->data);
    inorderTraversalTree(pnode->lch);
    inorderTraversalTree(pnode->rch);
    return 1;
}

// *********************************************************************
/// @Brief         postorderTraversalTree 後序遍歷
///
/// @Param         pnode
///
/// @Returns
// *********************************************************************
int postorderTraversalTree(TreeNode* pnode)
{
    if(pnode == NULL)
        return 0;

    postorderTraversalTree(pnode->lch);
    postorderTraversalTree(pnode->rch);
    printf("%d\n", pnode->data);
    return 1;
}
相關文章
相關標籤/搜索