🌺先画图,后再实战代码
本章需要掌握:
- 1、树、二叉树、满二叉树、完全二叉树
 
- 2、层次建树、先序遍历、中序遍历、后序遍历、层序遍历
 
- 3、真题
 
 
1、树(Tree)
树是一种逻辑结构,也是一种分层结构。n(n>=0)个结点的有限集。当n=0是空树;当n>0时①有且仅有一个根节点②n>1其余结点可分为m(m>0)个互不相交的有限集T1,T2,…,Tm,每个集合又是一颗二叉树,称为跟的子树。
2、二叉树(BiTree)
满二叉树(是除最后一层无任何子节点外,每一层上的所有结点都有两个子结点的二叉树。)
完全二叉树(除最后一 层外,其它各层的结点数都达到最大个数。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   | 
 
 
  #ifndef INC_1_TREE_FUNCTION_H #define INC_1_TREE_FUNCTION_H #include <stdio.h> #include <stdlib.h>
  typedef char BiElemType; typedef struct BiTNode{     BiElemType c;     struct BiTNode *lchild;     struct BiTNode *rchild; }BiTNode,*BiTree;
 
  typedef struct tag{     BiTree p;     struct tag *pnext; }tag_t,*ptag_t; #endif 
 
  | 
 
3、层次建树
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
   | #include "function.h"
  int main() {     BiTree pnew;     BiTree tree=NULL;     char c;     ptag_t phead=NULL,ptail=NULL,listpnew=NULL,pcur;          while(scanf("%c",&c))     {         if(c=='\n')         {             break;         }                  pnew= (BiTree)calloc(1,sizeof(BiTNode));         pnew->c=c;         listpnew= (ptag_t)calloc(1,sizeof(tag_t));         listpnew->p=pnew;                  if(NULL==tree)         {             tree=pnew;             phead=listpnew;             ptail=listpnew;             pcur=listpnew;         }else{                          ptail->pnext=listpnew;             ptail=listpnew;                          if(NULL==pcur->p->lchild)             {                 pcur->p->lchild=pnew;             }else if(NULL==pcur->p->rchild)             {                 pcur->p->rchild=pnew;                 pcur=pcur->pnext;             }         }     }     return 0; }
   | 
 
4、二叉树的前序、中序、后序、层次遍历
一些定义
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
   | 
 
 
 
  #ifndef INC_1_TREE_FUNCTION_H #define INC_1_TREE_FUNCTION_H #include <stdio.h> #include <stdlib.h>
  typedef char BiElemType; typedef struct BiTNode{     BiElemType c;     struct BiTNode *lchild;     struct BiTNode *rchild; }BiTNode,*BiTree;
 
  typedef struct tag{     BiTree p;     struct tag *pnext; }tag_t,*ptag_t;
 
  typedef BiTree ElemType; typedef struct LinkNode{     ElemType data;     struct LinkNode *next; }LinkNode; typedef struct{     LinkNode *front,*rear; }LinkQueue; void InitQueue(LinkQueue &Q); bool IsEmpty(LinkQueue Q); void EnQueue(LinkQueue &Q,ElemType x); bool DeQueue(LinkQueue &Q,ElemType &x);
  #endif 
 
  | 
 
链队列 操作代码
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
   | 
 
  #include "function.h"
 
  void InitQueue(LinkQueue &Q) {     Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));     Q.front->next=NULL; }
  bool IsEmpty(LinkQueue Q) {     return Q.rear==Q.front; }
  void EnQueue(LinkQueue &Q,ElemType x) {     LinkNode *pnew=(LinkNode*)malloc(sizeof(LinkNode));     pnew->data=x;     pnew->next=NULL;     Q.rear->next=pnew;     Q.rear=pnew; }
  bool DeQueue(LinkQueue &Q,ElemType &x) {     if(Q.rear==Q.front)     {         return false;     }     LinkNode* q=Q.front->next;     x=q->data;     Q.front->next=q->next;     if(Q.rear==q)     {         Q.rear=Q.front;     }     free(q);     return true; }
 
 
  | 
 
前序遍历、中序遍历、后序遍历、层次遍历
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
   |  #include "function.h"
 
  void PreOrder(BiTree p) {     if(p!=NULL)     {         printf("%c", p->c);         PreOrder(p->lchild);         PreOrder(p->rchild);     } }
  void InOrder(BiTree p) {     if(p!=NULL)     {         InOrder(p->lchild);         printf("%c", p->c);         InOrder(p->rchild);     } }
  void PostOrder(BiTree p) {     if(p!=NULL)     {         PostOrder(p->lchild);         PostOrder(p->rchild);         printf("%c", p->c);     } }
 
 
  void LevelOrder(BiTree T) {     LinkQueue Q;     InitQueue(Q);     BiTree p;     EnQueue(Q,T);     while(!IsEmpty(Q))     {         DeQueue(Q,p);         putchar(p->c);         if(p->lchild)         {             EnQueue(Q,p->lchild);         }         if(p->rchild)         {             EnQueue(Q,p->rchild);         }     } }
  int main() {     BiTree pnew;     BiTree tree=NULL;     char c;     ptag_t phead=NULL,ptail=NULL,listpnew=NULL,pcur;          while(scanf("%c",&c))     {         if(c=='\n')         {             break;         }                  pnew= (BiTree)calloc(1,sizeof(BiTNode));         pnew->c=c;         listpnew= (ptag_t)calloc(1,sizeof(tag_t));         listpnew->p=pnew;                  if(NULL==tree)         {             tree=pnew;             phead=listpnew;             ptail=listpnew;             pcur=listpnew;         }else{                          ptail->pnext=listpnew;             ptail=listpnew;                          if(NULL==pcur->p->lchild)             {                 pcur->p->lchild=pnew;             }else if(NULL==pcur->p->rchild)             {                 pcur->p->rchild=pnew;                 pcur=pcur->pnext;             }         }     }     printf("--------PreOrder----------\n");     PreOrder(tree);     printf("\n--------InOrder------------\n");     InOrder(tree);     printf("\n--------PostOrder------------\n");     PostOrder(tree);     printf("\n--------LevelOrder------------\n");     LevelOrder(tree);     return 0; }
 
  | 
 
5、2014 年 41 题真题讲解
 41.(13 分)二叉树的带权路径长度(WPL)是二叉树中所有叶结点的带权路径长度之和。给定一棵二叉树 T,采用二叉链表存储,结点结构如下:

其中叶结点的 weight 域保存该结点的非负权值。设 root 为指向 T 的根结点的指针,请设计 求 T 的 WPL 的算法,要求:
(1)给出算法的基本设计思想。
(2)使用 C 或 C++语言,给出二叉树结点的数据类型定义。
(3)根据设计思想,采用 C 或 C++语言描述算法,关键之处给出注释。
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
   | #include <stdio.h> #include <stdlib.h>
  typedef int BiElemType; typedef struct BiTNode{     BiElemType weight;     struct BiTNode *lchild;     struct BiTNode *rchild; }BiTNode,*BiTree;
 
  typedef struct tag{     BiTree p;     struct tag *pnext; }tag_t,*ptag_t;
 
 
  int wpl_PreOrder(BiTree p,int deep) {     static int wpl=0;     if(p!=NULL)     {                  if(p->lchild==NULL && p->rchild==NULL)         {             wpl+=p->weight*deep;         }         wpl_PreOrder(p->lchild,deep+1);         wpl_PreOrder(p->rchild,deep+1);     }     return wpl; }
  int WPL(BiTree tree) {     return wpl_PreOrder(tree,0); }
  int main() {     BiTree pnew;     BiTree tree=NULL;     char c;     ptag_t phead=NULL,ptail=NULL,listpnew=NULL,pcur;          while(scanf("%c",&c))     {         if(c=='\n')         {             break;         }                  pnew= (BiTree)calloc(1,sizeof(BiTNode));         pnew->weight=c;         listpnew= (ptag_t)calloc(1,sizeof(tag_t));         listpnew->p=pnew;                  if(NULL==tree)         {             tree=pnew;             phead=listpnew;             ptail=listpnew;             pcur=listpnew;         }else{                          ptail->pnext=listpnew;             ptail=listpnew;                          if(NULL==pcur->p->lchild)             {                 pcur->p->lchild=pnew;             }else if(NULL==pcur->p->rchild)             {                 pcur->p->rchild=pnew;                 pcur=pcur->pnext;             }         }     }     printf("%d\n",WPL(tree));     return 0; }
   |