C語言 二叉樹 BinaryTree

#include <stdio.h>
#include <stdlib.h>
#define bool    int
#define false    0
#define true        1
struct Node
{
    struct Node* leftNode;
    struct Node* rightNode;
    int nNum;
};
//struct BinaryTree play as root node
struct BinaryTree
{
    struct Node* leftNode;
    struct Node* rightNode;
    int nNum;
};

struct BinaryTree* createBinaryTree(int nNum)
{
    struct BinaryTree* ptrTree = (struct BinaryTree*)malloc(sizeof(struct BinaryTree));
    if (ptrTree == NULL)
        return NULL;
    ptrTree->leftNode = NULL;
    ptrTree->rightNode = NULL;
    ptrTree->nNum = nNum;
    return ptrTree;
}

bool addEleToTree(struct BinaryTree* ptrTree, int nNum)
{
    bool bRet = false;
    if (ptrTree == NULL)
        return bRet;
    int nDirection = 0;
    struct Node* curNode = NULL;
    struct Node* curNodeBackup = NULL;

    if (nNum < ptrTree->nNum)
    {
        curNode = ptrTree->leftNode;
        nDirection = 1;//mark move record
        curNodeBackup = ptrTree->leftNode;
    }
    else if (nNum > ptrTree->nNum)
    {
        curNode = ptrTree->rightNode;
        nDirection = 2;//mark move record
        curNodeBackup = ptrTree->rightNode;
    }
    else
    {
        return bRet;
    }

    while (true)
    {
        if (curNode == NULL)
        {
            struct Node* ptrNode = (struct Node*)malloc(sizeof(struct Node));
            if (ptrNode == NULL)
                break;
            ptrNode->leftNode = NULL;
            ptrNode->rightNode = NULL;
            ptrNode->nNum = nNum;
            if (curNodeBackup == NULL)
            {
                if (nDirection == 1)
                {
                    ptrTree->leftNode = ptrNode;//just root node exist, let new node to 1st son node
                }
                else
                {
                    ptrTree->rightNode = ptrNode;
                }
                break;
            }
            if (nDirection == 1)
            {
                curNodeBackup->leftNode = ptrNode;
            }
            else
            {
                curNodeBackup->rightNode = ptrNode;
            }
            bRet = true;
            break;//finish job
        }
        curNodeBackup = curNode;//save curent node's postion
        if (curNode->nNum > nNum)
        {//move go left
            curNode = curNode->leftNode;
            nDirection = 1;
        }
        else if (curNode->nNum < nNum)
        {//move go right
            curNode = curNode->rightNode;
            nDirection = 2;
        }
        else//when equal, fail for adding node, jump out of while loop
        {
            break;
        }
    }//end while
    return bRet;
}

void ascendingTraversalTree(struct BinaryTree* ptrTree)
{
    if (ptrTree->leftNode != NULL)
    {//exist leftNode step in
        ascendingTraversalTree((struct BinaryTree*)ptrTree->leftNode);
    }
    printf("%d\n", ptrTree->nNum);
    if (ptrTree->rightNode != NULL)
    {
        ascendingTraversalTree((struct BinaryTree*)ptrTree->rightNode);
    }
}

void descendingTraversalTree(struct BinaryTree* ptrTree)
{
    if (ptrTree->rightNode != NULL)
    {//exist leftNode step in
        descendingTraversalTree((struct BinaryTree*)ptrTree->rightNode);
    }
    printf("%d\n", ptrTree->nNum);
    if (ptrTree->leftNode != NULL)
    {
        descendingTraversalTree((struct BinaryTree*)ptrTree->leftNode);
    }
}
//use Mid-order traversal
struct Node* findEleInTree(struct BinaryTree* ptrTree, int nKey)
{
    if (ptrTree == NULL)
        return NULL;
    if (ptrTree->nNum == nKey)
    {
        return (struct Node*)ptrTree;
    }
    if (ptrTree->leftNode != NULL)
    {
        return findEleInTree(ptrTree->leftNode, nKey);        
    }
    if (ptrTree->rightNode != NULL)
    {
        return findEleInTree(ptrTree->rightNode, nKey);
    }
    return NULL;
}

void main()
{
    struct BinaryTree* ptrTree = createBinaryTree(10);
    addEleToTree(ptrTree, 9);
    addEleToTree(ptrTree, 15);
    addEleToTree(ptrTree, 7);
    addEleToTree(ptrTree, 8);
    addEleToTree(ptrTree, 19);
    addEleToTree(ptrTree, 12);
    ascendingTraversalTree(ptrTree);
    printf("descending sort\n");
    descendingTraversalTree(ptrTree);
    printf("find 8 in tree, shoule be true\n");
    struct Node* ptrFind1 = findEleInTree(ptrTree, 8);
    printf("find 8 node's address=%p\n", ptrFind1);
    printf("find 20 in tree, shoule be false\n");
    struct Node* ptrFind2 = findEleInTree(ptrTree, 20);
    printf("find 20 node's address=%p\n", ptrFind2);
    return;
}
相關文章
相關標籤/搜索