#include <iostream>
#include <cmath>
#include <string>
using namespace std;
const int F = 3;
struct node{
    string s;
    node *parent, *child[F];
    int val;
};
struct tree {
    node head;
    tree(string* s)
    {
        head.s = s[0];
        head.val = 0;
        head.parent = NULL;
        for (int i = 0; i < F; i++)
        {
            head.child[i] = add(s[i + 1], head.val+1);
            (head.child[i])->parent = &head;
        }
    }
    node *add(string s,int val)
    {
        node *p = new node;
        for (int i = 0; i < F; i++)
            p->child[i] = NULL;
        p -> s = s;
        p->val = val;
        return p;
    }
    void deltree(node * p)
    {
        
        for (int i = 0; i < F; i++)
        {
            if (p->child[i] != NULL)
            {
                deltree(p->child[i]);
                p->child[i] = NULL;
            }
        }
        cout << \"delete\" << p->val << \":\" << p->s << endl;
        delete p;
    }
    void display()
    {
        show(&(head));

    }
    void delt()
    {
        for(int i=0;i<F;i++)
        deltree(head.child[i]);
        cout << \"success delete\" << endl;
    }
    void show(node *p)
    {
        cout << \" \" << p->val << \":\" << p->s << endl;
        for (int i = 0; i < F; i++)
        {
            if(p->child[i]!=NULL)
            show(p->child[i]);
        }
    }
};
int main()
{
    string s[4]= { \"banana\",\"apple\",\"pear\",\"melon\" };
    tree r(s);
    r.display();
    r.delt();
    system(\"pause\");
    return 0;
}

收藏 打印