#include <stdio.h>
#include <stdlib.h>
struct treeNode {
char data;
struct treeNode* LChild;
struct treeNode* RChild;
};
struct treeNode* createNode(char data)
{
struct treeNode* newNode = (struct treeNode *)malloc(sizeof(struct treeNode));
newNode->data = data;
newNode->LChild = NULL;
newNode->RChild = NULL;
return newNode;
}
void linkNode(struct treeNode* currentNode, struct treeNode* LChildNode, struct treeNode* RChildNode)
{
currentNode->LChild = LChildNode;
currentNode->RChild = RChildNode;
}
void printNode(struct treeNode* currentNode)
{
printf("%c ", currentNode->data);
}
void PreOrder(struct treeNode* root)
{
if(root != NULL)
{
printNode(root);
PreOrder(root->LChild);
PreOrder(root->RChild);
}
}
void InOrder(struct treeNode* root)
{
if(root != NULL)
{
InOrder(root->LChild);
printNode(root);
InOrder(root->RChild);
}
}
void PostOrder(struct treeNode* root)
{
if(root != NULL)
{
PostOrder(root->LChild);
PostOrder(root->RChild);
printNode(root);
}
}
int main()
{
struct treeNode* A = createNode('A');
struct treeNode* B = createNode('B');
struct treeNode* C = createNode('C');
struct treeNode* D = createNode('D');
struct treeNode* E = createNode('E');
struct treeNode* F = createNode('F');
struct treeNode* G = createNode('G');
struct treeNode* H = createNode('H');
linkNode(A, B, C);
linkNode(B, D, E);
linkNode(C, F, G);
linkNode(F, NULL, H);
PreOrder(A);
printf("\n");
InOrder(A);
printf("\n");
PostOrder(A);
printf("\n");
return 0;
}
二叉树前中后序递归遍历C语言实现
最新推荐文章于 2024-01-31 08:47:40 发布