qingcheng奕  
//中D序??遍???历???二t叉?树???
//先??序??遍???历???二t叉?树???
//后??序??遍???历???二t叉?树???
#include <iostream>
using namespace std;

typedef char DataType;
struct BiNode{
                DataType data;
                 struct BiNode *lchild,*rchild;
};
void inOrder(BiNode *p)
{
                 if(p!=NULL)
                {
                                inOrder(p->lchild);
                                cout<<p->data;
                                inOrder(p->rchild);
                }
}
void preOrder(BiNode *p)
{
                 if(p!=NULL)
                {
                                cout<<p->data;
                                preOrder(p->lchild);
                                preOrder(p->rchild);
                }
}
void postOrder(BiNode *p)
{
                 if(p!=NULL)
                {
                                
                                postOrder(p->lchild);
                                postOrder(p->rchild);
                                cout<<p->data;
                }
}
int main()
{
                BiNode *p = (BiNode*)malloc( sizeof(BiNode)) ;p->data = '+' ;
                BiNode *p1 = (BiNode*)malloc( sizeof(BiNode)),*p2 = (BiNode*)malloc(sizeof (BiNode));
 
                p1->data = '-';p->lchild = p1;p2->data = '*' ;p->rchild = p2;
                BiNode *p3 = (BiNode*)malloc( sizeof(BiNode)),*p4 = (BiNode*)malloc(sizeof (BiNode));
 
                p3->data = 'a';p1->lchild = p3;p4->data = '/';p1->rchild = p4;
                BiNode *p5 = (BiNode*)malloc( sizeof(BiNode)),*p6 = (BiNode*)malloc(sizeof (BiNode));
 
                p5->data = '+';p2->lchild = p5;p6->data = 'f';p2->rchild = p6;
                BiNode *p7 = (BiNode*)malloc( sizeof(BiNode)),*p8 = (BiNode*)malloc(sizeof (BiNode));
 
                p7->data = 'b';p4->lchild = p7;p8->data = 'c';p4->rchild = p8;
                BiNode *p9 = (BiNode*)malloc( sizeof(BiNode)),*p10 =(BiNode*)malloc(sizeof (BiNode));
                
                p9->data = 'd';p5->lchild = p9;p10->data = 'e';p5->rchild = p10;
                p3->lchild =NULL; p3->rchild =NULL;
                p7->lchild =0; p7->rchild =0;
                p8->lchild = 0; p8->rchild = 0;
                p9->lchild = 0; p9->rchild = 0;
                p10->lchild = 0; p10->rchild =0;
                p6->lchild =0; p6->rchild =0;

                inOrder(p);
                cout<<endl;
                preOrder(p);
                cout<<endl;
                postOrder(p);
                 return 0;
}
//实???现?森?-林??的??Find和??Union
#include <iostream>
using namespace std;

#define MAX_PARENT 100
int parent[MAX_PARENT];
void Initialize(int n)
{
                 int e;
                 for(e=1;e<=n&&e<=MAX_PARENT;e++)
                {
                                parent[e] = 0 ;
                }
}
int Find(int e)
{
                 while(parent[e] && e<MAX_PARENT)
                                e = parent[e];
                 return e;
}
void Union(int i,int j)
{
                 if(j<MAX_PARENT)
                                parent[j] = i ;
}

void Combine(int a,int b)
{
                 int i = Find(a);
                 int j = Find(b);
                 if(i!=j)
                                Union(i,j);
}
int main()
{
                Combine(1,2);
                 return 0;
}

 

posted on 2013-11-21 16:33  qingcheng奕  阅读(341)  评论(0编辑  收藏  举报