zoukankan      html  css  js  c++  java
  • C之:微代码——二叉树

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <ctype.h>
      4 
    7 typedef struct  8 {
      9     long item;
     10     int count;
     11     Node* pLeft;
     12     Node* pRight;
     13 } Node;
     14 
     15 Node* creat_node(long);
     16 Node* add_node(long, Node*);
     17 void list_nodes(Node*);
     18 void free_nodes(Node*);
     19 
     20 int main(void)
     21 {
     22     long newvalue = 0;
     23     Node* pRoot = NULL;
     24     char answer = 'n';
     25     do
     26     {
     27         printf("Enter the node value: ");
     28         scanf("%ld", &newvalue);
     29         getchar();
     30         if(pRoot == NULL)
     31         {
     32             pRoot = creat_node(newvalue);
     33         }
     34         else
     35         {
     36             add_node(newvalue, pRoot);
     37         }
     38         printf("Do you want to enter another (y/n)? ");
     39         scanf("%c", &answer);
     40     } while(tolower(answer) == 'y');
     41 
     42     printf("The values in ascending sequence are: \n");
     43     list_nodes(pRoot);
     44     free_nodes(pRoot);
     45 
     46     return 0;
     47 }
     48 
     49 Node* creat_node(long value)
     50 {
     51     Node* pNode = (Node*)malloc(sizeof(Node));
     52     pNode->item = value;
     53     pNode->count = 1;
     54     pNode->pLeft = pNode->pRight = NULL;
     55     return pNode;
     56 }
     57 
     58 Node* add_node(long value, Node* pNode)
     59 {
     60     if(pNode == NULL)
     61     {
     62         return creat_node(value);
     63     }
     64 
     65     if(value == pNode->item)
     66     {
     67         ++pNode->count;
     68         return pNode;
     69     }
     70 
     71     else if(value < pNode->item)
     72     {
     73         if(pNode->pLeft == NULL)
     74         {
     75             pNode->pLeft = creat_node(value);
     76             return pNode->pLeft;
     77         }
     78         else
     79         {
     80             return add_node(value, pNode->pLeft);
     81         }
     82     }
     83     else
     84     {
     85         if(pNode->pRight == NULL)
     86         {
     87             pNode->pRight = creat_node(value);
     88             return pNode->pRight;
     89         }
     90         else
     91         {
     92             return add_node(value, pNode->pRight);
     93         }
     94     }
     95 }
     96 
     97 void list_nodes(Node* pNode)
     98 {
     99     if(pNode->pLeft != NULL)
    100     {
    101         list_nodes(pNode->pLeft);    
    102     }
    103     
    104     printf("%10d x %10ld\n", pNode->count, pNode->item);
    105 
    106     if(pNode->pRight != NULL)
    107     {
    108         list_nodes(pNode->pRight);
    109     }
    110 }
    111 
    112 void free_nodes(Node* pNode)
    113 {
    114     if(pNode == NULL)
    115     {
    116         return;
    117     }
    118 
    119     if(pNode->pLeft != NULL)
    120     {
    121         free_nodes(pNode->pLeft);
    122     }
    123 
    124     if(pNode->pRight != NULL)
    125     {
    126         free_nodes(pNode->pRight);
    127     }
    128 
    129     free(pNode);
    130 }
  • 相关阅读:
    Android的数据存储
    Servlet第一天
    JavaScript高级程序设计读书笔记(3)
    Interesting Papers on Face Recognition
    Researchers Study Ear Biometrics
    IIS 发生意外错误 0x8ffe2740
    Father of fractal geometry, Benoit Mandelbrot has passed away
    Computer vision scientist David Mumford wins National Medal of Science
    Pattern Recognition Review Papers
    盒模型bug的解决方法
  • 原文地址:https://www.cnblogs.com/hadex/p/5962777.html
Copyright © 2011-2022 走看看