• c语言(二叉树的操作)


    #include "stdio.h"
    #include "malloc.h"
    #include "stdlib.h"
    typedef struct BTNode
    {
        int data;
        struct BTNode *Lchild,*Rchild;
    }BTree;
    //初始化
    BTree * Ini_BTNode()
    {
        BTree *bt ;
        int a;
        bt=(BTree *)malloc(sizeof(BTree));
        printf("输入根节点:(0表示空树)
    ");
        scanf("%d",&a);
        if(a==0)
        {
            printf("这是空树!");
            exit(0);
        }
        bt->data=a; 
        bt->Lchild=NULL;  //左子树节点 
        bt->Rchild=NULL;  //右子树节点 
        return bt;
    } 
    creat_BiTree(BTree *bt)//输入左子树 
    {
           
          int a;
          BTree *Node;
          printf("请输入%d节点的左孩子(0为空)
    ",bt->data) ;
          scanf("%d",&a);
          if(a!=0)
          {
                  Node=(BTree*)malloc(sizeof(BTree));
                  Node->data=a;
                  Node->Lchild=NULL;
                  Node->Rchild=NULL;
                  bt->Lchild=Node;
                  creat_BiTree(bt->Lchild);
                    
          } 
          printf("请输入%d节点的右孩子(0为空)
    ",bt->data) ; //输入右子树
          scanf("%d",&a);
          if(a!=0)
          {
                  Node=(BTree *)malloc(sizeof(BTree));
                  Node->data=a;
                  Node->Lchild=NULL;
                  Node->Rchild=NULL;
                  bt->Rchild =Node;
                  creat_BiTree(bt->Rchild);
                    
          }
          
          return 0;
          
          
    }
    void PerOrderTraverse(BTree *bt) //先序遍历
    {
        if(bt!=NULL)
       { printf("%d -->",bt->data);
         PerOrderTraverse(bt->Lchild); 
         PerOrderTraverse(bt->Rchild);            
       }
    }
    void InOrderTraverse(BTree *bt) //中序遍历 
    {
        if(bt!=NULL)
       { 
         InOrderTraverse(bt->Lchild);
         printf("%d -->",bt->data); 
         InOrderTraverse(bt->Rchild);            
       }
    }
    void PostOrderTraverse(BTree *bt) //后序遍历 
    {
        if(bt!=NULL)
       { 
         PostOrderTraverse(bt->Lchild);
         PostOrderTraverse(bt->Rchild); 
         printf("%d -->",bt->data);            
       }
    }      
    
    
     
    main()
    {
        BTree  *bt;
        int a;
        printf("		****************  二叉树操作  ****************
    
    ");
        bt=Ini_BTNode();creat_BiTree(bt);
        printf("以%d为根的树创建成功!
    ",bt->data);
        system("pause"); 
        system("cls");
        while(1)
        {
    
            system("cls");
            printf("		1. 先序遍历
    ");
            printf("		2. 中序遍历
    ");
            printf("		3. 后序遍历
    ");
            printf("		4. 退出程序
    ");
            printf("	请选择:");
            scanf("%d",&a);
            switch(a)
            {
             case 1:printf("先序遍历
    ");
                    PerOrderTraverse(bt);   
                    printf("
    ");
                    system("pause"); break;
             case 2:printf("中序遍历
    ");
                     InOrderTraverse(bt);
                     printf("
    ");
                    system("pause"); break;
                 
    
             case 3:printf("后序遍历
    ");
                     PostOrderTraverse(bt);
                     printf("
    ");
                    system("pause"); break;
             case 4:exit(0); 
             default:printf("输入错误");
            }
        
         } 
      system("pause");  
    }

     

  • 相关阅读:
    ftp
    字符串
    A函数跨区域
    树状结构
    easyUI的基础布局easyui-accordion
    easyUI的基础布局
    sql server 下载安装标记
    (办公)记事本_Linux常用的目录命令
    (办公)记事本_Linux目录
    (办公)记事本_购买域名
  • 原文地址:https://www.cnblogs.com/doublekai/p/6755483.html
Copyright © 2020-2023  润新知