- // 数据结构.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- typedef char ElemType;
- typedef struct Lnode
- {
- struct Bitnode* Tree;//啊
- struct Lnode*next;
- }Lnode,*linkstack;//栈
- typedef struct Bitnode
- {
- ElemType data;
- struct Bitnode *lchild,*rchild;
- }Bitnode,*BitTree;//树
- /******栈*********
- _______________*/
- int initstack(linkstack &s)//建立空栈
- {
- s=new Lnode;
- s->next=NULL;//#
- return 1;
- }
- int Empty_stack(linkstack s)
- {if (s->next==NULL)return 1;
- else return 0;
- }
- int push(linkstack s,BitTree T)
- {linkstack p;
- p=new Lnode;
- if(!p){cout<<"分配内存失败"<<endl;return 0;}
- p->Tree=T;
- p->next=s->next;//##
- s->next=p;
- return 1;
- }
- int pop(linkstack s,BitTree &p)//这样用pop(,);
- {linkstack temp;
- if(Empty_stack(s)){cout<<"栈空了"<<endl;return 0;}
- temp=s->next;
- p=temp->Tree;
- s->next=temp->next;//他妈的错了
- free(temp);
- return 1;
- }
- int get_top(linkstack s,BitTree &p)
- {if(s->next==NULL){return 0;}
- else {p=s->next->Tree;return 1;}
- }
- void l_display(linkstack s)
- {while(s->next!=NULL)
- {s=s->next;
- if(s->Tree!=NULL)
- cout<<s->Tree->data;
- }
- }
- /******树********/
- int CreatB_Tree(BitTree &T)
- {
- ElemType ch;
- ch=getchar();//
- if(ch==' ')T=NULL;
- else
- {if(!(T=new Bitnode))cout<<"Error!"<<endl;
- T->data=ch;
- CreatB_Tree(T->lchild);
- CreatB_Tree(T->rchild);
- }
- return 1;
- }
- /*************非递归 *******先序***************/
- int Pr_Order_B_Tree(BitTree T)
- { BitTree p;
- linkstack s;
- initstack(s);
- push(s,T); //根节点入栈
- while(!Empty_stack(s))
- {pop(s,p);
- while(p!=NULL)
- {cout<<p->data<<" ";
- if(p->lchild)push(s,p->rchild);
- p=p->lchild;
- }
- }
- cout<<endl;
- return 1;
- }
- //这个易于理解
- int In_Order_B_Tree(BitTree T)
- {
- BitTree p;
- linkstack s;
- initstack(s);
- p=T;
- while(p||!Empty_stack(s))
- {while(p)
- {push(s,p);
- p=p->lchild;
- }/*******因为 push在前p——>next在后 因此空不入栈**************/
- pop(s,p);
- cout<<p->data<<" "; //因此不用 判断空否
- p=p->rchild;
- }
- cout<<endl;
- return 1;
- }
- int Po_Order_B_Tree(BitTree T)
- {
- BitTree p,q;
- linkstack s;
- initstack(s);
- p=T;q=NULL;
- while(p||!Empty_stack(s))
- {if(p!=q)
- {while(p!=NULL)
- {push(s,p);
- if(p->lchild)p=p->lchild;
- else p=p->rchild;
- }
- }
- if(Empty_stack(s))break;
- get_top(s,q);
- if(q->rchild==p)
- {pop(s,p);
- cout<<p->data<<" ";
- }
- else p=q->rchild;
- }
- cout<<endl;
- return 1;
- }
- int InOrder_B_Tree(BitTree T)//中序方法二
- {
- BitTree p;
- linkstack s;
- initstack(s);
- push(s,T); //根节点入栈
- while(!Empty_stack(s))
- { while(get_top(s,p)&&p)push(s,p->lchild);//
- pop(s,p);//空//
- if(!Empty_stack(s))
- {pop(s,p);
- if(p!=NULL)cout<<p->data<<" ";
- push(s,p->rchild);
- }
- }cout<<endl;
- return 1;
- }
- int main()
- {
- BitTree T;
- CreatB_Tree(T);
- /***************practice*********
- *********************************/
- Pr_Order_B_Tree(T);
- In_Order_B_Tree(T);
- InOrder_B_Tree(T);
- Po_Order_B_Tree(T);
- /****************** ************************/
- int m;
- cin>>m;
- return 1;
- }
栈 树
最新推荐文章于 2024-08-14 15:04:34 发布