二叉树学习

来源:互联网 发布:telnet php 编辑:程序博客网 时间:2024/06/05 17:12

1.二叉树的性质

二叉树是K叉树的特例,一个节点至多有俩个子树。

第N层上至多有2^(N-1)个元素。

深度为h的二叉树至多有2^(h)-1节点。

满二叉树:除了最深的一层其他层都是从左到右满层。

完全二叉树:除了叶节点所有节点都有左右两个子结点。

2.实现方式

数组

  1. #define LENGTH 100  typedef char datatype;  typedef struct node{      datatype data;      int lchild,rchild;      int parent;  }Node;    Node tree[LENGTH];  int length;  int root;  


链表

  1. typedef char datatype;  
  2.   
  3. typedef struct BinNode{  
  4.     datatype data;  
  5.     struct BinNode* lchild;  
  6.     struct BinNode* rchild;  
  7. }BinNode;  
  8.   
  9. typedef BinNode* bintree;
3.遍历方式

前序:根节点->左子树->右子树

中序:左子树->根节点->右子树

后序:左子树->右子树->根节点

4.遍历的实现

以前序遍历为例

递归实现:

void preorder(bintree pbt)

{

if(pbt->data!=NULL)

print(pbt->data);

preorder(pbt->lchild);

preorder(pbt->rchild);

}

非递归实现

  1. #define SIZE 100  
  2. typedef struct seqstack{  
  3.     bintree data[SIZE];  
  4.     int tag[SIZE];   //为后续遍历准备的  
  5.     int top;     //top为数组的下标  
  6. }seqstack;  
  7.   
  8. void push(seqstack *s,bintree t){  
  9.   
  10.     if(s->top == SIZE){  
  11.         printf("the stack is full\n");  
  12.     }else{  
  13.         s->top++;  
  14.         s->data[s->top]=t;  
  15.     }  
  16. }  
  17.   
  18. bintree pop(seqstack *s){  
  19.     if(s->top == -1){  
  20.         return NULL;  
  21.     }else{  
  22.         s->top--;  
  23.         return s->data[s->top+1];  
  24.     }  
  25. }  
  26. ##前序遍历
  27. void preorder(bintree t){
  28. seqstack s;
  29. s.top=-1;
  30. if(!t)
  31. print("NULL");
  32. else{
  33. while(t||s.top!=-1)
  34. {
  35. while(t){
  36. print(t->data);
  37. push(&s,t);
  38. t=t->lchild;
  39. }
  40. t=pop(&s);
  41. t=t->rchild;
  42. }

  43. }
  44. }
    1. ##中序遍历
    2. void midorder(bintree t){
    3. seqstack s;
    4. s.top=-1;
    5. if(!t)
    6. print("NULL");
    7. else{
    8. while(t||s.top!=-1)
    9. {
    10. while(t){
    11. push(&s,t);
    12. t=t->lchild;
    13. }
    14. t=pop(&s);
    15. print(t->data);
    16. t=t->rchild;
    17. }

    18. }
    19. }
    20. ##后序遍历

    21. void postorder_dev(bintree t){  
          seqstack s;  
          s.top = -1;  
          if(!t){  
              printf("the tree is empty!\n");  
          }else{  
              while(t || s.top != -1){    //栈空了的同时t也为空。  
                  while(t){  
                      push(&s,t);  
                      s.tag[s.top] = 0;   //设置访问标记,0为第一次访问,1为第二次访问  
                      t= t->lchild;  
                  }  
                  if(s.tag[s.top] == 0){  //第一次访问时,转向同层右结点  
                      t= s.data[s.top];   //左走到底时t是为空的,必须有这步!  
                      s.tag[s.top]=1;       
                      t=t->rchild;  
                  }else {  
                      while (s.tag[s.top] == 1){ //找到栈中下一个第一次访问的结点,退出循环时并没有pop所以为其左子结点  
                          t = pop(&s);  
                          printf("%c ",t->data);  
                      }  
                      t = NULL; //必须将t置空。跳过向左走,直接向右走  
                  }  
              }  
          }  
      }


前序遍历:


原创粉丝点击