网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

c语言二叉树的主函数 C语言二叉树的基本操作

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

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

成都创新互联公司长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为汤阴企业提供专业的成都网站建设、成都网站制作,汤阴网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。

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语言:建立二叉树,在main方法里写代码调试?

#includestdlib.h

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));/*调用函数输出树中叶子结点个数*/

}

二叉树的基本操作 C语言版的

#include iostream.h

typedef struct BiTNode

{

char data;

int bit;

struct BiTNode *lchild,*rchild,*parent;

}BiTNode;

void InitBT(BiTNode *t)//1、初始化,不带头结点

{

t=NULL;

}

/*void InitBT(BiTNode *t)//初始化,带头结点

{

t=new BiTNode;

t-lchild=t-rchild=t-parent=NULL;

}*/

int EmptyBT(BiTNode *t)//判断队空

{

if(t==0)

return 1;

else

return 0;

}

BiTNode *creatBT(BiTNode *t,int b)//2、创建二叉树

{

BiTNode *p;

char ch;

cinch;

if(ch=='#')return 0;

else

{

p=new BiTNode;

p-data=ch;

p-parent=t;

p-bit=b;

t=p;

t-lchild=creatBT(t,0);

t-rchild=creatBT(t,1);

}

return t;

}

void preorder(BiTNode *t)//3、先序遍历

{

if(!EmptyBT(t))

{

coutt-data;

preorder(t-lchild);

preorder(t-rchild);

}

}

void inorder(BiTNode *t)//中序遍历

{

if(!EmptyBT(t))

{

inorder(t-lchild);

coutt-data;

inorder(t-rchild);

}

}

void postorder(BiTNode *t)//后序遍历

{

if(!EmptyBT(t))

{

postorder(t-lchild);

postorder(t-rchild);

coutt-data;

}

}

void coutBT(BiTNode *t,int m,int n,int i)//4、计算二叉树中叶子结点、度为2的结点和度为1的结点的个数

{

if(!EmptyBT(t))

{

if((t-lchild==0) (t-rchild==0))

m++;//叶子结点

else if((t-lchild!=0) (t-rchild!=0))

i++;//度为2的结点

else

n++;//度为1的结点

coutBT(t-lchild,m,n,i);

coutBT(t-rchild,m,n,i);

}

}

void coutNode(BiTNode *t,int k)//5、求二叉树中结点个数

{

if(!EmptyBT(t))

{

k++;

coutNode(t-lchild,k);

coutNode(t-rchild,k);

}

}

int BTdepth(BiTNode *t)//6、求二叉树的深度

{

int i,j;

if(EmptyBT(t))

return 0;

else

{

i=BTdepth(t-lchild);

j=BTdepth(t-rchild);

return (ij?i:j)+1;

}

}

int Xdepth(BiTNode *t,char x)//7、查找x的层数

{

int num1,num2,n;

if(t==NULL)

return 0;

else{

if(t-data==x)

return 1;

num1=Xdepth(t-lchild,x);

num2=Xdepth(t-rchild,x);

n=num1+num2;

if(num1!=0||num2!=0)

n++;

return n;

}

}

static int flag;

void SearchChild(BiTNode *t,int k)//8、查找第k个结点的左右孩子

{

if(!EmptyBT(t))

{

if(k==0)

{

cout"位置不能为0!"endl;

return;

}

else

{

flag++;

if(flag==k)

{

if(t-lchild==0)

cout"无左孩子! ";

else

cout"左孩子为:"(t-lchild-data)" ";

if(t-rchild==0)

cout"无右孩子!"endl;

else

cout"右孩子为:"(t-rchild-data)endl;

}

else

{

SearchChild(t-lchild,k);

SearchChild(t-rchild,k);

}

}

}

}

int Xancestor(BiTNode *t,char x)//9、查找x结点祖先

{

int n,num1,num2;

if(t==NULL)

return 0;

else

{

if(t-data==x)

return 1;

num1=Xancestor(t-lchild,x);

num2=Xancestor(t-rchild,x);

n=num1+num2;

if(n!=0)

{

n++;

coutt-data" "endl;

}

}

}

void BTNodePath(BiTNode *t)//10、输出所有叶子结点路径

{

if(!EmptyBT(t))

{

if((t-lchild==0) (t-rchild==0))

{

coutt-data"的路径为:";

for(BiTNode *p=t;p!=0;p=p-parent)

coutp-data;

coutendl;

}

else

{

BTNodePath(t-lchild);

BTNodePath(t-rchild);

}

}

}

void BTNodebit(BiTNode *t)//11、输出所有叶子结点编码

{

if(!EmptyBT(t))

{

if((t-lchild==0) (t-rchild==0))

{

coutt-data"的编码为:";

for(BiTNode *p=t;p-parent!=0;p=p-parent)

coutp-bit;

coutendl;

}

else

{

BTNodebit(t-lchild);

BTNodebit(t-rchild);

}

}

}

void main()

{

BiTNode *t;

int m,n,i,d,q,k;

char x;

cout"1、初始化..."endl;

InitBT(t);

cout"2、创建二叉树..."endl;

t=creatBT(t,0);

cout"3.1、先序遍历..."endl;

preorder(t);

coutendl;

cout"3.2、中序遍历..."endl;

inorder(t);

coutendl;

cout"3.3、后序遍历..."endl;

postorder(t);

coutendl;

m=n=i=0;

cout"4、计算叶子结点,度为1的结点和度为2的结点的个数..."endl;

coutBT(t,m,n,i);

cout"叶子结点个数为:"mendl;

cout"度为1的结点个数为:"nendl;

cout"度为2的结点个数为:"iendl;

q=0;

cout"5、计算结点个数..."endl;

coutNode(t,q);

cout"结点个数为:"qendl;

d=0;

cout"6、计算深度..."endl;

d=BTdepth(t);

cout"深度为:"dendl;

cout"7、求x的层数..."endl;

cout"输入x:";

cinx;

if(Xdepth(t,x)==0)

cout"x不存在!"endl;

else

coutXdepth(t,x)endl;

cout"8、输入要查找孩子的结点在先序遍历中的位置k(不等于0):";

cink;

SearchChild(t,k);

if(kflag)

cout"位置超出长度!"endl;

cout"9、查询结点的所有祖先,请输入结点x:";

cinx;

int num;

num=Xancestor(t,x);

if(num==0)

cout"结点不存在!"endl;

if(num==1)

cout"根结点无祖先!"endl;

cout"10、输出所有叶子结点路径(叶→根):"endl;

BTNodePath(t);

cout"11、输出所有叶子结点编码(叶→根):"endl;

BTNodebit(t);

}


文章题目:c语言二叉树的主函数 C语言二叉树的基本操作
标题来源:http://bjjierui.cn/article/hjogeo.html

其他资讯