当前位置:首页 » 编程软件 » 叶子的编程

叶子的编程

发布时间: 2023-01-22 01:38:25

A. 汽轮机叶片的数控编程是用什么编的

我们一般是用PROE
3.0编程,叶可以用UG。以及楼上说的思
美创

其实还有其他很多软件都能编程。比如
powermill
hypermill
等等。都能用于叶片加工。

B. 数据结构编程: 统计二叉树中叶子结点的个数。

叶子节点:没有孩子节点的节点

也就是说,当我们明白了叶子节点的定义后,只需要遍历一遍二叉树,把符合这种条件(左孩子节点和右孩子节点都为NULL的节点)的节点统计出来就可以了。

于是,实际上这个问题也就转化成了如何遍历二叉树?很显然,遍历二叉树是可以有多种方式的,如:前序遍历(递归/非递归)、中序遍历(递归/非递归)、后序遍历(递归/非递归)、层次遍历等等。

下面我将给出使用递归前序遍历以及层次遍历两种思路实现的求解叶子节点的示例代码吧,仅供参考。其他几种实现方式思路类似,可自行尝试。示例代码如下:

java">packagecn.zifangsky.tree.questions;

importorg.junit.Test;

importcn.zifangsky.queue.LinkQueue;
importcn.zifangsky.tree.BinaryTreeNode;

/**
*求二叉树中叶子节点的个数
*@authorAdministrator
*
*/
publicclassQuestion2{

/**
*通过递归前序遍历获取叶子节点个数
*@paramroot
*@return
*/
(BinaryTreeNoderoot){
if(root==null){
return0;
}else{
if(root.getLeft()==null&&root.getRight()==null){//叶子节点
return1;
}else{
(root.getLeft())+getNumberOfLeavesByPreOrder(root.getRight());
}
}

}


/**
*使用层次遍历获取二叉树叶子节点个数
*
*@时间复杂度O(n)
*@paramroot
*/
(BinaryTreeNoderoot){
intcount=0;//叶子节点总数
LinkQueue<BinaryTreeNode>queue=newLinkQueue<>();
if(root!=null){
queue.enQueue(root);
}

while(!queue.isEmpty()){
BinaryTreeNodetemp=(BinaryTreeNode)queue.deQueue();
//叶子节点:左孩子节点和右孩子节点都为NULL的节点
if(temp.getLeft()==null&&temp.getRight()==null){
count++;
}else{
if(temp.getLeft()!=null){
queue.enQueue(temp.getLeft());
}
if(temp.getRight()!=null){
queue.enQueue(temp.getRight());
}
}
}
returncount;
}


/**
*测试用例
*/
@Test
publicvoidtestMethods(){
/**
*使用队列构造一个供测试使用的二叉树
*1
*23
*4567
*89
*/
LinkQueue<BinaryTreeNode>queue=newLinkQueue<BinaryTreeNode>();
BinaryTreeNoderoot=newBinaryTreeNode(1);//根节点

queue.enQueue(root);
BinaryTreeNodetemp=null;
for(inti=2;i<10;i=i+2){
BinaryTreeNodetmpNode1=newBinaryTreeNode(i);
BinaryTreeNodetmpNode2=newBinaryTreeNode(i+1);

temp=(BinaryTreeNode)queue.deQueue();

temp.setLeft(tmpNode1);
temp.setRight(tmpNode2);

if(i!=4)
queue.enQueue(tmpNode1);
queue.enQueue(tmpNode2);
}

System.out.println("叶子节点个数是:"+getNumberOfLeavesByPreOrder(root));
System.out.println("叶子节点个数是:"+getNumberOfLeavesByQueue(root));

}

}

测试代码输出如下:

叶子节点个数是:5
叶子节点个数是:5


附:

二叉树节点BinaryTreeNode的定义:

packagecn.zifangsky.tree;

publicclassBinaryTreeNode{
privateintdata;//数据
privateBinaryTreeNodeleft;//左孩子节点
privateBinaryTreeNoderight;//右孩子节点

publicBinaryTreeNode(intdata){
this.data=data;
}

publicBinaryTreeNode(intdata,BinaryTreeNodeleft,BinaryTreeNoderight){
this.data=data;
this.left=left;
this.right=right;
}

publicintgetData(){
returndata;
}

publicvoidsetData(intdata){
this.data=data;
}

publicBinaryTreeNodegetLeft(){
returnleft;
}

publicvoidsetLeft(BinaryTreeNodeleft){
this.left=left;
}

publicBinaryTreeNodegetRight(){
returnright;
}

publicvoidsetRight(BinaryTreeNoderight){
this.right=right;
}

}

队列LinkQueue的定义:

packagecn.zifangsky.queue;

importcn.zifangsky.linkedlist.SinglyNode;

/**
*基于单链表实现的队列
*@authorzifangsky
*@param<K>
*/
publicclassLinkQueue<KextendsObject>{
privateSinglyNode<?>frontNode;//队首节点
privateSinglyNode<?>rearNode;//队尾节点

publicLinkQueue(){
frontNode=null;
rearNode=null;
}

/**
*返回队列是否为空
*@时间复杂度O(1)
*@return
*/
publicbooleanisEmpty(){
return(frontNode==null);
}

/**
*返回存储在队列的元素个数
*@时间复杂度O(n)
*@return
*/
publicintsize(){
intlength=0;
SinglyNode<?>currentNode=frontNode;
while(currentNode!=null){
length++;
currentNode=currentNode.getNext();
}

returnlength;
}

/**
*入队:在链表表尾插入数据
*@时间复杂度O(1)
*@paramdata
*/
publicvoidenQueue(Kdata){
SinglyNode<K>newNode=newSinglyNode<K>(data);

if(rearNode!=null){
rearNode.setNext(newNode);
}

rearNode=newNode;

if(frontNode==null){
frontNode=rearNode;
}
}

/**
*出队:删除表头节点
*@时间复杂度O(1)
*@return
*/
publicObjectdeQueue(){
if(isEmpty()){
thrownewRuntimeException("QueueEmpty!");
}else{
Objectresult=frontNode.getData();

if(frontNode==rearNode){
frontNode=null;
rearNode=null;
}else{
frontNode=frontNode.getNext();
}

returnresult;
}
}

}

单链表节点SinglyNode的定义:

packagecn.zifangsky.linkedlist;

/**
*单链表的定义
*@authorzifangsky
*@param<K>
*/
publicclassSinglyNode<KextendsObject>{
privateKdata;//数据
privateSinglyNode<?>next;//该节点的下个节点

publicSinglyNode(Kdata){
this.data=data;
}

publicSinglyNode(Kdata,SinglyNode<?>next){
this.data=data;
this.next=next;
}

publicKgetData(){
returndata;
}

publicvoidsetData(Kdata){
this.data=data;
}

publicSinglyNode<?>getNext(){
returnnext;
}

publicvoidsetNext(SinglyNode<?>next){
this.next=next;
}

@Override
publicStringtoString(){
return"SinglyNode[data="+data+"]";
}

}

C. 用C语言编程,以二叉链表为存储结构,并以先跟序列输入创建二叉树,求叶子结点树

#include<stdio.h>
#include<stdlib.h>
typedef struct bitnode
{
char data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;
bitree create_tree() //先序创建
{
char a;bitree root;
scanf("%c",&a);
fflush(stdin);
if(a=='#')return NULL;
else
{
root=(bitnode *)malloc(sizeof(bitnode));
root->data=a;
root->lchild=create_tree();
root->rchild=create_tree();
}
return root;
}

void inorder(bitree root)//中根遍历
{
bitree s[100];
int top=0;
while(root||top)
{
while(root)
{
s[++top]=root;root=root->lchild;
}
if(top)
{
putchar(s[top]->data);
root=s[top--]->rchild;
}
}
}
int leafcount(bitree root)//计算叶子节点个数
{
if(!root)return 0;
else
{
if(!(root->lchild)&&!(root->rchild))return 1;
else return leafcount(root->lchild)+leafcount(root->rchild);
}
}
void main()
{
bitree root=NULL;
root=create_tree();//输入序列为先序遍历序列,#代表空
printf("中序遍历结果为:\n");
inorder(root);printf("\n");
printf("叶子节点个数为%d\n",leafcount(root));
printf("\n");
}

//输入先序序列例如ab##c##(每输入一次回车)不能省略#(代表空)

D. 在VB编程中,详细解释一下什么是二叉树,叶子结点,度的含义和关系。

二叉树是一类非常重要的树形结构,它可以递归地定义如下:
二叉树T是有限个结点的,它或者是空集,或者由一个根结点u以及分别称为左子树和右子树的两棵互不相交的二叉树u(1)和u(2)组成。
结点的孩子结点个数即为该结点的度.
度为0的结点叫叶子结点.
处在树的最顶端(没有双亲)的结点叫根结点.

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:710
制作脚本网站 发布:2025-10-20 08:17:34 浏览:972
python中的init方法 发布:2025-10-20 08:17:33 浏览:681
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:833
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:741
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1081
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:312
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:192
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:879
python股票数据获取 发布:2025-10-20 07:39:44 浏览:837