C語言二叉樹常見操作詳解【前序,中序,后序,層次遍歷及非遞歸查找,統(tǒng)計(jì)個(gè)數(shù),比較,求深度】
本文實(shí)例講述了C語言二叉樹常見操作。分享給大家供大家參考,具體如下:
一、基本概念
每個(gè)結(jié)點(diǎn)最多有兩棵子樹,左子樹和右子樹,次序不可以顛倒。
性質(zhì):
1、非空二叉樹的第n層上至多有2^(n-1)個(gè)元素。
2、深度為h的二叉樹至多有2^h-1個(gè)結(jié)點(diǎn)。
滿二叉樹:所有終端都在同一層次,且非終端結(jié)點(diǎn)的度數(shù)為2。
在滿二叉樹中若其深度為h,則其所包含的結(jié)點(diǎn)數(shù)必為2^h-1。
完全二叉樹:除了最大的層次即成為一顆滿二叉樹且層次最大那層所有的結(jié)點(diǎn)均向左靠齊,即集中在左面的位置上,不能有空位置。
對于完全二叉樹,設(shè)一個(gè)結(jié)點(diǎn)為i則其父節(jié)點(diǎn)為i/2,2i為左子節(jié)點(diǎn),2i+1為右子節(jié)點(diǎn)。
二、存儲(chǔ)結(jié)構(gòu)
順序存儲(chǔ):
將數(shù)據(jù)結(jié)構(gòu)存在一塊固定的數(shù)組中。
#define LENGTH 100
typedef char datatype;
typedef struct node{
  datatype data;
  int lchild,rchild;
  int parent;
}Node;
Node tree[LENGTH];
int length;
int root;
雖然在遍歷速度上有一定的優(yōu)勢,但因所占空間比較大,是非主流二叉樹。二叉樹通常以鏈?zhǔn)酱鎯?chǔ)。
鏈?zhǔn)酱鎯?chǔ):
typedef char datatype;
typedef struct BinNode{
  datatype data;
  struct BinNode* lchild;
  struct BinNode* rchild;
}BinNode;
typedef BinNode* bintree;     //bintree本身是個(gè)指向結(jié)點(diǎn)的指針
三、二叉樹的遍歷
遍歷即將樹的所有結(jié)點(diǎn)訪問且僅訪問一次。按照根節(jié)點(diǎn)位置的不同分為前序遍歷,中序遍歷,后序遍歷。
前序遍歷:根節(jié)點(diǎn)->左子樹->右子樹
中序遍歷:左子樹->根節(jié)點(diǎn)->右子樹
后序遍歷:左子樹->右子樹->根節(jié)點(diǎn)
例如:求下面樹的三種遍歷
前序遍歷:abdefgc
中序遍歷:debgfac
后序遍歷:edgfbca
四、遍歷的實(shí)現(xiàn)
遞歸實(shí)現(xiàn)(以前序遍歷為例,其他的只是輸出的位置稍有不同)
void preorder(bintree t){
  if(t){
    printf("%c ",t->data);
    preorder(t->lchild);
    preorder(t->rchild);
  }
}
非遞歸的實(shí)現(xiàn)
因?yàn)楫?dāng)遍歷過根節(jié)點(diǎn)之后還要回來,所以必須將其存起來??紤]到后進(jìn)先出的特點(diǎn),選用棧存儲(chǔ)。數(shù)量確定,以順序棧存儲(chǔ)。
#define SIZE 100
typedef struct seqstack{
  bintree data[SIZE];
  int tag[SIZE];  //為后續(xù)遍歷準(zhǔn)備的
  int top;   //top為數(shù)組的下標(biāo)
}seqstack;
void push(seqstack *s,bintree t){
  if(s->top == SIZE){
    printf("the stack is full\n");
  }else{
    s->top++;
    s->data[s->top]=t;
  }
}
bintree pop(seqstack *s){
  if(s->top == -1){
    return NULL;
  }else{
    s->top--;
    return s->data[s->top+1];
  }
}
1、前序遍歷
void preorder_dev(bintree t){
  seqstack s;
  s.top = -1;   //因?yàn)閠op在這里表示了數(shù)組中的位置,所以空為-1
  if(!t){
    printf("the tree is empty\n");
  }else{
    while(t || s.stop != -1){
      while(t){  //只要結(jié)點(diǎn)不為空就應(yīng)該入棧保存,與其左右結(jié)點(diǎn)無關(guān)
         printf("%c ",t->data);
        push(&s,t);
        t= t->lchild;
      }
      t=pop(&s);
      t=t->rchild;
    }
  }
}
2、中序遍歷
void midorder(bintree t){
  seqstack s;
  s.top = -1;
  if(!t){
    printf("the tree is empty!\n");
  }else{
    while(t ||s.top != -1){
      while(t){
        push(&s,t);
        t= t->lchild;
      }
      t=pop(&s);
      printf("%c ",t->data);
      t=t->rchild;
    }
  }
}
3、后序遍歷
因?yàn)楹笮虮闅v最后還要要訪問根結(jié)點(diǎn)一次,所以要訪問根結(jié)點(diǎn)兩次。采取夾標(biāo)志位的方法解決這個(gè)問題。
這段代碼非常糾結(jié),對自己有信心的朋友可以嘗試獨(dú)立寫一下。反正我是寫了很長時(shí)間。邏輯不難,我畫了一張邏輯圖:
代碼:
void postorder_dev(bintree t){
  seqstack s;
  s.top = -1;
  if(!t){
    printf("the tree is empty!\n");
  }else{
    while(t || s.top != -1){  //??樟说耐瑫r(shí)t也為空。
      while(t){
        push(&s,t);
        s.tag[s.top] = 0;  //設(shè)置訪問標(biāo)記,0為第一次訪問,1為第二次訪問
        t= t->lchild;
      }
      if(s.tag[s.top] == 0){ //第一次訪問時(shí),轉(zhuǎn)向同層右結(jié)點(diǎn)
        t= s.data[s.top];  //左走到底時(shí)t是為空的,必須有這步!
        s.tag[s.top]=1;
        t=t->rchild;
      }else {
        while (s.tag[s.top] == 1){ //找到棧中下一個(gè)第一次訪問的結(jié)點(diǎn),退出循環(huán)時(shí)并沒有pop所以為其左子結(jié)點(diǎn)
          t = pop(&s);
          printf("%c ",t->data);
        }
        t = NULL; //必須將t置空。跳過向左走,直接向右走
      }
    }
  }
}
4、層次遍歷:即每一層從左向右輸出
元素需要儲(chǔ)存有先進(jìn)先出的特性,所以選用隊(duì)列存儲(chǔ)。
隊(duì)列的定義:
#define MAX 1000
typedef struct seqqueue{
  bintree data[MAX];
  int front;
  int rear;
}seqqueue;
void enter(seqqueue *q,bintree t){
  if(q->rear == MAX){
    printf("the queue is full!\n");
  }else{
    q->data[q->rear] = t;
    q->rear++;
  }
}
bintree del(seqqueue *q){
  if(q->front == q->rear){
    return NULL;
  }else{
    q->front++;
    return q->data[q->front-1];
  }
}
遍歷實(shí)現(xiàn)
void level_tree(bintree t){
  seqqueue q;
  bintree temp;
  q.front = q.rear = 0;
  if(!t){
    printf("the tree is empty\n");
    return ;
  }
  enter(&q,t);
  while(q.front != q.rear){
    t=del(&q);
    printf("%c ",t->data);
    if(t->lchild){
      enter(&q,t->lchild);
    }
    if(t->rchild){
      enter(&q,t->rchild);
    }
  }
}
5、利用前序遍歷的結(jié)果生成二叉樹
//遞歸調(diào)用,不存點(diǎn),想的時(shí)候只關(guān)注于一個(gè)點(diǎn),因?yàn)檫€會(huì)回來的,不要跟蹤程序運(yùn)行,否則容易多加循環(huán)
void createtree(bintree *t){
  datatype c;
  if((c=getchar()) == '#')
    *t = NULL;
  else{
    *t = (bintree)malloc(sizeof(BinNode));
    (*t)->data = c;
    createtree(&(*t)->lchild);
    createtree(&(*t)->rchild);
  }
}
6、二叉樹的查找
bintree search_tree(bintree t,datatype x){
  if(!t){
    return NULL;
  }
  if(t->data == x){
    return t;
  }else{
    if(!search_tree(t->lchild,x)){
      return search_tree(t->rchild,x);
    }
    return t;
  }
}
7、統(tǒng)計(jì)結(jié)點(diǎn)個(gè)數(shù)
int count_tree(bintree t){
  if(t){
    return (count_tree(t->lchild)+count_tree(t->rchild)+1);
  }
  return 0;
}
8、比較兩個(gè)樹是否相同
int is_equal(bintree t1,bintree t2){
  if(!t1 && !t2){   //都為空就相等
    return 1;
  }
  if(t1 && t2 && t1->data == t2->data){   //有一個(gè)為空或數(shù)據(jù)不同就不判斷了
    if(is_equal(t1->lchild,t2->lchild))
      if(is_equal(t1->rchild,t2->rchild)){
        return 1;
      }
  }
  return 0;
}
9、求二叉樹的深度
int hight_tree(bintree t){
  int h,left,right;
  if(!t){
    return 0;
  }
  left = hight_tree(t->lchild);
  right = hight_tree(t->rchild);
  h = (left>right?left:right)+1;
  return h;
}
希望本文所述對大家C語言程序設(shè)計(jì)有所幫助。
上一篇:C++基于prim實(shí)現(xiàn)迷宮生成
欄 目:C語言
下一篇:OpenCV cv.Mat與.txt文件數(shù)據(jù)的讀寫操作
本文標(biāo)題:C語言二叉樹常見操作詳解【前序,中序,后序,層次遍歷及非遞歸查找,統(tǒng)計(jì)個(gè)數(shù),比較,求深度】
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/817.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
 - 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘
 


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
 
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
 - 04-02jquery與jsp,用jquery
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-11ajax實(shí)現(xiàn)頁面的局部加載
 - 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-10C#中split用法實(shí)例總結(jié)
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10delphi制作wav文件的方法
 


