Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

突然强迫症发了,贴个代码

Posted by Belldandy at 2014-01-29 19:32:21 on Problem 1577
#include <iostream>
using namespace std;

const int DEEPTH = 100;
const int WIDTH = 100;

class node
{
public:
    node(char data);
    void set_rchild(node *rchild);
    void set_lchild(node *lchild);
    char get_data();
    node **get_rchild();
    node **get_lchild();
private:
    const char _data;
    node *_lchild;
    node *_rchild;
};

class tree
{
public:
    tree();
    void insert(char data);
    ~tree();
private:
    node *_head;
    void _delete_one(node *pointer);
    void _do_insert(node **pointer, char data);
    void _do_print(node *pointer);
};

node::node(char data): _data(data), _lchild(NULL), _rchild(NULL) {};

void node::set_lchild(node *lchild)
{
    _lchild = lchild;
}

void node::set_rchild(node *rchild)
{
    _rchild = rchild;
}

char node::get_data()
{
    return _data;
}

node **node::get_rchild()
{
    return & _rchild;
}

node **node::get_lchild()
{
    return & _lchild;
}

tree::tree(): _head(NULL) {};

void tree::insert(char data)
{
    _do_insert(&_head, data);
}

void tree::_do_insert(node **pointer, char data)
{
    if (NULL == *pointer)
    {
        *pointer = new node(data);
    }
    else
    {
        if ((*pointer)->get_data() > data)
        {
            _do_insert((*pointer)->get_lchild(), data);
        }
        else
        {
            _do_insert((*pointer)->get_rchild(), data);
        }
    }
    return;
}

void tree::_do_print(node *pointer)
{
    if ( NULL != pointer)
    {
        cout<<pointer->get_data();
        _do_print(*(pointer->get_lchild()));
        _do_print(*(pointer->get_rchild()));
        delete pointer;
    }
    return;
}

tree::~tree()
{
    _do_print(_head);
    cout<<endl;
}

int main(int argc,char ** argv)
{
    char data[DEEPTH][WIDTH] = {0};
    int deepth = 0, i = 0;
    while ('$' != data[deepth][0])
    {
        deepth = 0;
        while (cin>>data[deepth] && !(( '*' == data[deepth][0] || '$' == data[deepth][0]) && 0 == data[deepth][1]))
        {
            ++deepth;
        }
        tree *t = new tree();
        for (int j = deepth - 1; j >= 0; --j)
        {
            for (i = 0; data[j][i]; ++i)
            {
                t->insert(data[j][i]);
            }
        }
        delete t;
    }
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator