二叉树

二叉树:

树的一种特殊形式,它的每个节点至多有两个子节点(left node, right node),每个节点的值比它的左子树的所有节点的值都要大,但比它的右子树的所有节点的值都要小,那么这种树中就不存在值相同的节点了。

 

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <time.h>
  4 #include <assert.h>
  5 
  6 typedef struct _binary_tree
  7 {
  8     int             m_nVal;
  9     struct _binary_tree*     m_psLeftNode;
 10     struct _binary_tree*     m_psRightNode;
 11 }binary_tree_s;
 12 
 13 static binary_tree_s* psTree = NULL;
 14 
 15 /**
 16  * @brief    myrand 
 17  *
 18  * @param[in]    range
 19  *
 20  * @return   random number 
 21  */
 22 int myrand(int range)
 23 {
 24     srand((unsigned int)time(NULL));
 25     return (rand()%range);
 26 }
 27 
 28 void insert(int nVal)
 29 {
 30     printf("insert:%d", nVal);
 31 
 32     binary_tree_s* psCurrent = NULL;
 33     binary_tree_s** ppsLink = NULL;
 34 
 35     ppsLink = &psTree;
 36 
 37     
 38     while((psCurrent = *ppsLink) != NULL)
 39     {
 40         if (nVal < psCurrent->m_nVal)
 41         {
 42             printf("left ");
 43             ppsLink = &psCurrent->m_psLeftNode;
 44         }
 45         else
 46         {
 47             if (nVal == psCurrent->m_nVal)
 48             {
 49                 printf("nVal=%d, m_nVal=%d\n", nVal, psCurrent->m_nVal);
 50                 return;
 51             }
 52             printf("right ");
 53             ppsLink = &psCurrent->m_psRightNode;    
 54         }
 55     
 56     }
 57 
 58     psCurrent = (binary_tree_s*)malloc(sizeof(binary_tree_s));
 59     psCurrent->m_nVal = nVal;
 60     psCurrent->m_psLeftNode = NULL;
 61     psCurrent->m_psRightNode = NULL;
 62     *ppsLink = psCurrent;
 63     printf("\n");
 64 }
 65 
 66 binary_tree_s* find(int nVal)
 67 {
 68     binary_tree_s* psCurrent = NULL;
 69 
 70     psCurrent = psTree;
 71 
 72     while((NULL != psCurrent) && (nVal != psCurrent->m_nVal))
 73     {
 74         if (nVal < psCurrent->m_nVal)
 75             psCurrent = psCurrent->m_psLeftNode;
 76         else
 77             psCurrent = psCurrent->m_psRightNode;
 78     }
 79     
 80     if (psCurrent == NULL)
 81         return NULL;
 82     else
 83         return psCurrent;
 84 }
 85 
 86 void myprint(int nVal)
 87 {
 88     printf("val:%d\n", nVal);
 89 }
 90 
 91 static void do_pre_order_traverse(binary_tree_s* psCurrent, void(*callback)(int nVal))
 92 {
 93     if (NULL != psCurrent)
 94     {
 95         callback(psCurrent->m_nVal);
 96         do_pre_order_traverse(psCurrent->m_psLeftNode, callback);
 97         do_pre_order_traverse(psCurrent->m_psRightNode, callback);
 98     }
 99 }
100 
101 void pre_order_traverse(binary_tree_s* psTree)
102 {
103     do_pre_order_traverse(psTree, myprint);
104 }
105 
106 static void do_mid_order_traverse(binary_tree_s* psCurrent, void(*callback)(int nVal))
107 {
108     if (NULL != psCurrent)
109     {
110         do_mid_order_traverse(psCurrent->m_psLeftNode, callback);
111         callback(psCurrent->m_nVal);
112         do_mid_order_traverse(psCurrent->m_psRightNode, callback);
113     }
114 }
115 
116 void mid_order_traverse(binary_tree_s* psTree)
117 {
118     do_mid_order_traverse(psTree, myprint);
119 }
120 
121 
122 static void do_aft_order_traverse(binary_tree_s* psCurrent, void(*callback)(int nVal))
123 {
124     if (NULL != psCurrent)
125     {
126         do_aft_order_traverse(psCurrent->m_psLeftNode, callback);
127         do_aft_order_traverse(psCurrent->m_psRightNode, callback);
128         callback(psCurrent->m_nVal);
129     }
130 }
131 
132 void aft_order_traverse(binary_tree_s* psTree)
133 {
134     do_aft_order_traverse(psTree, myprint);
135 }
136 
137 void destroy_tree(binary_tree_s* psNode)
138 {
139     binary_tree_s** ppsCur;
140 
141     ppsCur = &psNode;
142 
143     if (*ppsCur != NULL)
144     {
145         destroy_tree((*ppsCur)->m_psLeftNode);    
146         destroy_tree((*ppsCur)->m_psRightNode);    
147         free((*ppsCur));
148         ppsCur = NULL;
149     }
150 }
151 
152 void destroy_tree2(binary_tree_s* psNode)
153 {
154 
155     if (psNode != NULL)
156     {
157         destroy_tree(psNode->m_psLeftNode);    
158         destroy_tree(psNode->m_psRightNode);    
159         free(psNode);
160         psNode = NULL;
161     }
162 }
163 
164 int main(int argc, char** argv)
165 {
166     srand((unsigned int)time(NULL));
167 
168     //printf("rand:%d", rand()%100);
169     //printf("rand:%d", rand()%100);
170     //printf("rand:%d", rand()%100);
171     insert(rand()%100);
172     insert(rand()%100);
173     insert(rand()%100);
174     insert(rand()%100);
175     insert(rand()%100);
176     insert(rand()%100);
177     //insert(myrand(100));
178     //insert(myrand(100));
179 
180 #if 0
181     binary_tree_s* psTmp = find(49);
182     if (NULL != psTmp)
183         printf("val:%d\n", psTmp->m_nVal);
184 #endif
185 
186     printf("pre ord:\n");
187     pre_order_traverse(psTree);
188     printf("\nmid ord:\n");
189     mid_order_traverse(psTree);
190     printf("\naft ord:\n");
191     aft_order_traverse(psTree);
192 
193     destroy_tree(psTree);
194 
195     printf("xxxxxxxx\n");
196     //pre_order_traverse(psTree);
197 
198     return 0;
199 }

 

posted @ 2017-02-25 16:33  suonikeyinsu  Views(242)  Comments(0Edit  收藏  举报