数据结构基本结构:二叉树

来源:互联网 发布:夏帆 知乎 编辑:程序博客网 时间:2024/05/17 00:16

#include<stdio.h>
#include<malloc.h>
typedef struct BTree
{
    char data;
    struct BTree *leftChild;
    struct BTree *rightChild;
} *BTree;

typedef struct Queue
{
    BTree treeNode;
    struct Queue *next;
} *BTreeQueue;

//This queue used to keep the tree node that used in  HieraichicleSort().
BTreeQueue queue = NULL;

BTree head = NULL;

//Puts the node to the queue,
//this method used for HieraichicleSort();
void EnQueue(BTree node)
{
    BTreeQueue p;
    BTreeQueue temNode;
   
    temNode = (BTreeQueue)malloc(sizeof(struct Queue));
    temNode->treeNode = node;
    temNode->next = NULL;
   
    if(queue == NULL)
    {
        queue = temNode;
    }   
    else
    {
        p=queue;
        while(p->next!=NULL)
        {
            p=p->next;
        }       
        p->next = temNode;               
    }
}

//Gets the node out of the queue.
//this method used for HieraichicleSort();
BTree OutQueue()
{
    BTreeQueue firstNode = queue;
    if(queue!=NULL)
    {
        queue = queue->next;   
        return firstNode->treeNode;   
    }
    else
    {
        return NULL;
    }
}

//FirstHead Sort.
void FirstHeadSort(BTree node)
{
    if(node!=NULL)
    {
        printf("%c/t",node->data);
        FirstHeadSort(node->leftChild);
        FirstHeadSort(node->rightChild);
    }
}

//MiddleHead Sort.
void MiddleHeadSort(BTree node)
{
    if(node!=NULL)
    {   
        FirstHeadSort(node->leftChild);
        printf("%c/t",node->data);
        FirstHeadSort(node->rightChild);       
    }
}

//LastHead Sort.
void LastHeadSort(BTree node)
{
    if(node!=NULL)
    {   
        FirstHeadSort(node->leftChild);
        FirstHeadSort(node->rightChild);       
        printf("%c/t",node->data);
    }
}

//Hieraichicle Sort.
void HieraichicleSort()
{
    BTree node;
    EnQueue(head);
   
    while((node = OutQueue())!=NULL)
    {
        printf("%c/t",node->data);
        if(node->leftChild!=NULL)
        {
            EnQueue(node->leftChild);
        }
        if(node->rightChild!=NULL)
        {
            EnQueue(node->rightChild);
        }       
    }
}

//Mocks 5 nodes to test the functions.
void InitBTreeTestData()
{
    //Just mocks some nodes to test the functions.
    BTree node1 = (BTree)malloc(sizeof(struct BTree));
    node1->data = 'A';
    node1->leftChild = NULL;
    node1->rightChild = NULL;
   
    BTree node2 = (BTree)malloc(sizeof(struct BTree));
    node2->data = 'B';
    node2->leftChild = NULL;
    node2->rightChild = NULL;
   
    BTree node3 = (BTree)malloc(sizeof(struct BTree));
    node3->data = 'C';
    node3->leftChild = NULL;
    node3->rightChild = NULL;
   
    BTree node4 = (BTree)malloc(sizeof(struct BTree));
    node4->data = 'D';
    node4->leftChild = NULL;
    node4->rightChild = NULL;
   
    BTree node5 = (BTree)malloc(sizeof(struct BTree));
    node5->data = 'E';
    node5->leftChild = NULL;
    node5->rightChild = NULL;
   
    head = node1;
    head->leftChild = node2;
    head->rightChild = node3;
   
    node2->leftChild = node4;
    node3->rightChild = node5;
}

//Tests the functions.
void TestBTree()
{
    InitBTreeTestData();
    FirstHeadSort(head);
    printf("/n");
    MiddleHeadSort(head);
    printf("/n");
    LastHeadSort(head);
    printf("/n");
    HieraichicleSort();
}