前言
设计BST二叉查找树,方便对二叉树进行结点的增删,采用双向不循环链表实现,每个结点
都需要2个指针,分别指向该结点的左子树(lchild)和右子树(rchild)
附加1:计算所有节点的数量
附加2: 计算所以叶子节点的数量(度为0)
附加3: 计算二叉树的深度
 
More info: BSTree
构造二叉树(BSTree)
构造BST树的结点 BST树中所以结点的数据类型应该是相同的(BSTreeNode)
1 2 3 4 5 6 7
   |  typedef int dataType_t; typedef struct BSTreeNode {     dataType_t Keyval;                      struct BSTreeNode *lchild;           struct BSTreeNode *rchild;       } BSTNode_t;
 
  | 
 
创建一个带根结点的BST树,对BST树的根结点进行初始化(BSTree_Create)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
   | BSTNode_t* BSTree_Create(dataType_t Keyval) {          BSTNode_t *Root = (BSTNode_t*)calloc(1,sizeof (BSTNode_t));     if(Root == NULL)     {         perror("calloc memory for Root is Failed!\n");         exit(-1);            }
           Root->lchild = NULL;     Root->Keyval = Keyval;     Root->rchild = NULL;
           return Root; }
   | 
 
创建新的结点,并对新结点进行初始化(数据域 指针域)(BSTree_NewNode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
   | BSTNode_t *BSTree_NewNode(dataType_t Keyval) {          BSTNode_t *NewNode = (BSTNode_t *) calloc(1, sizeof(BSTNode_t));     if(NewNode == NULL)     {         perror("calloc memory for NewNode is Failed!\n");         return NULL;     }
           NewNode->lchild = NULL;     NewNode->Keyval = Keyval;     NewNode->rchild = NULL;
           return NewNode; }
   | 
 
向BST树中加入结点  根结点的左子树键值比根结点的键值小,根结点的右子树键值比根结点的键值大 体现递归思想(BSTree_InsertNode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
   | bool BSTree_InsertNode(BSTNode_t *Root,dataType_t Keyval) {          BSTNode_t *Proot = Root;
           BSTNode_t *NewNode = BSTree_NewNode(Keyval);
                if(Root == NULL)     {                  Root = NewNode;     }     else       {         while(Proot)         {                          if(NewNode->Keyval == Proot->Keyval)             {                 printf("Can Not Insert,......\n");                 return false;             }             else               {                                  if(NewNode->Keyval < Proot->Keyval)                 {                     if(Proot->lchild == NULL)                     {                         Proot->lchild = NewNode;                         break;                     }                     Proot = Proot->lchild;                 }                 else                   {                     if(Proot->rchild == NULL)                     {                         Proot->rchild = NewNode;                         break;                     }                     Proot = Proot->rchild;                 }             }         }     }     return true; }
   | 
 
前序遍历 根左右 体现递归思想(BSTree_PreOrder)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
   | bool BSTree_PreOrder(BSTNode_t *Root) {          if(Root == NULL)     {         return false;     }               printf("%d  \n",Root->Keyval);               BSTree_PreOrder(Root->lchild);          BSTree_PreOrder(Root->rchild); }
   | 
 
中序遍历 左根右 体现递归思想(BSTree_InOrder)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
   | bool BSTree_InOrder(BSTNode_t *Root) {          if(Root == NULL)     {         return false;     }          BSTree_InOrder(Root->lchild);
           printf("%d  \n",Root->Keyval);
           BSTree_InOrder(Root->rchild); }
   | 
 
后序遍历 左右根 体现递归思想(BSTree_PostOrder)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
   | bool BSTree_PostOrder(BSTNode_t *Root) {          if(Root == NULL)     {         return false;     }          BSTree_PostOrder(Root->lchild);
           BSTree_PostOrder(Root->rchild);
           printf("%d  \n",Root->Keyval); }
   | 
 
附加1:计算所有节点的数量 可采用递归(BSTree_CountNode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   | int BSTree_CountNode(BSTNode_t *Root) {     int n1 = 0;          int n2 = 0;     
           if(Root == NULL)     {         return 0;     }
           n1 = BSTree_CountNode(Root->lchild);     n2 = BSTree_CountNode(Root->rchild);
      return n1 + n2 +1; }
   | 
 
附加2:计算所有叶子节点的数量 可采用递归(BSTree_CountLeafNode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
   | int BSTree_CountLeafNode(BSTNode_t *Root) {     int n1 = 0;          int n2 = 0;     
           if (Root == NULL)     {         return 0;     }
           if(Root->lchild == NULL && Root->rchild == NULL)     {         return 1;     }
           n1 = BSTree_CountLeafNode(Root->lchild);     n2 = BSTree_CountLeafNode(Root->rchild);
      return n1 + n2; }
   | 
 
附加3:计算二叉树的深度 可采用递归(BSTree_GetDepth)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
   | int BSTree_GetDepth(BSTNode_t *Root) {     int n1 = 0;          int n2 = 0;     
           if (Root == NULL)     {         return 0;     }
 
 
 
 
 
 
 
           n1 = BSTree_GetDepth(Root->lchild);     n2 = BSTree_GetDepth(Root->rchild);
      return ( (n1>n2)?n1:n2 ) + 1; }
   | 
 
main主程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
   | int main() {          BSTNode_t *Root = BSTree_Create(10);
           BSTree_InsertNode(Root,5);     BSTree_InsertNode(Root,20);     BSTree_InsertNode(Root,7);     BSTree_InsertNode(Root,12);     BSTree_InsertNode(Root,8);     BSTree_InsertNode(Root,3);     BSTree_InsertNode(Root,25);     BSTree_InsertNode(Root,26);
 
 
 
 
 
      printf("前序遍历为:\n");     BSTree_PreOrder(Root);
      printf("\n中序遍历为:\n");     BSTree_InOrder(Root);
      printf("\n后序遍历为:\n");     BSTree_PostOrder(Root);
      printf("\n总节点数:%d",BSTree_CountNode(Root));
      printf("\n所有叶子节点数:%d",BSTree_CountLeafNode(Root));
      printf("\n二叉树的深度:%d",BSTree_GetDepth(Root));          return 0; }
   | 
 
结果验证
1 2 3 4 5 6 7 8 9 10 11
   | 前序遍历为: 10  5  3  7  8  20  12  25  26 中序遍历为: 3  5  7  8  10  12  20  25  26 后序遍历为: 3  8  7  5  12  26  25  20  10 总节点数:9 所有叶子节点数:4 二叉树的深度:4
  进程已结束,退出代码0
   | 
 
汇总
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
   | #include <stdio.h> #include <stdbool.h> #include <stdlib.h>
 
 
 
 
 
 
 
 
 
  typedef int dataType_t;
 
 
  typedef struct BSTreeNode {     dataType_t Keyval;                      struct BSTreeNode *lchild;           struct BSTreeNode *rchild;       } BSTNode_t;
 
 
  BSTNode_t* BSTree_Create(dataType_t Keyval) {          BSTNode_t *Root = (BSTNode_t*)calloc(1,sizeof (BSTNode_t));     if(Root == NULL)     {         perror("calloc memory for Root is Failed!\n");         exit(-1);            }
           Root->lchild = NULL;     Root->Keyval = Keyval;     Root->rchild = NULL;
           return Root; }
 
 
  BSTNode_t *BSTree_NewNode(dataType_t Keyval) {          BSTNode_t *NewNode = (BSTNode_t *) calloc(1, sizeof(BSTNode_t));     if(NewNode == NULL)     {         perror("calloc memory for NewNode is Failed!\n");         return NULL;     }
           NewNode->lchild = NULL;     NewNode->Keyval = Keyval;     NewNode->rchild = NULL;
           return NewNode; }
 
 
  bool BSTree_InsertNode(BSTNode_t *Root,dataType_t Keyval) {          BSTNode_t *Proot = Root;
           BSTNode_t *NewNode = BSTree_NewNode(Keyval);
                if(Root == NULL)     {                  Root = NewNode;     }     else       {         while(Proot)         {                          if(NewNode->Keyval == Proot->Keyval)             {                 printf("Can Not Insert,......\n");                 return false;             }             else               {                                  if(NewNode->Keyval < Proot->Keyval)                 {                     if(Proot->lchild == NULL)                     {                         Proot->lchild = NewNode;                         break;                     }                     Proot = Proot->lchild;                 }                 else                   {                     if(Proot->rchild == NULL)                     {                         Proot->rchild = NewNode;                         break;                     }                     Proot = Proot->rchild;                 }             }         }     }     return true; }
 
 
  bool BSTree_PreOrder(BSTNode_t *Root) {          if(Root == NULL)     {         return false;     }               printf("%d  ",Root->Keyval);               BSTree_PreOrder(Root->lchild);          BSTree_PreOrder(Root->rchild);
      return true; }
 
 
  bool BSTree_InOrder(BSTNode_t *Root) {          if(Root == NULL)     {         return false;     }          BSTree_InOrder(Root->lchild);
           printf("%d  ",Root->Keyval);
           BSTree_InOrder(Root->rchild);
      return true; }
 
 
  bool BSTree_PostOrder(BSTNode_t *Root) {          if(Root == NULL)     {         return false;     }          BSTree_PostOrder(Root->lchild);
           BSTree_PostOrder(Root->rchild);
           printf("%d  ",Root->Keyval);
      return true; }
 
 
  int BSTree_CountNode(BSTNode_t *Root) {     int n1 = 0;          int n2 = 0;     
           if(Root == NULL)     {         return 0;     }
           n1 = BSTree_CountNode(Root->lchild);     n2 = BSTree_CountNode(Root->rchild);
      return n1 + n2 +1; }
 
 
  int BSTree_CountLeafNode(BSTNode_t *Root) {     int n1 = 0;          int n2 = 0;     
           if (Root == NULL)     {         return 0;     }
           if(Root->lchild == NULL && Root->rchild == NULL)     {         return 1;     }
           n1 = BSTree_CountLeafNode(Root->lchild);     n2 = BSTree_CountLeafNode(Root->rchild);
      return n1 + n2; }
 
 
  int BSTree_GetDepth(BSTNode_t *Root) {     int n1 = 0;          int n2 = 0;     
           if (Root == NULL)     {         return 0;     }
 
 
 
 
 
 
 
           n1 = BSTree_GetDepth(Root->lchild);     n2 = BSTree_GetDepth(Root->rchild);
      return ( (n1>n2)?n1:n2 ) + 1; }
 
 
  int main() {          BSTNode_t *Root = BSTree_Create(10);
           BSTree_InsertNode(Root,5);     BSTree_InsertNode(Root,20);     BSTree_InsertNode(Root,7);     BSTree_InsertNode(Root,12);     BSTree_InsertNode(Root,8);     BSTree_InsertNode(Root,3);     BSTree_InsertNode(Root,25);     BSTree_InsertNode(Root,26);     
 
 
 
 
      printf("前序遍历为:\n");     BSTree_PreOrder(Root);
      printf("\n中序遍历为:\n");     BSTree_InOrder(Root);
      printf("\n后序遍历为:\n");     BSTree_PostOrder(Root);
      printf("\n总节点数:%d",BSTree_CountNode(Root));
      printf("\n所有叶子节点数:%d",BSTree_CountLeafNode(Root));
      printf("\n二叉树的深度:%d",BSTree_GetDepth(Root));          return 0; }
   |