?分别内容:
二叉树的直接赋值,二叉树的前、中、后序遍历,二叉树的前、中、后递归创建。
#include <stdio.h>
#include <stdlib.h>
typedef struct tree {
?? ?char date;
?? ?struct tree* left, * right;
}tree;
void makeTreeNote(tree** p, char ch);
void directMakeTree(tree** p);
void showTree1(tree* p);
void showTree2(tree* p);
void showTree3(tree* p);
void recursionMakeTree1(tree** temp);
void recursionMakeTree2(tree** temp);
void recursionMakeTree3(tree** temp);
int main()
{
?? ?
?? ?tree* tree1 = NULL;
?? ?directMakeTree(&tree1);
?? ?printf("先序输出:");
?? ?showTree1(tree1);
?? ?printf("\n中序输出:");
?? ?showTree2(tree1);
?? ?printf("\n后序输出:");
?? ?showTree3(tree1);
?? ?
?? ?/*
?? ?printf("\n前序创建:");
?? ?tree* tree2 = NULL;
?? ?recursionMakeTree1(&tree2);
?? ?printf("先序输出:");
?? ?showTree1(tree2);
?? ?printf("\n中序输出:");
?? ?showTree2(tree2);
?? ?printf("\n后序输出:");
?? ?showTree3(tree2);
?? ?*/
?? ?/*
?? ?printf("\n中序创建:");
?? ?tree* tree3 = NULL;
?? ?recursionMakeTree2(&tree3);
?? ?printf("先序输出:");
?? ?showTree1(tree3);
?? ?printf("\n中序输出:");
?? ?showTree2(tree3);
?? ?printf("\n后序输出:");
?? ?showTree3(tree3);
?? ?*/
?? ?/*
?? ?printf("\n后序创建:");
?? ?tree* tree4 = NULL;
?? ?recursionMakeTree3(&tree4);
?? ?printf("先序输出:");
?? ?showTree1(tree4);
?? ?printf("\n中序输出:");
?? ?showTree2(tree4);
?? ?printf("\n后序输出:");
?? ?showTree3(tree4);
?? ?*/
?? ?return 0;
}
void makeTreeNote(tree** p, char ch)
{
?? ?tree* temp = (tree*)malloc(sizeof(tree));
?? ?temp->date = ch;
?? ?temp->left = NULL;
?? ?temp->right = NULL;
?? ?*p = temp;
}
void directMakeTree(tree** p)
{
?? ?tree* temp = NULL;
?? ?makeTreeNote(&temp, 'A');
?? ?makeTreeNote(&temp->left, 'B');
?? ?makeTreeNote(&temp->right, 'C');
?? ?makeTreeNote(&temp->left->left, 'D');
?? ?makeTreeNote(&temp->left->right, 'E');
?? ?makeTreeNote(&temp->right->left, 'F');
?? ?makeTreeNote(&temp->right->right, 'G');
?? ?*p = temp;
}
void showTree1(tree* p)
{
?? ?if (p != NULL)
?? ?{
?? ??? ?printf("%c ", p->date);
?? ??? ?showTree1(p->left);
?? ??? ?showTree1(p->right);
?? ?}
}
void showTree2(tree* p)
{
?? ?if (p != NULL)
?? ?{
?? ??? ?showTree2(p->left);
?? ??? ?printf("%c ", p->date);
?? ??? ?showTree2(p->right);
?? ?}
}
void showTree3(tree* p)
{
?? ?if (p != NULL)
?? ?{
?? ??? ?showTree3(p->left);
?? ??? ?showTree3(p->right);
?? ??? ?printf("%c ", p->date);
?? ?}
}
void recursionMakeTree1(tree** temp)
{
?? ?char date;
?? ?scanf("%c", &date);
?? ?if (date == '#'|| date == 10) //#与换行下一个节点为NULL
?? ??? ?(*temp) = NULL;
?? ?else
?? ?{
?? ??? ?*temp = (tree*)malloc(sizeof(tree));
?? ??? ?(*temp)->date = date;
?? ??? ?recursionMakeTree1(&(*temp)->left);
?? ??? ?recursionMakeTree1(&(*temp)->right);
?? ?}
}
void recursionMakeTree2(tree** temp)
{
?? ?char date;
?? ?scanf("%c", &date);
?? ?if (date == '#' || date == 10)
?? ??? ?*temp = NULL;
?? ?else
?? ?{
?? ??? ?(*temp) = (tree*)malloc(sizeof(tree));
?? ??? ?recursionMakeTree2(&(*temp)->left);
?? ??? ?(*temp)->date = date;
?? ??? ?recursionMakeTree2(&(*temp)->right);
?? ?}
}
void recursionMakeTree3(tree** temp)
{
?? ?char date;
?? ?scanf("%c", &date);
?? ?if (date == '#' || date == 10)
?? ??? ?(*temp) = NULL;
?? ?else
?? ?{
?? ??? ?(*temp) = (tree*)malloc(sizeof(tree));
?? ??? ?recursionMakeTree3(&(*temp)->left);
?? ??? ?recursionMakeTree3(&(*temp)->right);
?? ??? ?(*temp)->date = date;
?? ?}
}
|