1、模板
int Count(BiTree T){
if(T==NULL)
return 0;//空树,返回0
else if(//自己填)
return Count(T->lchild) + Count(T->rchild) + 1;
else
return Count(T->lchild) + Count(T->rchild);
}
1、(1)以二叉链表作为二叉树的存储结构,统计二叉树的叶子结点个数。
int Count(BiTree T){
if(T==NULL)
return 0;//空树,返回0
else if(T->lchild==NULL && T->rchild==NULL)//叶子节点
return 1;
else
return Count(T->lchild) + Count(T->rchild);
}
1、(2)统计二叉树中单分支(度为1)的结点个数。
int Count(BiTree T){
if(T==NULL)
return 0;
else if((T->lchild!=NULL&&T->rchild==NULL) || (T->lchild==NULL&&T->rchild!=NULL))
return Count(T->lchild)+Count(T->rchild)+1;//单分支结点
else
return Count(T->lchild)+Count(T->rchild);
}
1、(3)统计二叉树中双分支(度为2)结点个数。
int Count(BiTree T){
if(T==NULL)
return 0;
else if(T->lchild!=NULL && T->rchild!=NULL)//双分支结点
return Count(T->lchild)+Count(T->rchild)+1;
else
return Count(T->lchild)+Count(T->rchild);
}
1、(4)统计二叉树结点个数
int Count(BiTree T){
if(T==NULL)
return 0;
else
return Count(T->lchild)+Count(T->rchild)+1;
}
2、以二叉链表作为二叉树的存储结构,判别两棵树是否相等。
int CmpTree(BiTree T1, BiTree T2){
if(T1==NULL && T2==NULL)
return 1;//都是NULL,相等
else if(T1==NULL || T2==NULL)
return 0;//只有一个为NULL,不等
if(T1->data != T2->data)//根节点不相等,直接返回不等,否则递归
return 0;
left = right = 0;
left = CmpTree(T1->lchild, T2->lchild);
right = CmpTree(T1->rchild, T2->rchild);
return left&&right;
}
3、以二叉链表作为二叉树的存储结构,交换二叉树每个结点的左孩子和右孩子。
void ChangeLR(BiTree &T){
if(T==NULL) return;
if(T->lchild==NULL && T->rchild==NULL)//左右子树均为空,返回
else{//交换左右孩子结点
temp = T->lchild;
T->lchild = T->rchild;
T->rchild = temp;
}
ChangeLR(T->lchild);
ChangeLR(T->rchild);
}
4、设计二叉树的双序遍历算法(双序遍历是指对于二叉树的每一个结点来说,先访问这个结点,再按双序遍历它的左子树,然后再一次访问这个结点,接下来按双序遍历它的右子树)。
void DoubleTraverse(BiTree T){
if(T){//若二叉树非空
print(T->data);
DoubleTraverse(T->lchild);
print(T->data);
DoubleTraverse(T->rchild);
}
}
5、计算二叉树的最大宽度(二叉树的最大宽度是指二叉树所有层中结点个数的最大值)。
int Max=0;//Max为宽度最大值
int conut[MaxSize];//count[i]为第i层结点个数,即第i层宽度
void getWidth(BiTree T,int k){ //k为当前层数,从1开始
if(T==NULL)// T为空直接返回
return;
else
conut[k]++;// T不为空,第k层结点+1
if(conut[k]>Max)//更新最大宽度
Max=conut[k];
getWidth(T->lchild,k+1);
getWidth(T->Rchild,k+1);
}
6、二叉树的层序遍历。
void LevelOrder(BiTree T){
InitQueue(Q);//初始化队列
BiTree p;
EnQueue(Q, T);//根结点入队
while(!IsEmpty(Q)){//队列不空则循环
DeQueue(Q,p)//出队
visit(p);//访问出队结点
if(p->lchild!=NULL)
EnQueue(Q, p->lchild);//左子树根结点入队
if(p->rchild!=NULL)
EnQueue(Q, p->rchild);//右子树根结点入队
}
}
7、用按层次顺序遍历二叉树的方法,统计树中具有度为1的结点数目。
int Level(BiTree T){
int num = 0;//num为度为 1结点的个数
InitQueue(Q);//初始化队列
BiTree p;
EnQueue(Q, T);//根结点入队
while(!IsEmpty(Q)){//队列不空则循环
DeQueue(Q,p)//出队
visit(p);//访问出队结点
if((p->lchild&&!p->rchild) || (!p->lchild&&p->rchild))
num++; //度为 1的结点
if(p->lchild!=NULL)
EnQueue(Q, p->lchild);//左子树根结点入队
if(p->rchild!=NULL)
EnQueue(Q, p->rchild);//右子树根结点入队
}
return num;
}
8、设计一个求结点 x 在二叉树中的双亲结点的算法。
void Parent(BiTree T, int x){
if(T!=NULL){
if(T->lchild && T->lchild==x){
print(T->data);//T的双亲结点为x
return;
}
if(T->rchild && T->rchild==x){
print(T->data);//T的双亲结点为x
return;
}
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
9、二叉树的三种递归遍历
//先序遍历
void PreOrder(BiTree T){
if(T!=NULL){
visit(T);//访问根节点
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
//中序遍历
void InOrder(BiTree T){
if(T!=NULL){
PreOrder(T->lchild);
visit(T);//访问根节点
PreOrder(T->rchild);
}
}
//后序遍历
void PostOrder(BiTree T){
if(T!=NULL){
PreOrder(T->lchild);
PreOrder(T->rchild);
visit(T);//访问根节点
}
}
10、在一棵以二叉链表存储的二叉树中,查找data域值等于key的结点是否存在,若存在则将指针q指向该结点。
void Parent(BiTree T, BiTNode *q, int key){
if(T!=NULL){
if(T->data==key){
q = T;
return;
}
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
11、二叉树采用二叉链表形式存储,设计一个算法,求先序遍历中第 k 个结点的值。
void Search_k(BiTree T, int k, int cnt){
//cnt用来记录遍历到了第几个结点
if(T!=NULL){
if(cnt == k){
print(T->data);//输出第k个结点的值
return;
}
PreOrder(T->lchild, k, cnt++);
PreOrder(T->rchild, k, cnt++);
}
}
12、求二叉树深度(高度)。
int Getdepth(BiTree* T) {//求二叉树深度(高度)
if (T == NULL)
return 0;
left = Getdepth(T->Lchild);//递归求左子树高度
right = Getdepth(T->Rchild);//递归求右子树高度
if(left >= right)
depth = 1+left;
else
depth = 1+right;
return depth;
}
13、二叉树采用二叉链表存储,求二叉树 T 中值为 x 的结点的层次号。
void Search_level(BiTree T, int x, int level){//level从1开始
if(T!=NULL){
if(T->data==x){
print(level);//输出x所在层数
return;
}
Search_level(T->lchild, x, level+1);
Search_level(T->rchild, x, level+1);
}
}
文章详细介绍了二叉树的各种操作,包括计数(叶子节点、单分支、双分支和总节点)、判断树的等价性、节点交换、遍历方法(先序、中序、后序、双序和层次遍历)、宽度计算、深度查找、双亲节点搜索以及特定节点层次号的求解。

6955

被折叠的 条评论
为什么被折叠?



