zoukankan      html  css  js  c++  java
  • 数据结构自己实现——Tree and Forest

    //中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;
    }
  • 相关阅读:
    判断activity是否显示在界面上
    限制EditText的输入字数
    安卓自定义类似TabHost的导航栏
    安卓中加载布局文件的三种方法
    绘图——Android绘图基础:Canvas、Paint等
    使用简单图片
    使用原始资源
    MySQL分表(Partition)学习研究报告
    Docker基础知识介绍
    Python开发系列
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3435831.html
Copyright © 2011-2022 走看看