成都创新互联网站制作重庆分公司

c语言二叉树主函数怎么写 c语言二叉树怎么建立

C语言:建立二叉树,在main方法里写代码调试?

#includestdlib.h

创新互联-专业网站定制、快速模板网站建设、高性价比沧源网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式沧源网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖沧源地区。费用合理售后完善,10年实体公司更值得信赖。

typedef struct node/*二叉链表结构声明*/

{

struct node *lchild;

char data;

struct node *rchild;

}bitnode,*bitree;/*bitnode、bitree为该结构体类型*/

bitree CreatTree()/*创建二叉链表*/

{

char a;

bitree new;

scanf("%c",a);

if(a=='#')

return NULL;

else

{

new=(bitree)malloc(sizeof(bitnode));

new-data=a;

new-lchild=CreatTree();/*递归创建左子树*/

new-rchild=CreatTree();/*递归创建右子树*/

}

return new;

}

int btreedepth(bitree bt)/*自定义函数btreedepth()求二叉树的深度*/

{

int ldepth,rdepth;

if(bt==NULL)

return 0;

else

{

ldepth=btreedepth(bt-lchild);

rdepth=btreedepth(bt-rchild);

return (ldepthrdepth?ldepth+1:rdepth+1);

}

}

int ncount(bitree bt)/*自定义函数ncount求结点的个数*/

{

if(bt==NULL)

return 0;

else return(ncount(bt-lchild)+ncount(bt-rchild)+1);

}

int lcount(bitree bt)/*自定义函数lcount求叶子结点的个数*/

{

if(bt==NULL)

return 0;

else if(bt-lchild==NULLbt-rchild==NULL)

return 1;

else return(lcount(bt-lchild)+lcount(bt-rchild));

}

void print(bitree bt)/*自定义函数print用中序遍历的方式输出二叉树结点内容*/

{

if(bt!=NULL)

{

print(bt-lchild);

printf("%c",bt-data);

print(bt-rchild);

}

}

void main()

{

bitree root;

root=CreatTree();/*调用函数创建二叉链表*/

printf("contents of binary tree:\n");

print(root);/*调用函数输出结点内容*/

printf("\ndepth of binary tree:%d\n",btreedepth(root));/*调用函数输出树的深度*/

printf("the number of the nodes:%d\n",ncount(root));/*调用函数输出树中结点个数*/

printf("the number of the leaf nodes:%d\n",lcount(root));/*调用函数输出树中叶子结点个数*/

}

求教二叉树的main函数怎么写合适:

我的能力也有限,学数据结构过的时间有点久了,而且这个程序我读的很吃力,没用过这样子的语言来写呢,刚刚写了个主类可是运行还是有错误,我又不会改,不好意思。。。我想要的主类大体是这样写的,你可以参考一下:

void main()

{

BiTree Tr;//这里定义的东西在这个程序里也不行,本来是想让那个BinTree是个指针的,可是这个程序俺也不大会弄

printf("按前序次序输入,以#表示为空:\n");

CreateBinTree(Tr,T,i);//这个括号里面的内容我也不知该怎么写,程序大体读了读 貌似不大会

printf("\n前序遍历结果为:\n");

PreOrder(Tr);//反正括号里的内容就是你前面写的那个函数括号里相应的

printf("\n中序遍历结果为:\n");

InOrder(Tr);

printf("\n后序遍历结果为:\n");

PostOder(Tr);

printf("\n层序遍历结果为:\n");

LevelOrder(Tr);

printf("\n该二叉树的深度为:\n%d",countHighOfBiTree(Tr));

printf("\n该二叉树的叶子节点个数为:\n");

countNumOfLeaf(Tr);

printf("\n该二叉树的所有结点数为:\n");

Count(Tr);

printf("\n");

}

这里是实验课上老师布置给我们的,然后自己写的,语言和你的不大一样 但思路差不多,你可以看看这个的,毕竟我还是学的时候思路比较清晰啦,嘿嘿,貌似~是按前序序列来创建的二叉树,你输入的前序序列一定要是正确的哦~我的这个程序还很低级,错误的它不会提示,不好意思哈,学习不大好,只能帮到这里了

#include "stdio.h"

#include "conio.h"

#include "stdlib.h"

#includemalloc.h

#includestdlib.h

//#includestdio.h

#includestring.h

#define NULL 0

typedef char Elemtype;

typedef struct BinNode

{

Elemtype data;

struct BinNode *lchild,*rchild;//左右孩子指针

}BinTNode,*BinTree;

//按前序构造二叉树链表表示的二叉树序列

BinTree CreateBinTree(BinTree T)

{

char ch;

scanf("%c,\n",ch);

if(ch=='#') T=NULL;

else

{

T=(BinNode *)malloc(sizeof(BinNode));

T-data=ch;//生成根结点

CreateBinTree(T-lchild);//生成左子树

CreateBinTree(T-rchild);//生成右子树

}//if

return T;

}//CreateBinTree

void Visit(char dataa)

{

printf("%c",dataa);

}

//前序遍历二叉树

void PreOrderTraverse(BinTree T)

{

//前序遍历二叉树T的递归算法,Visit是访问数据元素的函数

if(T)//二叉树非空

{

Visit(T-data);//访问根结点

PreOrderTraverse(T-lchild);//前序遍历左子树

PreOrderTraverse(T-rchild);//前序遍历右子树

}//if

}//PreOrderTraverse

//中序遍历二叉树

void InOrderTraverse(BinTree T)

{

//中序遍历二叉树T的递归算法,Visit是访问数据元素的函数

if(T)//二叉树非空

{

InOrderTraverse(T-lchild);//中序遍历左子树

Visit(T-data);//访问根结点

InOrderTraverse(T-rchild);//中序遍历右子树

}//if

}//InOrderTraverse

void PostOrderTraverse(BinTree T)

{

//后序遍历二叉树T的递归算法,visit是访问数据元素的函数

if(T)//二叉树非空

{

PostOrderTraverse(T-lchild);//后序遍历左子树

PostOrderTraverse(T-rchild);//后序遍历右子树

Visit(T-data);//访问根结点

}//if

}//PostOrderTraverse

//求二叉树的深度

int Depth(BinTree T)

{

int DepthLeft,DepthRight,depthval;

if(!T)

return 0;

else

{

DepthLeft=Depth(T-lchild);

DepthRight=Depth(T-rchild);

depthval=1+(DepthLeftDepthRight?DepthLeft:DepthRight);

return depthval;

}//if

}//Depth

void CountLeaf(BinTree T,int count0,int count2)

{

//统计二叉树中的叶子节点个数

if(T)

{

if((!T-lchild)(!T-rchild))

count0++;

CountLeaf(T-lchild,count0,count2);

CountLeaf(T-rchild,count0,count2);

}

count2=count0-1;

}

void Countduone(BinTree T,int count1)

{

//统计二叉树中度为1的结点个数

if(T)

{

if(((!T-lchild)(T-rchild))||((T-lchild)(!T-rchild)))

count1++;

Countduone(T-lchild,count1);

Countduone(T-rchild,count1);

}

}

int ZongNode(int a,int b,int c)

{

return (a+b+c);

}

void main()

{

BinTree Tr;

int count0,count1,count2;

int jie;

count0=0;

count1=0;

count2=0;

printf("按前序次序输入,以#表示为空:\n");

CreateBinTree(Tr);

printf("\n前序遍历结果为:\n");

PreOrderTraverse(Tr);

printf("\n中序遍历结果为:\n");

InOrderTraverse(Tr);

printf("\n后序遍历结果为:\n");

PostOrderTraverse(Tr);

printf("\n该二叉树的深度为:\n%d",Depth(Tr));

printf("\n该二叉树的叶子节点个数为:\n");

CountLeaf(Tr,count0,count2);

printf("%d",count0);

printf("\n该二叉树的所有结点数为:\n");

//CountLeaf(Tr,count0,count2);

Countduone(Tr,count1);

jie=ZongNode(count1,count2,count0);

printf("%d",jie);

printf("\n");

}

二叉树前、中、后遍历后要用括号表示法输出;主函数怎么写啊。

#include iostream

using std::cin;

using std::cout;

using std::endl;

//using namespace std;

typedef struct BiTNode {

char data;

struct BiTNode *Lchild, *Rchild; // 左、右孩子指针

} *BiTree;

void CreateBiTree(BiTree T){

以B为根节点的左子树 A根节点 以C为根节点的右子树

以D为根节点的左子树 B根节点 以E为根节点的右子树

以G为根节点的左子树 D根节点 以H为根节点的右子树

以K为根节点的左子树 C根节点 以F为根节点的右子树

以I为根节点的左子树 F根节点 右子树为空

左子树为空 I根节点 以J为根节点的右子树

扩展资料:

主函数的两个形参形式中的形参,允许从执行环境中传递任意的多字节字符串(它们通常被称为命令行参数),各个指针 argv[1] .. argv[argc-1] 指向每个这些字符串的第一个字符。argv[0] 是指向一个表示用于执行该程序自身的名字的空结尾多字节字符串(或者当执行环境不支持时,为空字符串 "")的开头字符的指针。

这些字符串是可以改动的,虽然对它们的改动并不会被传回给执行环境:比如可以用 std::strtok 来使用它们。由 argv 所指向的数组的大小至少为 argc+1,其最后一个元素 argv[argc] 保证为一个空指针。

参考资料来源:百度百科-main函数


当前名称:c语言二叉树主函数怎么写 c语言二叉树怎么建立
网站网址:http://cxhlcq.com/article/hjgcds.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部