本篇文章给大家谈谈树c语言基本算法,以及c++抽象语法树对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、C语言中基本的几种算法有哪些越多越好!就像打擂台算法’冒泡排序法等等…2、用C语言编程实现二叉树的中序遍历算法3、二叉树的基本操作 C语言版的4、C语言二叉树递归算法怎么做?5、用C语言的递归算法求树的叶子数
C语言中基本的几种算法有哪些越多越好!就像打擂台算法’冒泡排序法等等…
排序算法
冒泡排序
选择排序
快速排序
高精度运算
存储方法
加法运算
减法运算
乘法运算
扩大进制数
习题与练习
搜索算法
枚举算法
深度优先搜索
广度优先搜索
8数码问题
n皇后问题
搜索算法习题
枚举法习题
聪明的打字员
量水问题
染色问题
跳马问题
算24点
图论算法
最小生成树算法(Prim算法)
单源最短路径算法(Dijkstra算法)
任意结点最短路径算法(Floyd算法)
求有向带权图的所有环
Bellman-Ford算法
计算图的连通性
计算最佳连通分支
计算拓扑序列
图论算法习题
网络建设问题
最短变换问题
挖地雷
乌托邦城市
乌托邦交通中心
动态规划
最短路径问题
动态规划概念
骑士游历问题
最长递增子序列
合唱队形
石子合并问题
能量项链
0/1背包问题
开心的金明
金明的预算方案
加分二叉树
字串编辑距离
花瓶插花
凸多边形三角划分
快餐店
用C语言编程实现二叉树的中序遍历算法
#includestdio.h
#includestdlib.h
struct BiTNode *stack[100];
struct BiTNode//定义结构体
{
char data;
struct BiTNode *lchild,*rchild;
};
void later(struct BiTNode *p) //前序创建树
{
char ch;
scanf(“%c”,ch);
if(ch==’ ‘)
p=NULL;
else
{
p=(struct BiTNode *)malloc(sizeof(struct BiTNode));
p-data=ch;
later(p-lchild);
later(p-rchild);
}
}
void print(struct BiTNode *p) //前序遍历(输出二叉树)
{
int i=-1;
while(1)
{
while(p!=NULL)
{
stack[++i]=p-rchild;/*printf(“ok?\n”);*/
printf(“%c”,p-data);
p=p-lchild;
}
if(i!=-1)
{
p=stack[i];
i–;
}
else
return;
}
}
void main()//主函数
{
struct BiTNode *p,*t;
later(p);
print(p);
}
二叉树的基本操作 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语言二叉树递归算法怎么做?
#include stdio.h
#include string.h
struct treenode{
int value;
treenode* left;
treenode* right;
};
typedef treenode* BiTree;
void visit(treenode* node)
{
printf(“%2d “, node-value);
}
// 结点总数
int node(BiTree T)
{
if( !T ){
return 0;
}
return node(T-left) + node(T-right) + 1;
}
// 前序
void preOrder(BiTree T)
{
if( T ){
visit(T);
preOrder(T-left);
preOrder(T-right);
}
}
// 中序
void inOrder(BiTree T)
{
if( T ){
inOrder(T-left);
visit(T);
inOrder(T-right);
}
}
// 后序
void postOrder(BiTree T)
{
if( T ){
postOrder(T-left);
postOrder(T-right);
visit(T);
}
}
// 叶子节点数
int leafnode(BiTree T)
{
if( T ){
if( !T-left !T-right )
return 1;
else
leafnode(T-left) + leafnode(T-right);
}else{
return 0;
}
}
int height(BiTree T)
{
if( T ){
int lh = height(T-left);
int rh = height(T-right);
return (lhrh ? lh:rh) + 1;
}else{
return 0;
}
}
int main()
{
return 0;
}
用C语言的递归算法求树的叶子数
typedef
struct
node
{
struct
node
*left;
struct
node
*right;
}Node;
int
Leaf(Node*
tree)
{
if(tree==NULL)
//终止条件1,tree指向NULL时返回0
{
return
0;
}
else
if(tree-left==NULL
tree-right==NULL)
//终止条件2
两个节点都为空时,找到叶子了,返回
1
{
return
1;
}
else
{
return
Leaf(tree-left)+Leaf(tree-right);
//递归调用
}
}
//主函数不用写了吧,创建一棵树以及调用函数,自己写
关于树c语言基本算法和c++抽象语法树的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。