-
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;
}
Następne wpisy z tego wątku
Najnowsze wątki z tej grupy
- Do czego nadaje się QDockWidget z bibl. Qt?
- Bibl. Qt jest sztucznie ograniczona - jest nieprzydatna do celów komercyjnych
- Co sciaga kretynow
- AEiC 2024 - Ada-Europe conference - Deadlines Approaching
- Jakie są dobre zasady programowania programów opartych na wtyczkach?
- sprawdzanie słów kluczowych dot. zła
- Re: W czym sie teraz pisze programy??
- Re: (PDF) Surgical Pathology of Non-neoplastic Gastrointestinal Diseases by Lizhi Zhang
- CfC 28th Ada-Europe Int. Conf. Reliable Software Technologies
- Młodzi programiści i tajna policja
- Ada 2022 Language Reference Manual to be Published by Springer
- Press Release - AEiC 2023, Ada-Europe Reliable Softw. Technol.
- Ada-Europe - AEiC 2023 early registration deadline approaching
- Ada-Europe Int.Conf. Reliable Software Technologies, AEiC 2023
- Ile cykli zajmuje mnożenie liczb 64-bitowych?
Najnowsze wątki
- 2024-05-10 Warszawa => Konsultant ds. cyberbezpieczeństwa <=
- 2024-05-10 Warszawa => Software .Net Developer <=
- 2024-05-10 Warszawa => Programista Full Stack .Net <=
- 2024-05-10 Warszawa => Interactive/Experience Designer <=
- 2024-05-10 Wrocław => Consultant/Implementer Comarch ERP XL <=
- 2024-05-09 Chess
- 2024-05-09 Vitruvian Man - parts 7-11a
- 2024-05-09 Drukara laserowa
- 2024-05-09 Chess
- 2024-05-09 sedzia Szmydt
- 2024-05-09 Chess
- 2024-05-09 [newbie] Jaki multimetr za 2-4 stówy?
- 2024-05-09 Chcą poł. tunelem Europę z Afryką - 27km za 6GEUR
- 2024-05-09 Gorzów Wielkopolski => Konsultant/Wdrożeniowiec Comarch ERP XL <=
- 2024-05-09 Kraków => Senior PHP Developer (Symfony) <=