編程葉子節點
⑴ 求統計二叉樹葉子結點數的遞歸演算法
···cpp
由於不知道你的存儲方式,假設你是指針存,用孩子兄弟表示法。
(偽)代碼:
structnode{
data{
...
}val;
node*fchild,*brother;
}
voidgetnum(nodex){
if(x.fchild==nu)ans++;
else{
getnum(*x.fchild);
getnum(*x.brother);
}
}就這樣
⑵ c語言二叉樹的統計葉子節點問題
intPreOrder(BiTreeroot){
inti=0;
if(root==NULL)return0;
if(root->lchild==NULL&&root->rchild==NULL){
i++;
}
printf("%c",root->data);
intj=PreOrder(root->lchild);
intk=PreOrder(root->rchild);
returni+j+k;
}
⑶ 怎麼計算C語言的二叉樹中的葉子節點數
結點的度是指,該結點的子樹的個數,在二叉樹中,不存在度大於2的結點。
計算公式:n0=n2+1
n0
是葉子節點的個數
n2
是度為2的結點的個數
n0=n2+1=5+1=6
故二叉樹有5個度為2的結點,則該二叉樹中的葉子結點數為6。
(3)編程葉子節點擴展閱讀
葉子結點是離散數學中的概念。一棵樹當中沒有子結點(即度為0)的結點稱為葉子結點,簡稱「葉子」。
葉子是指度為0的結點,又稱為終端結點。
葉子結點
就是度為0的結點
就是沒有子結點的結點。
n0:度為0的結點數,n1:度為1的結點
n2:度為2的結點數。
N是總結點
在二叉樹中:
n0=n2+1;
N=n0+n1+n2
參考資料:葉子結點_網路
⑷ 在VB編程中,詳細解釋一下什麼是二叉樹,葉子結點,度的含義和關系。
同意樓上,二叉樹,一種數據類型,看看你的資源管理器就明白了,資源管理器的文件夾相當於二叉樹的葉子,可以展開的加號相當於節點,度就是從根目錄到目標文件夾經過的加號的個數相當於度
c:
|
+——windows
|
+——+user
...|
...+desktop
那麼desktop的度就是4,並且是葉子,windows,user為節點。
二叉樹,顧名思義,只能分兩個叉,一個叉表示和自身同級的文件夾,另一個表示包含的文件夾
一般左分叉為自身同級的文件夾,右為包含的文件夾
⑸ 在VB編程中,詳細解釋一下什麼是二叉樹,葉子結點,度的含義和關系。
二叉樹是一類非常重要的樹形結構,它可以遞歸地定義如下:
二叉樹T是有限個結點的,它或者是空集,或者由一個根結點u以及分別稱為左子樹和右子樹的兩棵互不相交的二叉樹u(1)和u(2)組成。
結點的孩子結點個數即為該結點的度.
度為0的結點叫葉子結點.
處在樹的最頂端(沒有雙親)的結點叫根結點.
⑹ c語言中葉子節點數和節點數有什麼不同
葉子節點指一棵樹上所有終端節點,按照從上向下畫的方式,就是最「下面」的節點。
而節點包含所有節點,也就是除了葉子節點外,還有根節點和中間節點。
以下圖為例:

葉子節點只包括C,D,E三個節點,所以這個樹的葉子節點數為3。
而計算節點數要包括所有節點,即A,B,C,D,E,所以節點數為5。
⑺ 用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##(每輸入一次回車)不能省略#(代表空)
⑻ 數據結構編程: 統計二叉樹中葉子結點的個數。
葉子節點:沒有孩子節點的節點
也就是說,當我們明白了葉子節點的定義後,只需要遍歷一遍二叉樹,把符合這種條件(左孩子節點和右孩子節點都為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語言求樹中的葉子結點數
有從上至下和從下至上兩種方式可以統計樹的節點數。
設葉子節點(度為0的節點)數為x:
從上至下時,度為n的節點有n個子節點,再加上根節點,總結點數量為1+4×1+3×2+2×3+1×4+0×n=21
從下至上時,節點數為度為0~4的所有節點數相加,總節點數量為1+2+3+4+n=10+n
所以有21=10+n,得n=11.
