国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

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

2020-05-23 14:15:55
字體:
供稿:網(wǎng)友

這篇文章主要介紹了使用C語言使用C語言構(gòu)建基本的二叉樹數(shù)據(jù)結(jié)構(gòu),包括根據(jù)前序序列和中序序列構(gòu)建二叉樹的方法,需要的朋友可以參考下

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

 

 
  1. #include 
  2. #include 
  3.  
  4. typedef struct Node{ 
  5. int data; 
  6. Node *leftchild; 
  7. Node *rightchild; 
  8. }Node; 
  9.  
  10.  
  11. /* 
  12. 初始化一棵二叉樹排序樹。 
  13. */ 
  14. void InitBinaryTree(Node**root,int elem) 
  15. *root=(Node*)malloc(sizeof(Node)); 
  16. if(!(*root)) 
  17. printf("Memory allocation for root failed./n"); 
  18. return
  19. (*root)->data=elem; 
  20. (*root)->leftchild=NULL; 
  21. (*root)->rightchild=NULL; 
  22.  
  23. /* 
  24. 向二叉樹排序樹中插入節(jié)點(diǎn)。 
  25. */ 
  26. void InsertNode(Node *root,int elem) 
  27. Node *newnode=NULL; 
  28. Node *p=root,*last_p=NULL; 
  29.  
  30. newnode=(Node*)malloc(sizeof(Node)); 
  31. if(!newnode) 
  32. printf("Memory allocation for newnode failed./n"); 
  33. return
  34. newnode->data=elem; 
  35. newnode->leftchild=NULL; 
  36. newnode->rightchild=NULL; 
  37.  
  38. while(NULL!=p) 
  39. last_p=p; 
  40. if(newnode->datadata) 
  41. p=p->leftchild; 
  42. else if(newnode->data>p->data) 
  43. p=p->rightchild; 
  44. else 
  45. printf("Node to be inserted has existed./n"); 
  46. free(newnode); 
  47. return
  48. p=last_p; 
  49. if(newnode->datadata) 
  50. p->leftchild=newnode; 
  51. else 
  52. p->rightchild=newnode; 
  53.  
  54. /* 
  55. 創(chuàng)建一棵二叉樹排序樹。 
  56. */ 
  57. void CreatBinarySearchTree(Node **root,int data[],int num) 
  58. int i; 
  59.  
  60. for(i=0;i 
  61. if(NULL==*root) 
  62. InitBinaryTree(root,data[i]); 
  63. else 
  64. InsertNode(*root,data[i]); 

根據(jù)前序序列、中序序列構(gòu)建二叉樹

函數(shù)定義

 

 
  1. bt rebuildTree(char *pre, char *inint len);  

參數(shù):

* pre:前序遍歷結(jié)果的字符串?dāng)?shù)組

* in:中序遍歷結(jié)果的字符串?dāng)?shù)組

len : 樹的長度

例如:

前序遍歷結(jié)果: a b c d e f g h

中序遍歷結(jié)果: c b e d f a g h

算法思想

遞歸思想,遞歸的終止條件是樹的長度len == 0

在中序遍歷的數(shù)組中找到前序數(shù)組的第一個(gè)字符,記錄在中序數(shù)組中的位置index.如果找不到,說明前序遍歷數(shù)組和中序遍歷數(shù)組有問題,提示錯(cuò)誤信息,退出程序即可;找到index后,新建一個(gè)二叉樹節(jié)點(diǎn)t,t->item = *pre,然后遞歸的求t的左孩子和有孩子

遞歸的左孩子:void rebuildTree(pre + 1, in, index)

遞歸的右孩子:void rebuildTree(pre + (index + 1), in + (index + 1), len - (index + 1))

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

 

 
  1. /**  
  2. * Description:根據(jù)前序和中序構(gòu)建二叉樹  
  3. */ 
  4. bt rebuildTree(char *pre, char *inint len)  
  5. {  
  6. bt t;  
  7. if(len <= 0)  
  8. {  
  9. //遞歸終止  
  10. t = NULL;  
  11. }else 
  12. {  
  13. //遞歸主體  
  14. int index = 0;  
  15.  
  16. while(index < len && *(pre) != *(in + index))  
  17. {  
  18. index ++;  
  19. }  
  20.  
  21. if(index >= len)  
  22. {  
  23. printf("前序遍歷或者中序遍歷數(shù)組有問題!/n");  
  24. exit(-1);  
  25. }  
  26.  
  27. t = (struct bintree *)malloc(sizeof(struct bintree));  
  28. t->item = *pre;  
  29. t->lchild = rebuildTree(pre + 1, in, index);  
  30. t->rchild = rebuildTree(pre + (index + 1), in + (index + 1), len - (index + 1));  
  31. }  
  32. return t;  
  33. }  

根據(jù)中序序列、后序序列構(gòu)建二叉樹

函數(shù)定義

 

 
  1. /**  
  2. * 中序、后序序列構(gòu)建二叉樹  
  3. */ 
  4. 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ù)后序遍歷的特點(diǎn),知道后序遍歷最后一個(gè)節(jié)點(diǎn)為根節(jié)點(diǎn),即為A

觀察中序遍歷,A左側(cè)CBEDF為A左子樹節(jié)點(diǎn),A后側(cè)HGJI為A右子樹節(jié)點(diǎn)

然后遞歸的構(gòu)建A的左子樹和后子樹

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

 

 
  1. /**  
  2. * 根據(jù)中序和后序序列構(gòu)建二叉樹  
  3. * (ps:昨晚參加阿里筆試,等到最后說可以免筆試直接面試,今天估計(jì)還是要根據(jù)學(xué)校篩選,哈哈,為了這點(diǎn)  
  4. * 也得參加阿里筆試,不能讓自己的學(xué)校受到鄙視)  
  5. */ 
  6.  
  7. #include <stdio.h>  
  8. #include <stdlib.h>  
  9. #include <string.h>  
  10.  
  11. int n;  
  12.  
  13. typedef struct btree {  
  14. struct btree *lchild;  
  15. struct btree *rchild;  
  16. char data;  
  17. } btree;  
  18.  
  19. /**  
  20. * 中序、后序序列構(gòu)建二叉樹  
  21. */ 
  22. btree* rebuildTree(char *order, char *post, int len)  
  23. {  
  24. btree *t;  
  25.  
  26. if (len <= 0) {  
  27. return NULL;  
  28. else {  
  29. int index = 0;  
  30.  
  31. while (index < len && *(post + len - 1) != *(order + index)) {  
  32. index ++;  
  33. }  
  34.  
  35. t = (btree *)malloc(sizeof(btree));  
  36. t->data = *(order + index);  
  37. t->lchild = rebuildTree(order, post, index);  
  38. t->rchild = rebuildTree(order + index + 1, post + index, len - (index + 1));  
  39. }  
  40.  
  41. return t;  
  42. }  
  43.  
  44. /**  
  45. * 前序遍歷二叉樹  
  46. */ 
  47. void preTraverse(btree *t)  
  48. {  
  49. if (t) {  
  50. printf("%c ", t->data);  
  51. preTraverse(t->lchild);  
  52. preTraverse(t->rchild);  
  53. }  
  54. }  
  55.  
  56. int main(void)  
  57. {  
  58. int i;  
  59. char *post, *order;  
  60. btree *t;  
  61.  
  62. while (scanf("%d", &n) != EOF) {  
  63. post = (char *)malloc(n);  
  64. order = (char *)malloc(n);  
  65.  
  66. getchar();  
  67. for (i = 0; i < n; i ++)  
  68. scanf("%c", order + i);  
  69.  
  70. getchar();  
  71. for (i = 0; i < n; i ++)  
  72. scanf("%c", post + i);  
  73.  
  74. t = rebuildTree(order, post, n);  
  75.  
  76. preTraverse(t);  
  77. printf("/n");  
  78.  
  79. free(post);  
  80. free(order);  
  81.  
  82. }  
  83.  
  84. return 0;  
  85. }  

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 巩留县| 华坪县| 靖西县| 正蓝旗| 西藏| 肇州县| 南召县| 商洛市| 米林县| 六枝特区| 会理县| 台州市| 施秉县| 定南县| 新丰县| 格尔木市| 茌平县| 筠连县| 甘德县| 玛多县| 新津县| 澄江县| 固始县| 南通市| 抚宁县| 伊通| 武邑县| 谢通门县| 扶余县| 收藏| 浏阳市| 勃利县| 洪雅县| 天气| 逊克县| 曲阳县| 花莲县| 拉萨市| 霍邱县| 屏东市| 吴江市|