zoukankan      html  css  js  c++  java
  • 数据结构 树的建立与遍历

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 typedef char ElemType;
     5 
     6 typedef struct node{
     7     ElemType data;
     8     struct node *lson;
     9     struct node *rson;
    10 }Bynode,*Bytree;
    11 
    12 Bytree CreateBytree()//树的建立
    13 {
    14     char ch;
    15     Bytree t;
    16     ch = getchar(); 
    17     if ( ch == '1')
    18         t = NULL;
    19     else 
    20     { 
    21         t = (Bytree)malloc(sizeof(Bynode));
    22         t->data = ch;
    23         t->lson = CreateBytree();
    24         t->rson = CreateBytree();
    25      }
    26     return t;
    27 }
    28 
    29 void pre(Bytree t)//先序遍历
    30 {
    31     if(t)
    32     {
    33         putchar(t->data);
    34         pre(t->lson);
    35         pre(t->rson);
    36     }
    37 }
    38 
    39 void in(Bytree t)//中序遍历
    40 {
    41     if(t)
    42     {
    43         in(t->lson);
    44         putchar(t->data);
    45         in(t->rson);
    46     }
    47 }
    48 
    49 void post(Bytree t)//后序遍历
    50 {
    51     if(t)
    52     {
    53         post(t->lson);
    54         post(t->rson);
    55         putchar(t->data);
    56     }
    57 }
    58 
    59 void main()
    60 {
    61     Bytree t;
    62     t=CreateBytree();
    63     printf("先序遍历:");
    64     pre(t);
    65     putchar('
    ');
    66     printf("中序遍历:");
    67     in(t);
    68     putchar('
    ');
    69     printf("后续遍历:");
    70     post(t);
    71     putchar('
    ');
    72 }
  • 相关阅读:
    倒水问题(BFS)
    小程序整理
    微信小程序--录音
    mpvue
    hbuilder 打包 vueAPP
    react rem
    react 关闭eslint 配置
    react axios 配置
    react 路由之react-router-dom
    react mobx 装饰器语法配置
  • 原文地址:https://www.cnblogs.com/weiyuan/p/6040311.html
Copyright © 2011-2022 走看看