eGospodarka.pl
eGospodarka.pl poleca

eGospodarka.plGrupypl.comp.programmingSzukanie najdłuższego ciągu w drzewie › Re: Szukanie najdłuższego ciągu w drzewie
  • Path: news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!newsfeed2.atman.pl!newsfeed.
    atman.pl!.POSTED!not-for-mail
    From: Borneq <b...@a...hidden.pl>
    Newsgroups: pl.comp.programming
    Subject: Re: Szukanie najdłuższego ciągu w drzewie
    Date: Sat, 7 May 2016 13:13:52 +0200
    Organization: ATMAN - ATM S.A.
    Lines: 126
    Message-ID: <ngkilg$e9c$1@node2.news.atman.pl>
    References: <ngk077$d49$1@node2.news.atman.pl>
    <c...@g...com>
    <ngkcdb$2mt$2@node2.news.atman.pl> <ngkddo$507$1@node2.news.atman.pl>
    NNTP-Posting-Host: 91.239.205.105
    Mime-Version: 1.0
    Content-Type: text/plain; charset=iso-8859-2; format=flowed
    Content-Transfer-Encoding: 8bit
    X-Trace: node2.news.atman.pl 1462619632 14636 91.239.205.105 (7 May 2016 11:13:52
    GMT)
    X-Complaints-To: u...@a...pl
    NNTP-Posting-Date: Sat, 7 May 2016 11:13:52 +0000 (UTC)
    User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101
    Thunderbird/45.0
    In-Reply-To: <ngkddo$507$1@node2.news.atman.pl>
    Xref: news-archive.icm.edu.pl pl.comp.programming:209359
    [ ukryj nagłówki ]

    W dniu 07.05.2016 o 11:44, Borneq pisze:
    > Może redukować drzewo?
    > http://i.imgur.com/sRfoI9o.png

    Przykład nierekurencyjnego ze stosem znalezienia najlepszej/najdłuższej
    ściężki. Tylko że ten stos będzie zbytnio rósł?
    #include "stdafx.h"
    #include <stack>
    #include "exception.h"
    #include "Node.h"

    //http://i.imgur.com/7XGaVEL.png
    void makeSample0()
    {
    CNode* node0 = CNode::AddRoot("0");
    CNode* node1 = node0->Add("1");
    CNode* node2 = node0->Add("2");
    CNode* node3 = node0->Add("3");
    node1->Add("4");
    node1->Add("5");
    CNode* node6 = node2->Add("6");
    node3->Add("7");
    CNode* node8 = node3->Add("8");
    CNode* node9 = node6->Add("9");
    node8->Add("12");
    CNode* node10 = node9->Add("10");
    node9->Add("11");
    node10->Add("13");
    CNode* node14 = node10->Add("14");
    node14->Add("15");
    }


    //Traverse tree depth-first search, pre-order, stack instead of recursion
    void TreverseTree(CNode *startNode)
    {
    if (startNode == NULL) throw new Exception("root == NULL");
    stack<int> treeStack;
    CNode *node = startNode;
    int nr = 0;
    while (true)
    {
    if (node->height!=0)
    throw new Exception("error");
    node->height = treeStack.size();
    cout << node->label.c_str() << " " << node->height << endl;
    while (nr >= node->childs.size())
    {
    if (treeStack.size() == 0) return;
    nr = treeStack.top();
    treeStack.pop();
    node = node->parent;
    nr++;
    }
    treeStack.push(nr);
    node = node->childs[nr];
    nr = 0;
    }
    }

    int main()
    {
    makeSample0();
    TreverseTree(CNode::root);
    return 0;
    }


    --------------------------------
    Node.h:
    #pragma once
    #include <iostream>
    #include <string.h>
    #include <vector>

    using namespace std;

    class CNode
    {
    public:
    CNode();
    ~CNode();
    static CNode* AddRoot(string label);
    CNode* Add(string label);
    int height;
    CNode* parent;
    vector<CNode*> childs;
    static CNode* root;
    string label;
    };


    --------------------------------
    Node.cpp:
    #include "Node.h"

    CNode* CNode::root = NULL;

    CNode::CNode()
    {
    height = 0;
    }


    CNode::~CNode()
    {
    }

    CNode* CNode::AddRoot(string label)
    {
    root = new CNode();
    root->label = label;
    root->parent = NULL;
    root->height = 0;
    return root;
    }

    CNode * CNode::Add(string label)
    {
    CNode *elem = new CNode();
    elem->label = label;
    elem->parent = this;
    childs.push_back(elem);
    return elem;
    }

Podziel się

Poleć ten post znajomemu poleć

Wydrukuj ten post drukuj


Następne wpisy z tego wątku

Najnowsze wątki z tej grupy


Najnowsze wątki

Szukaj w grupach

Eksperci egospodarka.pl

1 1 1

Wpisz nazwę miasta, dla którego chcesz znaleźć jednostkę ZUS.

Wzory dokumentów

Bezpłatne wzory dokumentów i formularzy.
Wyszukaj i pobierz za darmo: