欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用C語言構(gòu)建基本的二叉樹數(shù)據(jù)結(jié)構(gòu)

 更新時間:2015年08月19日 12:19:29   作者:FinL  
這篇文章主要介紹了使用C語言使用C語言構(gòu)建基本的二叉樹數(shù)據(jù)結(jié)構(gòu),包括根據(jù)前序序列和中序序列構(gòu)建二叉樹的方法,需要的朋友可以參考下

二叉樹結(jié)構(gòu)常用的一些初始化代碼

#include
#include

typedef struct Node{
 int data;
 Node *leftchild;
 Node *rightchild;
}Node;


/*
 初始化一棵二叉樹排序樹。
*/
void InitBinaryTree(Node**root,int elem)
{
 *root=(Node*)malloc(sizeof(Node));
 if(!(*root))
 {
 printf("Memory allocation for root failed.\n");
 return;
 }
 (*root)->data=elem;
 (*root)->leftchild=NULL;
 (*root)->rightchild=NULL;
}

/*
 向二叉樹排序樹中插入節(jié)點。
*/
void InsertNode(Node *root,int elem)
{
 Node *newnode=NULL;
 Node *p=root,*last_p=NULL;

 newnode=(Node*)malloc(sizeof(Node));
 if(!newnode)
 {
 printf("Memory allocation for newnode failed.\n");
 return;
 }
 newnode->data=elem;
 newnode->leftchild=NULL;
 newnode->rightchild=NULL;

 while(NULL!=p)
 {
 last_p=p;
 if(newnode->datadata)
 {
 p=p->leftchild;
 }
 else if(newnode->data>p->data)
 {
 p=p->rightchild;
 }
 else
 {
 printf("Node to be inserted has existed.\n");
 free(newnode);
 return;
 }
 }
 p=last_p;
 if(newnode->datadata)
 {
 p->leftchild=newnode;
 }
 else
 {
 p->rightchild=newnode;
 }
}

/*
 創(chuàng)建一棵二叉樹排序樹。
*/
void CreatBinarySearchTree(Node **root,int data[],int num)
{
 int i;

 for(i=0;i
 {
 if(NULL==*root)
 {
 InitBinaryTree(root,data[i]);
 }
 else
 {
 InsertNode(*root,data[i]);
 }
 }
}

根據(jù)前序序列、中序序列構(gòu)建二叉樹
函數(shù)定義

 bt rebuildTree(char *pre, char *in, int len); 


參數(shù):
* pre:前序遍歷結(jié)果的字符串數(shù)組
* in:中序遍歷結(jié)果的字符串數(shù)組
len : 樹的長度
例如:
前序遍歷結(jié)果: a b c d e f g h
中序遍歷結(jié)果: c b e d f a g h

算法思想

  •     遞歸思想,遞歸的終止條件是樹的長度len == 0
  •     在中序遍歷的數(shù)組中找到前序數(shù)組的第一個字符,記錄在中序數(shù)組中的位置index.如果找不到,說明前序遍歷數(shù)組和中序遍歷數(shù)組有問題,提示錯誤信息,退出程序即可;找到index后,新建一個二叉樹節(jié)點t,t->item = *pre,然后遞歸的求t的左孩子和有孩子
  •     遞歸的左孩子:void rebuildTree(pre + 1, in, index)
  •     遞歸的右孩子:void rebuildTree(pre + (index + 1), in + (index + 1), len - (index + 1))

實現(xiàn)代碼(c語言版)

  

 /** 
  * Description:根據(jù)前序和中序構(gòu)建二叉樹 
  */ 
 bt rebuildTree(char *pre, char *in, int len) 
 { 
  bt t; 
  if(len <= 0) 
  { 
   //遞歸終止 
   t = NULL; 
  }else 
  { 
   //遞歸主體 
   int index = 0; 
    
   while(index < len && *(pre) != *(in + index)) 
   { 
    index ++; 
   } 
    
   if(index >= len) 
   { 
    printf("前序遍歷或者中序遍歷數(shù)組有問題!\n"); 
    exit(-1); 
   } 
    
   t = (struct bintree *)malloc(sizeof(struct bintree)); 
   t->item = *pre; 
   t->lchild = rebuildTree(pre + 1, in, index); 
   t->rchild = rebuildTree(pre + (index + 1), in + (index + 1), len - (index + 1)); 
  } 
  return t; 
 } 


根據(jù)中序序列、后序序列構(gòu)建二叉樹
函數(shù)定義

 /** 
  * 中序、后序序列構(gòu)建二叉樹 
  */ 
 btree* rebuildTree(char *order, char *post, int len); 


算法思想
中序序列:C、B、E、D、F、A、H、G、J、I

后序序列:C、E、F、D、B、H、J、I、G、A

遞歸思路:

  •     根據(jù)后序遍歷的特點,知道后序遍歷最后一個節(jié)點為根節(jié)點,即為A
  •     觀察中序遍歷,A左側(cè)CBEDF為A左子樹節(jié)點,A后側(cè)HGJI為A右子樹節(jié)點
  •     然后遞歸的構(gòu)建A的左子樹和后子樹


實現(xiàn)代碼(c代碼)

 /** 
  * 根據(jù)中序和后序序列構(gòu)建二叉樹 
  * (ps:昨晚參加阿里筆試,等到最后說可以免筆試直接面試,今天估計還是要根據(jù)學校篩選,哈哈,為了這點 
  * 也得參加阿里筆試,不能讓自己的學校受到鄙視) 
  */ 
  
 #include <stdio.h> 
 #include <stdlib.h> 
 #include <string.h> 
  
 int n; 
  
 typedef struct btree { 
  struct btree *lchild; 
  struct btree *rchild; 
  char data; 
 } btree; 
  
 /** 
  * 中序、后序序列構(gòu)建二叉樹 
  */ 
 btree* rebuildTree(char *order, char *post, int len) 
 { 
  btree *t; 
  
  if (len <= 0) { 
   return NULL; 
  } else { 
   int index = 0; 
  
   while (index < len && *(post + len - 1) != *(order + index)) { 
    index ++; 
   }  
  
   t = (btree *)malloc(sizeof(btree)); 
   t->data = *(order + index); 
   t->lchild = rebuildTree(order, post, index); 
   t->rchild = rebuildTree(order + index + 1, post + index, len - (index + 1)); 
  } 
  
  return t; 
 } 
  
 /** 
  * 前序遍歷二叉樹 
  */ 
 void preTraverse(btree *t) 
 { 
  if (t) { 
   printf("%c ", t->data); 
   preTraverse(t->lchild); 
   preTraverse(t->rchild); 
  } 
 } 
  
 int main(void) 
 { 
  int i; 
  char *post, *order; 
  btree *t; 
  
  while (scanf("%d", &n) != EOF) { 
   post = (char *)malloc(n); 
   order = (char *)malloc(n); 
    
   getchar(); 
   for (i = 0; i < n; i ++) 
    scanf("%c", order + i); 
  
   getchar(); 
   for (i = 0; i < n; i ++) 
    scanf("%c", post + i); 
  
   t = rebuildTree(order, post, n); 
  
   preTraverse(t); 
   printf("\n"); 
  
   free(post); 
   free(order); 
  
  } 
  
  return 0; 
 } 

相關(guān)文章

  • C++中用兩個標準容器stack,實現(xiàn)一個隊列的方法詳解

    C++中用兩個標準容器stack,實現(xiàn)一個隊列的方法詳解

    本篇文章是對C++中使用兩個標準容器stack,實現(xiàn)一個隊列的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • 使用OpenGL創(chuàng)建窗口的示例詳解

    使用OpenGL創(chuàng)建窗口的示例詳解

    OpenGL,也就是Open?Graphics?Library。其主要就是用于我們?nèi)ヤ秩?D、3D矢量圖形的一種跨語言、跨平臺的應(yīng)用程序編程接口,這篇文章主要介紹了使用OpenGL創(chuàng)建窗口,需要的朋友可以參考下
    2022-04-04
  • 淺談C++的語句語法與強制數(shù)據(jù)類型轉(zhuǎn)換

    淺談C++的語句語法與強制數(shù)據(jù)類型轉(zhuǎn)換

    這篇文章主要介紹了淺談C++的語句語法與強制數(shù)據(jù)類型轉(zhuǎn)換,是C++入門學習中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-09-09
  • 利用Matlab繪制一款專屬進度條

    利用Matlab繪制一款專屬進度條

    MATLAB自帶的進度條是很簡單的,這樣的進度條顯得冷冰冰的。因此,本文將用Matlab來DIY一款專屬的進度條,感興趣的小伙伴可以了解一下
    2022-02-02
  • 詳解C++ string常用截取字符串方法

    詳解C++ string常用截取字符串方法

    這篇文章主要介紹了C++ string常用截取字符串方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • QT使用QComBox和QLineEdit實現(xiàn)模糊查詢功能

    QT使用QComBox和QLineEdit實現(xiàn)模糊查詢功能

    模糊查詢是指根據(jù)用戶輸入的文本,在下拉框的選項中進行模糊匹配,并動態(tài)地顯示匹配的選項,本文將使用QComBox和QLineEdit實現(xiàn)模糊查詢功能,需要的可以參考下
    2023-11-11
  • C++中map和set的使用及示例

    C++中map和set的使用及示例

    map和set是STL容器中的部分,本文主要介紹了C++中map和set的使用小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-01-01
  • C++/C 回文字符串的實例詳解

    C++/C 回文字符串的實例詳解

    這篇文章主要介紹了C++ 回文字符串的實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • C++中std::count函數(shù)介紹和使用場景

    C++中std::count函數(shù)介紹和使用場景

    std::count函數(shù)是一個非常實用的算法,它可以幫助我們快速統(tǒng)計給定值在指定范圍內(nèi)的出現(xiàn)次數(shù),本文主要介紹了C++中std::count函數(shù)介紹和使用場景,感興趣的可以了解一下
    2024-02-02
  • C++實現(xiàn)屏幕截圖(全屏截圖)

    C++實現(xiàn)屏幕截圖(全屏截圖)

    屏幕截圖已經(jīng)成為了所有IM即時通訊軟件的必備模塊,也是日常辦公中使用最頻繁的功能之一。今天我們從C++開發(fā)的角度,來看看屏幕截圖的主要功能點是如何實現(xiàn)的,感興趣的可以了解一下
    2021-11-11

最新評論