Chinaunix首页 | 论坛 | 博客
  • 博客访问: 541714
  • 博文数量: 129
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1888
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-20 11:09
文章分类

全部博文(129)

文章存档

2016年(1)

2015年(5)

2014年(64)

2013年(59)

我的朋友

分类: C/C++

2013-11-26 10:25:06

我自己实现的二叉树的操作,比较简易的版本。更具体的见我的上一篇博客。

 
  1. #include<iostream>
  2. #include<stack>
  3. using namespace std;

  4. typedef struct BiTNode
  5. {
  6.     char data;
  7.     BiTNode*lchild;
  8.     BiTNode*rchild;
  9. }BiTNode,*BiTree;


  10. //°????ò?ò???¨???????÷
  11. void CreateBiTree(BiTree &T)
  12. {
  13.     char ch;
  14.     scanf("%c",&ch);
  15.     if(ch=='#')
  16.         T=NULL;
  17.     else
  18.     {
  19.      T=(BiTNode*)malloc(sizeof(BiTNode));
  20.         T->data=ch;
  21.         CreateBiTree(T->lchild);
  22.         CreateBiTree(T->rchild);
  23.     }
  24. }

  25. //?°?ò±é?ú??·¨
  26. void PreOrder(BiTree &T) //???é???°?ò±é?ú
  27. {
  28.     if(T)
  29.     {
  30.      printf("%c ",T->data);
  31.         PreOrder(T->lchild);
  32.         PreOrder(T->rchild);
  33.     }
  34.     
  35.     else return;

  36. }

  37. void InOrder(BiTree &T) //???ò???é±é?ú
  38. {
  39.     if(T)
  40.     {
  41.      InOrder(T->lchild);
  42.         printf("%c ",T->data);
  43.         InOrder(T->rchild);
  44.     }
  45.     else return ;
  46. }

  47. void PostOrder(BiTree &T)
  48. {
  49.     if(T)
  50.     {
  51.      PostOrder(T->lchild);
  52.         PostOrder(T->rchild);
  53.         printf("%c ",T->data);
  54.     }
  55.     else
  56.         return;

  57. }

  58. void PreOrder_Nonrecursive(BiTree &T) //·????é?°?ò
  59. {
  60.     if(!T)
  61.         return;
  62.     stack<BiTree> s;
  63.     s.push(T);
  64.     while(!s.empty())
  65.     {
  66.      BiTree tmp=s.top();
  67.      cout<<tmp->data<<" ";
  68.         s.pop();
  69.         if(tmp->rchild)
  70.             s.push(tmp->rchild);
  71.         if(tmp->lchild)
  72.             s.push(tmp->lchild);
  73.     }

  74. }

  75. void InOrder_Nonrecursive(BiTree &T) //·????é???ò±é?ú???????é??????·¨
  76. {
  77.     BiTree p,tmp;
  78.     p=T;
  79.     stack<BiTree> s;
  80.     while(p || !s.empty())
  81.     {
  82.      if(p)
  83.         {
  84.          s.push(p);
  85.             p=p->lchild;
  86.         }
  87.         else
  88.         {
  89.          tmp=s.top();
  90.             cout<<tmp->data<<" ";
  91.             s.pop();
  92.             p=tmp->rchild;
  93.         }
  94.     
  95.     }

  96. }


  97. void PostOrder_Nonrecursive(BiTree &T) //·????é?ó??±é?ú??????·¨
  98. {
  99.     stack<BiTree> s1,s2;
  100.     BiTree curr;
  101.     s1.push(T);
  102.     while(!s1.empty())
  103.     {
  104.      curr=s1.top();
  105.         s2.push(curr);
  106.         s1.pop();
  107.         
  108.         if(curr->lchild)
  109.             s1.push(curr->lchild);
  110.         if(curr->rchild)
  111.             s1.push(curr->rchild);
  112.     }
  113.     while(!s2.empty())
  114.     {
  115.      printf("%c ",s2.top()->data);
  116.         s2.pop();
  117.     }
  118. }

  119. int main()
  120. {
  121.     BiTree T=NULL;
  122.     CreateBiTree(T);
  123.     PreOrder(T);
  124.     printf("\n");
  125.     InOrder(T);
  126.     printf("\n");
  127.     PostOrder(T);
  128.     printf("\n");
  129.     PreOrder_Nonrecursive(T);
  130.     printf("\n");
  131.     InOrder_Nonrecursive(T);
  132.     printf("\n");
  133.     PostOrder_Nonrecursive(T);
  134.     printf("\n");
  135.     return 0;
  136. }

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