25.二叉树的应用实例
算法设计:判断两棵二叉树A与B是否相似,即要么它们都为空或都只有一个根结点,要么它们的左右子树均相似。
算法思路:
1、保留之前所写二叉树头文件内容。
2、再设计判断相似算法Like(BiTreeNode *A, BiTreeNode *B),该算法功能是:若A与B相似,则函数返回1,否则返回0,具体如下:
(1)若A== B==NULL,则A与B相似,即Like(A,B)=1;
(2)若A与B有一个为NULL,另一个不为NULL,则A与B不相似,即Like(A,B)=0;
(3)采用递归方法,进一步判断A的左子树和B的左子树、A的右子树和B的右子树是否相似。
3、在主函数中实现,验证两棵树是否相似。(需要先建立两棵树,然后再判定)
头文件1:BiTree.h
#include<stdio.h>
#include<stdlib.h>
typedef char DataType;
typedef struct Node
{
DataType data;
struct Node* leftChild;
struct Node* rightChild;
}BiTreeNode;
void Initiate(BiTreeNode** root)
{
*root = (BiTreeNode*)malloc(sizeof(BiTreeNode));
(*root)->leftChild = NULL;
(*root)->rightChild = NULL;
}
BiTreeNode* InsertLeftNode(BiTreeNode* curr, DataType x)
{
BiTreeNode* s, * t;
if (curr == NULL)
return NULL;
t = curr->leftChild;
s = (BiTreeNode*)malloc(sizeof(BiTreeNode));
s->data = x;
s->leftChild = t;
s->rightChild = NULL;
curr->leftChild = s;
return curr->leftChild;
}
BiTreeNode* InsertRightNode(BiTreeNode* curr, DataType x)
{
BiTreeNode* s, * t;
if (curr == NULL)
return NULL;
t = curr->rightChild;
s = (BiTreeNode*)malloc(sizeof(BiTreeNode));
s->data = x;
s->rightChild = t;
s->leftChild = NULL;
curr->rightChild = s;
return curr->rightChild;
}
void Destroy(BiTreeNode** root)
{
if ((*root) != NULL && (*root)->leftChild != NULL)
Destroy(&(*root)->leftChild);
if ((*root) != NULL && (*root)->rightChild != NULL)
Destroy(&(*root)->rightChild);
free(*root);
}
头文件2:BiTreeTraverse.h
#include"BiTree.h"
void Visit(DataType item)
{
printf("%c ", item);
}
void PrintBiTree(BiTreeNode* root, int n)
{
int i;
if (root == NULL)
return;
PrintBiTree(root->rightChild, n + 1);
for (i = 0; i < n - 1; i++)
printf(" ");
if (n > 0)
{
printf("---");
printf("%c\n", root->data);
}
PrintBiTree(root->leftChild, n + 1);
}
BiTreeNode* Search(BiTreeNode* root, DataType x)
{
BiTreeNode* find = NULL;
if (root != NULL)
{
if (root->data == x)
find = root;
else
{
find = Search(root->leftChild, x);
if (find == NULL)
find = Search(root->rightChild, x);
}
}
return find;
}
void PreOrder(BiTreeNode* t, void Visit(DataType item))
{
if (t != NULL)
{
Visit(t->data);
PreOrder(t->leftChild, Visit);
PreOrder(t->rightChild, Visit);
}
}
void InOrder(BiTreeNode* t, void Visit(DataType item))
{
if (t != NULL)
{
InOrder(t->leftChild, Visit);
Visit(t->data);
InOrder(t->rightChild, Visit);
}
}
void PostOrder(BiTreeNode* t, void Visit(DataType item))
{
if (t != NULL)
{
PostOrder(t->leftChild, Visit);
PostOrder(t->rightChild, Visit);
Visit(t->data);
}
}
源文件:main.c
#include"BiTreeTraverse.h"
int Like(BiTreeNode* a, BiTreeNode* b)
{
if ((a == NULL && b != NULL) || (a != NULL && b == NULL))
return 0;
if (a == NULL && b == NULL)
return 1;
return Like(a->leftChild, b->leftChild) && Like(a->rightChild, b->rightChild);
}
int main()
{
BiTreeNode* root1, * root2, * p, * pp;
Initiate(&root1);
Initiate(&root2);
p = InsertLeftNode(root1, '2');
p = InsertLeftNode(p, '6');
p = InsertRightNode(root1->leftChild, '4');
pp = p;
p = InsertLeftNode(p, '5');
p = InsertLeftNode(p, '3');
InsertRightNode(pp, '1');
p = InsertLeftNode(root2, '1');
p = InsertLeftNode(p, '2');
p = InsertRightNode(root2->leftChild, '3');
pp = p;
p = InsertLeftNode(p, '4');
p = InsertLeftNode(p, '5');
InsertRightNode(pp, '7');
PrintBiTree(root1, 0);
printf("\n");
PrintBiTree(root2, 0);
if (Like(root1, root2))
printf("两棵树相似!\n");
else
printf("两棵树不相似!\n");
Destroy(&root1);
Destroy(&root2);
return 0;
}