#include <iostream>
#include <queue>
using namespace std;
typedef char ElemType;
typedef struct BtNode //定义二叉树节点
{
BtNode *leftchild;
BtNode *rightchild;
ElemType data;
}BtNode, *BinaryTree;
BtNode *BuyNode() //购买节点
{
BtNode *s = (BtNode*)malloc(sizeof(BtNode));
if(s == NULL) exit(1);
memset(s,0,sizeof(BtNode)); //将申请的节点初始化为0
return s;
}
void FreeNode(BtNode *p) //释放结点
{
free(p);
}
BtNode *CreateTree(ElemType *&str) //创建二叉树
{
BtNode *s = NULL;
if(*str != '#')
{
s = BuyNode();
s->data = *str;
s->leftchild = CreateTree(++str);
s->rightchild = CreateTree(++str);
}
return s;
}
//求节点个数:用递归实现
int GetSize(BtNode *p)
{
if(p == NULL)
return 0;
else
return GetSize(p->leftchild) + GetSize(p->rightchild) + 1; //1表示根节点个数
}
//求树的深度
int Depth(BtNode *p)
{
if(p == NULL)
return 0;
else
return max(Depth(p->leftchild), Depth(p->rightchild)) + 1;
}
//查找value值
BtNode * FindValue(BtNode *ptr, ElemType x)
{
if(ptr == NULL || ptr->data == x)
{
return ptr;
}
else
{
BtNode* s = FindValue(ptr->leftchild, x); //先从左边找,找到直接返回s
if(s == NULL)
{
s = FindValue(ptr->rightchild, x); //若左边没找到,则从右边找
}
return s;
}
}
//查找父结点
BtNode* FindParent(BtNode *ptr, BtNode *child)
{
if(ptr == NULL || child == NULL || ptr == child)
{
return NULL;
}
else
{
return FindParent(ptr, child);
}
}
BtNode* Parent(BtNode* ptr, BtNode *child)
{
if(ptr == NULL || ptr->leftchild == child || ptr->rightchild == child)
{
return ptr;
}
else
{
BtNode *s = Parent(ptr->leftchild, child);
if(s == NULL)
{
s = Parent(ptr->rightchild, child);
}
return s;
}
}
//是否为满二叉树
bool Is_Full_BinaryTree(BtNode* ptr)
{
bool tag = true;
int n = 1;
queue<BtNode*> qu;
qu.push(ptr);
while(!qu.empty())
{
int i=0;
for(;i<n && !qu.empty(); i++)
{
BtNode *p = qu.front();
qu.pop();
if(p->leftchild != NULL)
{
qu.push(p->leftchild);
}
if(p->rightchild != NULL)
{
qu.push(p->rightchild);
}
}
if(i < n)
{
tag = false;
break;
}
n += n;
}
return tag;
}
//判断两个二叉树是否相等
//思想:先序递归实现,先判断根节点是否相等,再判断左右子树是否相等
bool Is_Comp_BinaryTree1(BtNode *ptr1, BtNode *ptr2)
{
if(ptr1 == NULL && ptr2 == NULL) //两棵树递归到终点
{
return true;
}
if(!ptr1 || !ptr2) //树的结构不一样
{
return false;
}
if(ptr1->data == ptr2->data) //判断根节点是否相等
{
return true;
}
else
{
return false;
}
}
//判断为完全二叉树
//思想:用队列进行层次性遍历,判断遇到第一个为空之后的结点是否为空,若都为空则true,否则false
bool Is_wanquan_BinaryTree2(BtNode *ptr)
{
//bool res = true;
if(ptr == NULL)
{
return true;
}
queue<BtNode *> qu;
qu.push(ptr);
while(ptr != NULL)
{
ptr = qu.front(); qu.pop();
if(ptr == NULL ) break;
qu.push(ptr->leftchild);
qu.push(ptr->rightchild);
}
while(!qu.empty())
{
ptr = qu.front(); qu.pop();
if(ptr != NULL)
{
return false;
}
}
return true;
}
//使用队列的方法输出树的各层结点
void NiceLevelOrder(BtNode *p)
{
if(p == NULL) return;
queue<BtNode *> qu;
qu.push(p);
while(!qu.empty())
{
BtNode *p = qu.front();
qu.pop();
if(p->leftchild != NULL)
{
qu.push(p->leftchild);
}
if(p->rightchild != NULL)
{
qu.push(p->rightchild);
}
}
cout<<endl;
}
int main()
{
BinaryTree root1 = NULL;
char *str1 = "ABC##DE##F##G#H##";
root1 = CreateTree(str1);
BinaryTree root2 = NULL;
char *str2 = "ABC##DE##F##G#H##";
root2 = CreateTree(str2);
Depth(root1); //树的深度
FindValue(root1, 2); //val值
FindParent(root1, root1->leftchild); //查找父结点
Is_Full_BinaryTree(root1); //是否为满二叉树
Is_Comp_BinaryTree1(root1, root2); //两个二叉树是否相等
Is_wanquan_BinaryTree2(root1); //是否为完全二叉树
NiceLevelOrder(root1); ////使用队列的方法输出树的各层结点
cout<<endl;
return 0;
}