Aktív témák

  • Miracle

    senior tag

    válasz silentBob #54 üzenetére

    1: NEM KELL TOBB HELYEN FELTENNI A KERDEST, ES VEGKEPP NEM KELL MINDEN HULYESEGNEK TOPICOT NYITNI, MERT ROHADT IDEGESITO!!!!
    itt van 1 trinaris fa postorder bejarassal, szep kiiro es beolvas operatorokkal, a step fuggvenyt kell atirni h inorder legyen, csak ki kell cserelni 6 sor sorrendjet, de ha mar tudsz topicot nyitni nyilvan ez is menni fog.


    //---------------------------------------------------------------------------
    // Tree
    //---------------------------------------------------------------------------
    #ifndef TREE_H
        #define TREE_H

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <map>
    #include <set>




    template<class eT1>
    struct tree_element
    {
        tree_element()
                {
                p_dat = new eT1();
                p_left = 0;
                p_mid = 0;
                p_right = 0;
                p_parent = 0;
                }
        tree_element(const eT1& t1,
                     tree_element* l,
                     tree_element* m,
                     tree_element* r,
                     tree_element* p)
                {
                        p_dat = new eT1(t1);
                        p_left = l;
                        p_mid = m;
                        p_right = r;
                        p_parent = p;
                }
        ~tree_element()
                {
                       &nbsp.|. p_dat;
                       &nbsp.|. p_left;
                       &nbsp.|. p_right;
                       &nbsp.|. p_mid;
                }
        tree_element<eT1> *p_left, *p_mid, *p_right, *p_parent;
        eT1* p_dat;
    };

    template<class cT1> class c_tree
    {
    public:
        c_tree()            :cap(0) {p_top = p_act = new tree_element<cT1>();}
        ~c_tree()           {delete p_top;}

        void reset()        {p_act = p_top;}
        void empty()        {delete p_top; p_top = p_act = new tree_element<cT1>();}
    /**********   FELADAT MEGOLDÁSA    ****************************************/
    /**/void step();        //postorder
    /**/cT1* get_first_repeating();//ez a feladat
    /***************************************************************************/

        bool read_tree(std :: istream& is, std :: ostream& os = std :: cout);
        void print_tree(std :: ostream& os);
    //Teszteléshez használt függvények
        void set_act(cT1& const t1)  {*p_actual -> p_dat = t1;}
        int get_capacity()  {return cap;}
        tree_element<cT1>* get_top()    {return p_top;}
        tree_element<cT1>* get_act()    {return p_act;}
        cT1* get_data_adr()  {return p_act -> p_dat;}
        cT1 get_data()       {return *get_data_adr();}
    private:
        void read_branch(tree_element<cT1> * p_a, int c, std :: istream& is, std :: ostream& os);
        void print_branch(tree_element<cT1> *p_a, int c, std :: ostream& os, bool other_one = false);
        tree_element<cT1> *p_top;
        tree_element<cT1> *p_act;
        int cap;
        std :: map<int, bool> blocked_columns;
    };

    template<class fT1>
    std::istream& operator>> (std::istream& is, c_tree<fT1>& t);

    template<class fT1>
    std::istream& operator>> (std::istream& is, c_tree<fT1>& t)
    {

        t.read_tree(is);
        return is;
    }

    template<class fT1>
    std::ostream& operator<< (std::ostream& os, c_tree<fT1>& t);

    template<class fT1>
    std::ostream& operator<< (std::ostream& os, c_tree<fT1>& t)
    {

        t.print_tree(os);
        return os;
    }


    template <class fT1>
    void c_tree<fT1> :: read_branch(tree_element<fT1> * p_a, int c, std :: istream& is, std :: ostream& os)
    {
        fT1 temp;
        std :: string str_temp;
        std :: istringstream isstr;
        tree_element<fT1> *null_pointer = 0;

        for(int i = 1 ; i < c ; ++i)  os << ' ' << ((blocked_columns) ? ' ' : char(179)) << ' ' ;
        os << ' ' << char(195) << char(196) << char(196) << char(194) << char(196);
        is >> str_temp;

        if (str_temp != ''x'')
        {
                blocked_columns[c]=false;
                isstr.str(str_temp);
                isstr >> temp;
                p_a -> p_left = new tree_element<fT1>(temp, null_pointer, null_pointer, null_pointer, p_a);
                read_branch(p_a -> p_left, c+1, is, os);
                ++cap;
        }

        str_temp = '''';
        isstr.clear();

        for(int i = 1 ; i < c ; ++i)  os << ' ' << ((blocked_columns
    ) ? ' ' : char(179)) << ' ' ;
        os << ' ' << char(195) << char(196) << char(196) << char(194) << char(196);
        is >> str_temp;
        if (str_temp != ''x'')
        {
                isstr.str(str_temp);
                isstr >> temp;
                p_a -> p_mid = new tree_element<fT1>(temp, null_pointer, null_pointer, null_pointer, p_a);
                read_branch(p_a -> p_mid, c+1, is, os);
                ++cap;
        }

        str_temp = '''';
        isstr.clear();

        for(int i = 1 ; i < c ; ++i)  os << ' ' << ((blocked_columns) ? ' ' : char(179)) << ' ' ;
        os << ' ' << char(192) << char(196) << char(196) << char(194) << char(196);
        is >> str_temp;
        if (str_temp != ''x'')
        {
                blocked_columns[c]=true;
                isstr.str(str_temp);
                isstr >> temp;
                p_a -> p_right = new tree_element<fT1>(temp, null_pointer, null_pointer, null_pointer, p_a);
                read_branch(p_a -> p_right, c+1, is, os);
                ++cap;
        }
    }

    template <class fT1>
    bool c_tree<fT1> :: read_tree(std :: istream& is, std :: ostream& os)
    {
        for(int i = 0; i < 3000; ++i)  blocked_columns
     = true;
        fT1 temp;
        os << ''Kerem az adatokat\n'';
        os << char(196) << char(194) << char(196);
        is >> temp;
        p_top -> p_dat = new fT1(temp);
        read_branch(p_top,1, is, os);
        return true;
    }

    template <class fT1>
    void c_tree<fT1> :: print_branch(tree_element<fT1> *p_a, int c, std :: ostream& os, bool other_one)
    {
        for(int i = 1 ; i < c; ++i) os << ' ' << ((blocked_columns) ? ' ' : char(179)) << ' ';
        os << ' ' << char (other_one ? 195 : 192)  << char(196) << char(196) << char(( p_a->p_left != 0 || p_a -> p_right != 0) ? 194 : 196)<< char(196)   << *p_a -> p_dat << endl;
        if (p_a -> p_mid != 0)
        {
                if(p_a -> p_left != 0 && p_a -> p_right != 0)
                {
                        blocked_columns[c+1] = false;
                        print_branch(p_a -> p_left, c+1, os, true);
                        print_branch(p_a -> p_mid, c+1, os, true);
                        blocked_columns[c+1] = true;
                        print_branch(p_a -> p_right, c+1, os, false);
                }
                if(p_a -> p_left != 0 && p_a -> p_right == 0)
                {
                        blocked_columns[c+1] = false;
                        print_branch(p_a -> p_left, c+1, os, true);
                        blocked_columns[c+1] = true;
                        print_branch(p_a -> p_mid, c+1, os, false);
                }
                if(p_a -> p_left == 0 && p_a -> p_right != 0)
                {
                        blocked_columns[c+1] = false;
                        print_branch(p_a -> p_mid, c+1, os, true);
                        blocked_columns[c+1] = true;
                        print_branch(p_a -> p_right, c+1, os, false);

                }
        }
        else //(p_a -> p_left == 0 && p_a -> p_right == 0)
        {
                if(p_a -> p_left != 0 && p_a -> p_right != 0)
                {
                        blocked_columns[c+1] = false;
                        print_branch(p_a -> p_left, c+1, os, true);
                        blocked_columns[c+1] = true;
                        print_branch(p_a -> p_right, c+1, os, false);
                }
                if(p_a -> p_left != 0 && p_a -> p_right == 0)
                {
                        blocked_columns[c+1] = true;
                        print_branch(p_a -> p_left, c+1, os, false);
                }
                if(p_a -> p_left == 0 && p_a -> p_right != 0)
                {
                        blocked_columns[c+1] = true;
                        print_branch(p_a -> p_right, c+1, os, false);
                }
        }


    }

    template <class fT1>
    void c_tree<fT1> :: print_tree(std :: ostream& os)
    {
        for(int i = 0; i < 3000; ++i)  blocked_columns
     =true;
        print_branch(p_top, 1, os, false);
    }

    template <class fT1>
    void c_tree<fT1> :: step()
    {
        if (p_act == p_top)
        {
                while(p_act -> p_left || p_act -> p_mid || p_act -> p_right  )
                {
                        if(p_act -> p_left)
                        {
                                p_act = p_act -> p_left;
                                continue;
                        }
                        if(p_act -> p_mid)
                        {
                                p_act = p_act -> p_mid;
                                continue;
                        }
                        if(p_act -> p_right)
                        {
                                p_act = p_act -> p_right;
                                continue;
                        }

                }
        }
        else //(p_act != p_top)
        {
                if (p_act == p_act -> p_parent -> p_left)
                        p_act = ((p_act -> p_parent -> p_mid) ? (p_act -> p_parent -> p_mid) : ((p_act -> p_parent -> p_right) ? p_act -> p_parent -> p_right : p_act -> p_parent));
                else if (p_act == p_act -> p_parent -> p_mid)
                        p_act = (p_act -> p_parent -> p_right ? p_act -> p_parent -> p_right : p_act -> p_parent);
                else if (p_act == p_act -> p_parent -> p_right)
                        p_act = p_act -> p_parent;
        }
    }

    template <class fT1>
    fT1* c_tree<fT1> :: get_first_repeating()
    {
        std :: set<fT1> once_found;
        typedef std :: set<fT1>::const_iterator SCI;
        SCI temp  = once_found.end();

        reset();
        step();

        while(p_act != p_top)
        {
                temp = once_found.find(*get_data_adr());
                if (temp == once_found.end())
                        once_found.insert(*get_data_adr());
                else
                        return get_data_adr();
                step();
        }
        return 0;
    }
    #endif // Unit1_H

Aktív témák