二叉树的基本操作

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>


typedef struct Node
{
char data;
    struct Node *Lchild;
    struct Node *Rchild;
     
 }BiTNode,*BiTree; 
 
/*************************************/ 
void createBiTree(BiTree *root)                  //扩展先序遍历序列创建二叉树 
{
char ch;
ch    = getchar();
if(ch == '^')  *root=NULL;
else
{
*root=(BiTree)malloc(sizeof(BiTNode));
(*root)->data=ch;
createBiTree(&((*root)->Lchild));
createBiTree(&((*root)->Rchild));
}  
}
void printTree(BiTree root,int h)               //按树状输出二叉树 
{
int i;
if(root==NULL) return ;
printTree(root->Rchild,h+1);
for(i=0;i<h;i++) printf(" ");
printf("%c\n",root->data);
printTree(root->Lchild,h+1);
   
}
/*************************************/




/************************************/
void Preorder(BiTree root){              //先序遍历算法 
    if(root){ 
        printf("%2c",root->data);
        Preorder(root->Lchild);
        Preorder(root->Rchild);
    }
}
/***********************************/
void Inorder(BiTree root){              //中序遍历算法 
    if(root==NULL) return;
        Inorder(root->Lchild);
        printf("%2c",root->data);
        Inorder(root->Rchild);
}
/***********************************/
void NRInorder(BiTree root)              //非递归中序遍历 
{          
BiTree s;             //s-指向当前节点
BiTree stack[100];               //定义栈
int    top=-1;                 //初始化栈顶指针

if(root==NULL)
return;

stack[++top]=root;                  //根指针入栈
s=root->Lchild;                     //s指向左子树
while(s!=NULL||top!=-1)             //当存在节点(涉及到根下右子树)或者栈不为空,进行遍历
{
while(s!=NULL)                  //如果存在节点,寻找最左子树并入栈
{
if(top>=99)
{
printf("栈为满\n");
return;
}
stack[++top]=s;            //当前节点入栈
s=s->Lchild;               //左子树进行遍历
}


if(top==-1)
{
printf("栈为空\n");
return;
}


s=stack[top--];      //弹出栈顶元素到s中  
printf("%c ",s->data);      //输出当前节点元素值
s=s->Rchild;      //遍历右子树

}
}
/*************************************/ 
void Postorder(BiTree root){              //后序遍历算法 
    if(root){ 
        Postorder(root->Lchild);
        Postorder(root->Rchild);
        printf("%2c",root->data);
    }
}
/***********************************/       //树的高度 
int PostTreeDepth(BiTree root)
{
int hl,hr,h;
if(root==NULL) return 0;
else
{
hl=PostTreeDepth(root->Lchild);
hr=PostTreeDepth(root->Rchild);
h =(hl>hr? hl:hr)+1;
return h;
}

/**********************************/ 
int PostTreeLeaf(BiTree root)              //后序遍历求节点数 
{
int count,count1,count2;
if(root == NULL) 
   return 0;
if(root->Lchild == NULL && root->Rchild == NULL)
   return 1;
count1=PostTreeLeaf(root->Lchild);
count2=PostTreeLeaf(root->Rchild);
count =count1+count2;
return count;
}
/***********************************/
int PostTreeOneAspectLeaf(BiTree root,int aspect)      //求树每层有多少个叶子 
{
int count,count1,count2;
if(root==NULL||aspect==0) return 0;
if(aspect==1&&root->Lchild==NULL&&root->Rchild==NULL) return 1;
count1=PostTreeOneAspectLeaf(root->Lchild,aspect-1);
count2=PostTreeOneAspectLeaf(root->Rchild,aspect-1);
count=count1+count2; 
   return count;
   
}
BiTree SDParents(BiTree root,BiTree current)
{
BiTree p;
if(root==NULL) return NULL;
if(root->Lchild==current||root->Rchild==current)
    return root;
p=SDParents(root->Lchild,current);
    if(p!=NULL) return p;
    else return(SDParents(root->Rchild,current));
}
int main()
{
BiTree root=NULL;
int h;            //树的高度;
int count;        //树的叶子节点的个数 
int aspect=1;       //树的层数  
char ch;

printf("请按扩展的先序遍历序列输入树的节点数据:\n");
createBiTree(&root);

printf("按树状结果输出:\n");
printTree(root,1); 

printf("\n先序遍历输出:\n");
Preorder(root);

printf("\n中序遍历输出:\n");
Inorder(root);

printf("\n非递归中序遍历输出:\n");
NRInorder(root);

printf("\n后序遍历输出:\n");
Postorder(root);

h=PostTreeDepth(root);
printf("\n树的高度为:\n");
printf("%d",h); 

count=PostTreeLeaf(root);
printf("\n树的叶子节点个数为:\n");
printf("%d\n",count); 

printf("第%d层有%d个叶子结点",aspect,count);

printf("\n结点B的双亲为:\n");
root=SDParents(root,root->Lchild);
printf("%c",root->data);

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树是一种非常重要的数据结构,它的基本操作包括创建、销毁、遍历、查找等。下面是二叉树基本操作的实现方法: 1. 创建二叉树:通过前序遍历的数组构建二叉树,其中 '#' 表示空节点。具体实现方法可以参考引用中的 BinaryTreeCreate 函数。 2. 销毁二叉树:遍历二叉树,依次释放每个节点的内存空间。具体实现方法可以参考引用中的 BinaryTreeDestory 函数。 3. 遍历二叉树二叉树的遍历包括前序遍历、中序遍历、后序遍历和层序遍历。具体实现方法可以参考引用中的 BinaryTreePrevOrder、BinaryTreeInOrder、BinaryTreePostOrder 和 BinaryTreeLevelOrder 函数。 4. 查找二叉树节点:在二叉树中查找值为 x 的节点,具体实现方法可以参考引用中的 BinaryTreeFind 函数。 5. 计算二叉树节点个数:计算二叉树中节点的个数,具体实现方法可以参考引用[2]中的 BinaryTreeSize 函数。 6. 计算二叉树叶子节点个数:计算二叉树中叶子节点的个数,具体实现方法可以参考引用中的 BinaryTreeLeafSize 函数。 7. 计算二叉树第 k 层节点个数:计算二叉树中第 k 层节点的个数,具体实现方法可以参考引用中的 BinaryTreeLevelKSize 函数。 8. 判断二叉树是否是完全二叉树:判断二叉树是否是完全二叉树,具体实现方法可以参考引用中的 BinaryTreeComplete 函数。 9. 计算二叉树的深度:计算二叉树的深度,具体实现方法可以参考引用中的 BinaryTreeDeep 函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值