⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 未命名1.cpp

📁 一个c语言编写的求二叉树叶子结点个数的程序
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct bnode
{
        int data;
        struct bnode  *lchild,*rchild;
                      
        }btree;
btree *insert_node(btree *root,int node)
{      
   btree *newpointer;
   btree *currentpointer;
   btree *parentpointer;
   newpointer=(btree *)malloc(sizeof(btree));
   newpointer->data=node;
   newpointer->lchild=NULL;
   newpointer->rchild=NULL;
   if(root==NULL)
   {                 
    return newpointer;                        
                 }
    else
    {
      currentpointer=root;
      while(currentpointer!=NULL)
      {
         parentpointer=currentpointer;
         if(currentpointer->data>node)                        
       {
         currentpointer=currentpointer->lchild;                           
        
        }
     else
     {
         currentpointer=currentpointer->rchild;  
         
         }
}
if(parentpointer->data>node)
{
  parentpointer->lchild=newpointer;
 
 }
 else
 {
   parentpointer->rchild=newpointer;
    
     } 
  }
  return root;
}

btree *create_btree(int data[],int len)
{
      int i;
      btree *root= NULL;
      for(i=0;i<len;i++)
      {
        root=insert_node(root,data[i]);
                        
                        }
      return root;
      
      
      }

void print_btree(btree *root);
int main(int argc, char *argv[])
{
  btree *root=NULL;
  int value,index,nodelist[20];
  printf("please input data (exit for 0):\n");
  index=0;
  scanf("%d",&value);
  while(value!=0)
  {
     nodelist[index]=value;
     index++;
     scanf("%d",&value);
   }
   root=create_btree(nodelist,index);
   print_btree(root);
   system("pause");	
  return 0;
}



void print_btree(btree *root)
{
 if (root!=NULL)
   {
     printf("%d",root->data);
    
    }  
  if(root->lchild!=NULL||root->rchild!=NULL)
     {
   printf("(") ;
   if (root->rchild!=NULL)
    {
      printf(",");
      print_btree(root->rchild);
    } 
                                           
    printf(")");                                        
                                            
                                            
   }   
   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -