二叉树的基本操作(C语言实现)

  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. //二叉树的节点定义  
  4. typedef struct TreeNode  
  5. {  
  6.     char ch;                  //数据域  
  7.     struct TreeNode *lchild;  //左孩子  
  8.     struct TreeNode *rchild;  //右孩子  
  9. }BTNode,*PBTNode;  
  10.   
  11. //先序构造二叉树  
  12. void createBTree(PBTNode *root)  
  13. {  
  14.     char ch;  
  15.     ch=getchar();  
  16.     if(ch=='#')  
  17.         *root=NULL;  
  18.     else{  
  19.         *root=(PBTNode)malloc(sizeof(BTNode));  
  20.         (*root)->ch=ch;  
  21.         (*root)->lchild=NULL;  
  22.         (*root)->rchild=NULL;  
  23.         createBTree(&(*root)->lchild);  
  24.         createBTree(&(*root)->rchild);  
  25.     }  
  26. }  
  27.   
  28. //先序遍历二叉树  
  29. void preOrder(PBTNode root)  
  30. {  
  31.     if(root==NULL)  
  32.         return;  
  33.     printf("%-3c",root->ch);  
  34.     preOrder(root->lchild);  
  35.     preOrder(root->rchild);  
  36. }  
  37.   
  38. //中序遍历二叉树  
  39. void inOrder(PBTNode root)  
  40. {  
  41.     if(root==NULL)  
  42.         return;  
  43.     preOrder(root->lchild);  
  44.     printf("%-3c",root->ch);  
  45.     preOrder(root->rchild);  
  46. }  
  47.   
  48. //后序遍历二叉树  
  49. void postOrder(PBTNode root)  
  50. {  
  51.     if(root==NULL)  
  52.         return;  
  53.     preOrder(root->lchild);  
  54.     preOrder(root->rchild);  
  55.     printf("%-3c",root->ch);  
  56. }  
  57.   
  58. //输出叶子结点  
  59. void displyLeaf(PBTNode root)  
  60. {  
  61.     if(root==NULL)  
  62.         return;  
  63.     if(root->lchild==NULL&&root->rchild==NULL)  
  64.         printf("%-3c",root->ch);  
  65.     else{  
  66.         displyLeaf(root->lchild);  
  67.         displyLeaf(root->rchild);  
  68.     }  
  69. }  
  70.   
  71. //左结点插入  
  72. void inseartLeftNode(PBTNode root,char ch)  
  73. {  
  74.     PBTNode p,newNode;  
  75.     if(root==NULL)  
  76.         return;  
  77.     p=root->lchild;  
  78.     newNode=(PBTNode)malloc(sizeof(BTNode));  
  79.     newNode->ch=ch;  
  80.     newNode->rchild=NULL;  
  81.     newNode->lchild=p;  
  82.     root->lchild=newNode;  
  83. }  
  84.   
  85. //右结点插入  
  86. void inseartRightNode(PBTNode root,char ch)  
  87. {  
  88.     PBTNode p,newNode;  
  89.     if(root==NULL)  
  90.         return;  
  91.     p=root->rchild;  
  92.     newNode=(PBTNode)malloc(sizeof(BTNode));  
  93.     newNode->ch=ch;  
  94.     newNode->lchild=NULL;  
  95.     newNode->rchild=p;  
  96.     root->rchild=newNode;  
  97. }  
  98.   
  99. //销毁一颗二叉树  
  100. void clear(PBTNode* root)  
  101. {  
  102.     PBTNode pl,pr;  
  103.     if(*root==NULL)  
  104.         return;  
  105.     pl=(*root)->lchild;  
  106.     pr=(*root)->rchild;  
  107.     (*root)->lchild=NULL;  
  108.     (*root)->rchild=NULL;  
  109.     free((*root));  
  110.     *root=NULL;  
  111.     clear(&pl);  
  112.     clear(&pr);  
  113. }  
  114.   
  115. //删除左子树  
  116. void deleteLeftTree(PBTNode root)  
  117. {  
  118.     if(root==NULL)  
  119.         return;  
  120.     clear(&root->lchild);  
  121.     root->lchild=NULL;  
  122. }  
  123.   
  124. //删除右子树  
  125. void deleteRightTree(PBTNode root)  
  126. {  
  127.     if(root==NULL)  
  128.         return;  
  129.     clear(&root->rchild);  
  130.     root->rchild=NULL;  
  131. }  
  132.   
  133. //查找结点  
  134. PBTNode   search(PBTNode root,char ch)  
  135. {  
  136.     PBTNode p;  
  137.     if(root==NULL)  
  138.         return NULL;  
  139.     if(root->ch==ch)  
  140.         return root;  
  141.     else{  
  142.         if((p=search(root->lchild,ch))!=NULL)  
  143.             return p;  
  144.         else  
  145.             return  search(root->rchild,ch);    
  146.     }  
  147. }  
  148.   
  149. //求二叉树的高度  
  150. int BTreeHeight(PBTNode root)  
  151. {  
  152.     int lchildHeight,rchildHeight;  
  153.     if(root==NULL)  
  154.         return 0;  
  155.     lchildHeight=BTreeHeight(root->lchild);  
  156.     rchildHeight=BTreeHeight(root->rchild);  
  157.     return (lchildHeight>rchildHeight)?(1+lchildHeight):(1+rchildHeight);  
  158. }  
  159.   
  160. //求叶子结点的个数  
  161. int countLeaf(PBTNode root)  
  162. {  
  163.     if(root==NULL)  
  164.         return 0;    
  165.     if(root->lchild==NULL&&root->rchild==NULL)  
  166.         return 1;  
  167.     else{  
  168.         return countLeaf(root->lchild)+countLeaf(root->rchild);  
  169.     }  
  170. }  
  171.   
  172. //求所有结点的个数  
  173. int countAll(PBTNode root)  
  174. {  
  175.     if(root==NULL)  
  176.         return 0;  
  177.     return countAll(root->lchild)+countAll(root->rchild)+1;  
  178. }  
  179.   
  180. //复制二叉树  
  181. PBTNode copyBTree(PBTNode root)  
  182. {  
  183.     PBTNode p,lchild,rchild;  
  184.     if(root==NULL)  
  185.         return NULL;  
  186.     lchild=copyBTree(root->lchild);  
  187.     rchild=copyBTree(root->rchild);  
  188.     p=(PBTNode)malloc(sizeof(BTNode));  
  189.     p->ch=root->ch;  
  190.     p->lchild=lchild;  
  191.     p->rchild=rchild;  
  192.     return p;  

  193. int main()
    {
    BTNode *T;
    int depth;
    int leafCount = 0;
    createBTree(&T);


    printf("先序遍历二叉树:");
    preOrder(T);
    printf("\n");


    printf("中序遍历二叉树:");
    inOrder(T);
    printf("\n");


    printf("后序遍历二叉树:");
    postOrder(T);
    printf("\n");


    depth = BTreeHeight(T);
    printf("树的深度为:%d\n", depth);


    leafCount = countLeaf(T);
    printf("叶子节点个数:%d\n",leafCount);




    getchar();
    return 0;

猜你喜欢

转载自blog.csdn.net/zjy900507/article/details/80616136