当前位置:   article > 正文

二叉树的各种创建方法_二叉树的建立

二叉树的建立

1.前序创建

  1. #include<stdlib.h>
  2. #include<malloc.h>
  3. #include<iostream>
  4. #include<stack>
  5. #include<queue>
  6. using namespace std;
  7. typedef  char ElemType;
  8. typedef struct  BtNode
  9. {
  10. BtNode *leftchild;
  11. BtNode *rightchild;
  12. ElemType data;
  13. }BtNode,*BinaryTree;
  14. //购买结点
  15. BtNode *Buynode()
  16. {
  17. BtNode *p=(BtNode *)malloc(sizeof(BtNode));
  18. if(NULL==p)  exit(1);
  19. memset(p,0,sizeof(BtNode));
  20. return p;
  21. }
  22. void *Freenode(BtNode *ptr)
  23. {
  24. free(ptr);
  25. }
第一种建树方法(通过结点进行创建二叉树)
  1. //方法一
  2. //前序创建二叉树
  3. void *PreCreateTree(BinaryTree *ptr)
  4. {
  5. char ch;
  6. cin>>ch;
  7. if(ch!='#'||ptr!=NULL)
  8.         {
  9. *ptr=Buynode();
  10. (*ptr)->data=ch;
  11. PreCreateTree(&(*ptr)->leftchild);
  12. PreCreateTree(&(*ptr)->rightchild);
  13. }
  14. }
  15. int main()
  16. {
  17. BinaryTree root=NULL;
  18. PreCreateTree(&root);
  19. }
第二种创建方法:
  1. //利用指针
  2. void Createtree4(BtNode ** const p,char *&str)
  3. {
  4. if( str != NULL && *str != '#')
  5. {
  6. (*p) = Buynode();
  7. (*p)->data = *str;
  8. Createtree4(&(*p)->leftchild,++str);
  9. Createtree4(&(*p)->rightchild,++str);
  10. }
  11. }
第三种用前序方法创建二叉树(通过传入空,进行创建)
  1. BtNode *CreateTree()
  2. {
  3. ElemType x;
  4. BtNode *s=NULL;
  5. cin>>x;
  6. if(x!=END)
  7. {
  8. s=Buynode();
  9. s->data=x;
  10. s->leftchild=CreateTree();
  11. s->rightchild=CreateTree();
  12. }
  13. return s;
  14. }
第四种用前序创建二叉树,通过传入引用(如果不是传入的是引用,会出现问题)
  1. //传引用建树
  2. BtNode *Createtree1(char *&str)
  3. {
  4. BtNode *p = NULL;
  5. if( str != NULL && *str != '#')
  6. {
  7. p = Buynode();
  8. p->data = *str;
  9. p->leftchild = Createtree1(++str);
  10. p->rightchild = Createtree1(++str);
  11. }
  12. return p;
  13. }
第五种创建方法:
  1. //引用转换成二级指针建树
  2. BtNode *Createtree2(char ** const str)
  3. {
  4. BtNode *p = NULL;
  5. if( str != NULL && *str != NULL && **str != '#')
  6. {
  7. p = Buynode();
  8. p->data = **str;
  9. p->leftchild = Createtree1(++*str);
  10. p->rightchild = Createtree1(++*str);
  11. }
  12. return p;
  13. }
第六种创建方法:
  1. //给一个结点建立左右孩子
  2. void Createtree3(BtNode *&p,char *&str)
  3. {
  4. if( str != NULL && *str != '#')
  5. {
  6. p = Buynode();
  7. p->data = *str;
  8. Createtree3(p->leftchild,++str);
  9. Createtree3(p->rightchild,++str);
  10. }
  11. }

2.用前序和中序创建二叉树

方法一:

  1. //用前序和中序创建二叉树
  2. *BtNode* CreateTreePI(char ps[],char is[],int n )
  3. {
  4. if(n<=0)
  5. {
  6. return NULL;
  7. }
  8. BtNode* p=(BtNode*)malloc(sizeof(BtNode));
  9. int i=0;
  10. int m;
  11. while(i<n)
  12. {
  13. if(ps[0]==is[i])
  14. {
  15. m=i;
  16. break;
  17. }
  18. ++i;
  19. }
  20. if(i>=n)
  21. {
  22. return NULL;
  23. }
  24. p->data=ps[0];
  25. p->leftchild=CreateTreePI(ps+1,is,m);
  26. p->rightchild=CreateTreePI(ps+m+1,is+m+1,n-m-1);
  27. return p;
  28. }
  29. int main()
  30. {
  31. char ps[]="ABDGHCEIF";
  32. char is[]="GDHBAEICF";
  33. int len=strlen(ps);
  34. CreateTreePI(ps,is,len);
  35. }

方法二:

  1. int Findvalue(char *is,ElemType p,int n)
  2. {
  3. for(int i = 0;i < n;++i)
  4. {
  5. if(is[i] == p)
  6. return i;
  7. }
  8. return -1;
  9. }
  10. //根据前序和中序建立一个二叉树
  11. BtNode *CreatePM(char *pr,char *is,int n)
  12. {
  13. ElemType p = pr[0];
  14. BtNode *tmp = NULL;
  15. if(n > 0)
  16. {
  17. int m = Findvalue(is,p,n);
  18. if(-1 == m) exit(1);
  19. tmp = Buynode();
  20. tmp->data = p;
  21. tmp->leftchild = CreatePM(pr+1,is,m);
  22. tmp->rightchild = CreatePM(pr+m+1,is+m+1,n-m-1);
  23. }
  24. return tmp;
  25. }
  26. BtNode *CreatetreePM(char *pr,char *is,int n)
  27. {
  28. if(pr == NULL || is == NULL || n < 1) return NULL;
  29. return CreatePM(pr,is,n);
  30. }

3.用中序和后序创建二叉树

方法一:

  1. //用中序和后序创建二叉树
  2. BtNode* CreateTreeIL(char is[],char ls[],int n)
  3. {
  4. if(n<=0)
  5. {
  6. return NULL;
  7. }
  8. BtNode *p=(BtNode*)malloc(sizeof(BtNode));
  9. int i=0;
  10. int m;
  11. while(i<n)
  12. {
  13. if(ls[n-1]==is[i])
  14. {
  15. m=i;
  16. break;
  17. }
  18. ++i;
  19. }
  20. if(i>=n)
  21. {
  22. return NULL;
  23. }
  24. p->data=ls[n-1];
  25. p->leftchild= CreateTreeIL(is,ls,m);
  26. p->rightchild= CreateTreeIL(is+m+1,ls+m,n-m-1);
  27. return p;
  28. }
  29. int main()
  30. {
  31. char is[]="GDHBAEICF";
  32. char ls[]="GHDBIEFCA";
  33. int len=strlen(is);
  34. CreateTreeIL(is,ls,len);
  35. }

方法二:

  1. // 根据中序和后序建立一个二叉树
  2. BtNode *CreateML(char *mi,char *la,int n)
  3. {
  4. ElemType p = la[n-1];
  5. BtNode *tmp = NULL;
  6. if(n > 0)
  7. {
  8. int m = Findvalue(mi,p,n);
  9. if(-1 == m) exit(1);
  10. tmp = Buynode();
  11. tmp->data = p;
  12. tmp->leftchild = CreateML(mi,la,m);
  13. tmp->rightchild = CreateML(mi+m+1,la+m,n-m-1);
  14. }
  15. return tmp;
  16. }
  17. BtNode *CreatetreeML(char *mi,char *la,int n)
  18. {
  19. if(mi == NULL || la == NULL || n < 1) return NULL;
  20. return CreateML(mi,la,n);
  21. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/233222
推荐阅读
相关标签
  

闽ICP备14008679号