zoukankan      html  css  js  c++  java
  • 求树的高度的递归和非递归方法

    我觉得树的知识大家也不陌生,这里直接上代码得了.

    非递归的方法是采用了中序遍历的非递归方式:

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 #define   MAX   100
      5 
      6 #define   ARRAY_SIZE(array, type)   (sizeof(array) / sizeof(type))
      7 
      8 typedef struct node {
      9     int  value;
     10     struct node* left;
     11     struct node* right;
     12     struct node* parent;
     13 } node;
     14 
     15 typedef struct t_node {
     16     node*  p;
     17     int    height;
     18 } t_node;
     19 
     20 void insert_node(node** root, int value) {
     21     node* p = (node*)malloc(sizeof(node));
     22     if (NULL == p) {
     23         fprintf(stderr, "malloc memory failed!
    ");
     24         exit(1);
     25     }
     26     p->value = value;
     27     p->left = p->right = p->parent = NULL;
     28     
     29     if (NULL == *root) {
     30         *root = p;
     31     }
     32     else {
     33         node* q = *root;
     34         node* r = NULL;
     35 
     36         while (q != NULL) {
     37             r = q;
     38             if (q->value > p->value) {
     39                 q = q->left;
     40             }
     41             else if (q->value < p->value) {
     42                 q = q->right;
     43             }
     44             else {
     45                 return;
     46             }
     47        }
     48 
     49         p->parent = r;
     50 
     51         if (r->value > p->value) {
     52             r->left = p;
     53         }
     54         else {
     55             r->right = p;
     56         }
     57     }
     58 }
     59 
     60 void pre_order(node* root) {
     61     if (root != NULL) {
     62         fprintf(stderr, "%d
    ", root->value);
     63         pre_order(root->left);
     64         pre_order(root->right);
     65     }
     66 }
     67 
     68 int  get_height(node* root) {
     69     t_node  stack[MAX];
     70     int     pos = -1;
     71     int     cur = 0;
     72     int     max = 0;
     73 
     74     while (root != NULL || pos != -1) {
     75         if (root == NULL) {
     76             root = stack[pos].p;
     77             cur = stack[pos].height;
     78             --pos;
     79             root = root->right;
     80         }
     81         else {
     82             ++pos;
     83             ++cur;
     84             stack[pos].p = root;
     85             stack[pos].height = cur;
     86             root = root->left;
     87         }
     88 
     89         if (cur > max) {
     90             max = cur;
     91         }
     92     }
     93 
     94     return  max;
     95 }
     96 
     97 int  main(void) {
     98     int  array[] = {5, 8, 3, 10, 2, 1, 0, 32, 321, 222, 11};
     99     int i = 0;
    100     node* root = NULL;
    101 
    102     for (; i < ARRAY_SIZE(array, int); ++i) {
    103         insert_node(&root, array[i]);
    104     }
    105 
    106     int height = get_height(root);
    107 
    108     fprintf(stderr, "%d
    ", height);
    109 
    110 
    111     return  0;
    112 }

    递归方法采用的是前序遍历的递归方式:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 #define  ARRAY_SIZE(array, type)    (sizeof(array) / sizeof(type))
     5 
     6 typedef struct node {
     7     int  value;
     8     struct node* left;
     9     struct node* right;
    10     struct node* parent;
    11 } node;
    12 
    13 void  insert_node(node** root, int value) {
    14     node* p = (node*)malloc(sizeof(node));
    15     if (NULL == p) {
    16         fprintf(stderr, "malloc memory failed!
    ");
    17         exit(1);
    18     }
    19     p->value = value;
    20     p->left = p->right = p->parent = NULL;
    21 
    22     if (*root == NULL) {
    23         *root = p;
    24     }
    25     else {
    26         node* q = *root;
    27         node* r = NULL;
    28         while (q != NULL) {
    29             r = q;
    30             if (q->value > p->value) {
    31                 q = q->left;
    32             }
    33             else if (q->value < p->value) {
    34                 q = q->right;
    35             }
    36             else {
    37                 return;
    38             }
    39         }
    40 
    41         p->parent = r;
    42         if (r->value > p->value) {
    43             r->left = p;
    44         }
    45         else {
    46             r->right = p;
    47         }
    48     }
    49 }
    50 
    51 void pre_order(node* root) {
    52     if (root != NULL) {
    53         fprintf(stderr, "%d
    ", root->value);
    54         pre_order(root->left);
    55         pre_order(root->right);
    56     }
    57 }
    58 
    59 void get_height(node* root, int now, int* max) {
    60     if (root != NULL) {
    61         ++now;
    62         if (now > *max) {
    63             *max = now;
    64         }
    65 
    66         get_height(root->left, now, max);
    67         get_height(root->right, now, max);
    68     }
    69 }
    70 
    71 int  main(void) {
    72    int  array[] = {5, 3, 4, 1, 2, 0, -1, -2,  9, 21, 7, 6, 5, 22, 23, 24, 25};
    73 
    74    node*  root = NULL;
    75    int i = 0;
    76    for (; i < ARRAY_SIZE(array, int); ++i) {
    77        insert_node(&root, array[i]);
    78    }
    79 
    80    pre_order(root);
    81 
    82    int  max = 0;
    83    int  now = 0;
    84 
    85    get_height(root, now, &max);
    86 
    87    fprintf(stderr, "%d
    ", max);
    88 
    89    return  0;
    90 }
  • 相关阅读:
    开启ecstore隐藏的功能
    对接ECOS框架(含ecstore、ocs、erp等产品)的方法【其他系统请求ecos的api接口】
    shopex 网店系统基于云登录系统的信任登录设置
    怎样获取机器码
    windows 无法连接远程桌面
    ecos框架中data/ 目录下img*文件数量过多的问题
    mac 下brew解决php安装问题
    20140708 总结
    20140705 总结
    bzoj 2751
  • 原文地址:https://www.cnblogs.com/coder-zhang/p/3751528.html
Copyright © 2011-2022 走看看