数据结构基本结构:二叉树
来源:互联网 发布:夏帆 知乎 编辑:程序博客网 时间: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();
}
- 数据结构基本结构:二叉树
- 二叉树基本结构
- 基本数据结构之二叉树
- 基本数据结构之二叉树
- 基本数据结构只二叉树
- 基本数据结构之二叉树
- 算法系列(七)数据结构之树的基本结构和二叉树的遍历
- 【数据结构】树形结构:二叉树&树
- 【数据结构】二叉树顺序结构实现_BiTreeArray
- 【数据结构】二叉树链式结构实现_BiTreeLink
- 数据结构 - 二叉树的存储结构
- 数据结构_树形结构_二叉树
- 数据结构(8):二叉树的存储结构
- 二叉树的存储结构(数据结构)
- 数据结构--二叉树(链表)基本操作
- 基本数据结构——二叉查找树
- 基本数据结构之二叉查找树
- 数据结构二叉树的编程基本问题
- 插件系统框架分析
- 好的服务器系统
- 在Ubuntu下安装SAP GUI碰到的一个笨问题
- 我内,我外,我是谁?
- 在Xubuntu下跑SAP GUI
- 数据结构基本结构:二叉树
- 安全意味着麻烦
- 在VCL应用中运用MVC模式
- 一个批量更改文件名的Python脚本
- [技术评论]龙芯电脑
- 安全意味着麻烦
- 选择什么数据库
- 从一个笑话看软件开发管理
- 2007年最主流操作系统、数据库、协作办公、IDE