栈 树

来源:互联网 发布:java 字符串包含汉字 编辑:程序博客网 时间:2024/05/21 04:02
Code:
  1. // 数据结构.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3. #include "stdafx.h"  
  4. #include <iostream>  
  5. using namespace std;  
  6. typedef char ElemType;  
  7.   
  8.   
  9. typedef struct Lnode  
  10.   {  
  11.     struct Bitnode* Tree;//啊   
  12.     struct Lnode*next;  
  13.    }Lnode,*linkstack;//栈  
  14. typedef struct Bitnode  
  15.   {  
  16.      ElemType data;  
  17.     struct Bitnode *lchild,*rchild;  
  18.   }Bitnode,*BitTree;//树  
  19.   
  20.   
  21. /******栈********* 
  22. _______________*/  
  23. int initstack(linkstack &s)//建立空栈  
  24. {  
  25.     s=new Lnode;  
  26.     s->next=NULL;//#  
  27.     return 1;  
  28.     }  
  29. int Empty_stack(linkstack s)  
  30. {if (s->next==NULL)return 1;  
  31. else return 0;  
  32. }  
  33. int push(linkstack s,BitTree T)  
  34. {linkstack p;  
  35.  p=new Lnode;  
  36.  if(!p){cout<<"分配内存失败"<<endl;return 0;}  
  37.  p->Tree=T;  
  38.  p->next=s->next;//##  
  39.  s->next=p;  
  40.  return 1;  
  41.     }  
  42. int pop(linkstack s,BitTree &p)//这样用pop(,);  
  43. {linkstack temp;  
  44. if(Empty_stack(s)){cout<<"栈空了"<<endl;return 0;}  
  45. temp=s->next;  
  46. p=temp->Tree;  
  47. s->next=temp->next;//他妈的错了  
  48. free(temp);  
  49. return 1;  
  50.     }  
  51. int get_top(linkstack s,BitTree &p)  
  52. {if(s->next==NULL){return 0;}  
  53. else {p=s->next->Tree;return 1;}  
  54. }  
  55. void l_display(linkstack s)  
  56. {while(s->next!=NULL)  
  57.   {s=s->next;  
  58. if(s->Tree!=NULL)  
  59.    cout<<s->Tree->data;  
  60.   }  
  61. }  
  62.   
  63. /******树********/  
  64. int CreatB_Tree(BitTree &T)  
  65. {  
  66.  ElemType ch;  
  67.  ch=getchar();//  
  68.  if(ch==' ')T=NULL;  
  69. else  
  70.  {if(!(T=new Bitnode))cout<<"Error!"<<endl;  
  71.   T->data=ch;  
  72.   CreatB_Tree(T->lchild);  
  73.   CreatB_Tree(T->rchild);  
  74.   }  
  75. return 1;  
  76. }  
  77.   
  78. /*************非递归 *******先序***************/  
  79. int Pr_Order_B_Tree(BitTree T)  
  80. {  BitTree p;  
  81.     linkstack s;  
  82.     initstack(s);     
  83.     push(s,T);  //根节点入栈   
  84.     while(!Empty_stack(s))  
  85.     {pop(s,p);  
  86.      while(p!=NULL)  
  87.      {cout<<p->data<<" ";  
  88.      if(p->lchild)push(s,p->rchild);  
  89.      p=p->lchild;  
  90.      }  
  91.     }  
  92. cout<<endl;  
  93. return 1;  
  94. }  
  95. //这个易于理解  
  96. int In_Order_B_Tree(BitTree T)  
  97. {  
  98.     BitTree p;  
  99.     linkstack s;  
  100.     initstack(s);  
  101.   
  102.     p=T;  
  103.     while(p||!Empty_stack(s))  
  104.     {while(p)  
  105.      {push(s,p);  
  106.       p=p->lchild;  
  107.      }/*******因为 push在前p——>next在后 因此空不入栈**************/  
  108.     pop(s,p);  
  109.     cout<<p->data<<" "//因此不用 判断空否  
  110.     p=p->rchild;  
  111.        
  112.        
  113.     }  
  114.     cout<<endl;  
  115.     return 1;  
  116. }  
  117. int Po_Order_B_Tree(BitTree T)  
  118. {  
  119.     BitTree p,q;  
  120.     linkstack s;  
  121.     initstack(s);  
  122.   
  123.     p=T;q=NULL;  
  124.  while(p||!Empty_stack(s))       
  125.  {if(p!=q)  
  126.   {while(p!=NULL)    
  127.      {push(s,p);  
  128.        if(p->lchild)p=p->lchild;  
  129.        else p=p->rchild;  
  130.       }  
  131.  }  
  132. if(Empty_stack(s))break;  
  133. get_top(s,q);  
  134. if(q->rchild==p)  
  135.     {pop(s,p);  
  136. cout<<p->data<<" ";  
  137.   
  138.     }  
  139. else p=q->rchild;  
  140.      }  
  141.  cout<<endl;  
  142. return 1;  
  143. }  
  144. int InOrder_B_Tree(BitTree T)//中序方法二  
  145. {    
  146.     BitTree p;  
  147.     linkstack s;  
  148.     initstack(s);  
  149.       
  150.     push(s,T);  //根节点入栈   
  151.   
  152. while(!Empty_stack(s))  
  153. {        while(get_top(s,p)&&p)push(s,p->lchild);//  
  154.                     pop(s,p);//空//  
  155.   
  156.   
  157.           if(!Empty_stack(s))  
  158.           {pop(s,p);  
  159.           if(p!=NULL)cout<<p->data<<" ";  
  160.           push(s,p->rchild);  
  161.           }         
  162. }cout<<endl;  
  163. return 1;  
  164.     }  
  165. int main()  
  166. {  
  167.      
  168.   
  169.    BitTree T;  
  170.    CreatB_Tree(T);  
  171.    /***************practice********* 
  172.    *********************************/  
  173.   Pr_Order_B_Tree(T);  
  174.   In_Order_B_Tree(T);  
  175.   InOrder_B_Tree(T);  
  176.   Po_Order_B_Tree(T);  
  177.     /******************  ************************/  
  178.   
  179.      
  180. int m;  
  181. cin>>m;  
  182.  return 1;  
  183. }  
  184.    
原创粉丝点击