Create a B-tree, dynamically add nodes, and using three traversal algorithm to traverse the tree

ks17: $ CAT btree_test.c the Apple algorithm 
/// ************************************** ************************************************************ 
/// @filename: btree_test.c 
/// @Brief: try to build b-tree, and use three traversal tree traversal algorithm 
/// 
/// 
/// @notice: application space in which to function pointers, is not returned, must be packed layer, assigned to the pointer mode pointer, otherwise the function returns the result of the assignment is NULL 
@author ///: kuang17 
/// @Last Modified: 2019-06-17 18:51 
/// ************************************************************ ************************************** 

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

/// definition structure 
typedef struct the treeNode 
{ 
    int Data; 
    struct the treeNode * parent; 
    struct the treeNode * LCH; 
    struct the treeNode * Rch; 
} the 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");
TreeNode * mallocNode ()
// ********** ************************************************** *********
/// @Returns
///
/// @Brief mallocNode application space
// *********************************************** **********************
}

{
    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;
    0 return; 
} 

// **************************** ************************************************************ 
/// @Brief the addNode add nodes 
/// 
/// @Param parentNode 
/// @Param newData 
// / 
/// @Returns 
// ****************************************** *************************** 
int the addNode (the parentNode the TreeNode *, int newData) 
{ 
    the TreeNode * the newNode; 
    the newNode mallocNode = (); 
    newnode- > Data = newData; 

    IF (newData <= parentNode-> Data) 
    { 
        the printf ( "parent Data IS: D%, the Add LCH \ n-", parentNode-> Data); 
        if(parentNode->lch == NULL)
        { 
            // here parentNode not directly assigned only give him the assignment of nodes
            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;
}

 

Guess you like

Origin www.cnblogs.com/kuang17/p/11041557.html