Chinaunix首页 | 论坛 | 博客
  • 博客访问: 480536
  • 博文数量: 144
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1190
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-08 20:16
文章分类

全部博文(144)

文章存档

2017年(1)

2015年(5)

2014年(108)

2013年(30)

我的朋友

分类: C/C++

2014-08-06 11:07:25


点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <list>
  5. #include <deque>
  6. #include <malloc.h>
  7. using namespace std;
  8. struct BinaryTreeNode
  9. {
  10.     int data;
  11.      struct BinaryTreeNode * left;
  12.       struct BinaryTreeNode * right;
  13. };
  14. typedef struct BinaryTreeNode * BTLink;
  15. int CreatBinaryTree(BTLink *T)
  16. {
  17.     int a;
  18.     BTLink tmp;
  19.     scanf("%d",&a);
  20.     if(a==0)
  21.     {

  22.         *T=0;
  23.         return 1;
  24.     }
  25.     (*T)=(BTLink)malloc(sizeof(struct BinaryTreeNode));
  26.     (*T)->data=a;
  27.     //*T=tmp;
  28.     CreatBinaryTree(&((*T)->left));
  29.     CreatBinaryTree(&(*T)->right);
  30.     return 0;
  31. }
  32. int PrintBinaryTree(BTLink T)
  33. {
  34.  
  35.     if(T==0)
  36.         return 1;
  37.      printf("%d",T->data);
  38.     PrintBinaryTree(T->left);
  39.     PrintBinaryTree(T->right);
  40.     return 0;
  41. }
  42. int BTLink2DLink(BTLink T,BTLink *s)//二叉树转换为双向链表,这里转换完成后s指向双向链表的尾部,
  43.      //因此在主函数里面转换了下
  44. {
  45.     BTLink tmp=T;
  46.     if(T==NULL)
  47.         return 0;
  48.     else
  49.     {
  50.         BTLink2DLink( T->left,s);
  51.         (*s)->right=tmp;
  52.         tmp->left=(*s);

  53.         (*s)=tmp;
  54.          BTLink2DLink( tmp->right,s );
  55.     }
  56.     return 0;
  57. }

  58. int main()
  59. {
  60.  
  61.  
  62.     BTLink BT =(BTLink)malloc(sizeof(struct BinaryTreeNode));
  63.         BTLink head=(BTLink)malloc(sizeof(struct BinaryTreeNode));
  64.         head->left=NULL;
  65.         head->right=NULL;
  66.   CreatBinaryTree(&BT);
  67.   BTLink2DLink(BT,&head);
  68.  
  69.   while(head->left!=NULL)
  70.   {
  71.  
  72.      head=head->left;
  73.   }
  74.   head=head->right;
  75.   while(head!=NULL)
  76.   {
  77.      printf("%d ",head->data);
  78.      head=head->right;
  79.   }
  80. return 0;
  81.  // PrintBinaryTree(BT);

  82. }

阅读(908) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~