-
Data: 2016-05-07 13:13:52
Temat: Re: Szukanie najdłuższego ciągu w drzewie
Od: Borneq <b...@a...hidden.pl> szukaj wiadomości tego autora
[ pokaż wszystkie 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-20 Fiat 125p wer. pikup - w PRL moszna było, w III Reczy [pospolitej] nie moszna
- 2024-05-19 Pożar salonu z chińskimi elektrykami
- 2024-05-18 LED
- 2024-05-19 ceny nieruchomości
- 2024-05-18 Szczecin => UX/UI Designer <=
- 2024-05-18 Warszawa => Mid PHP Developer (Laravel) <=
- 2024-05-18 Warszawa => Software .Net Developer <=
- 2024-05-18 Warszawa => Mid/Senior QA Engineer <=
- 2024-05-18 Ulm => Solution Architect (sichere Kommunikation und IoT-Loesungen <=
- 2024-05-18 Katowice => Head of Virtualization Platform Management and Operating S
- 2024-05-18 Warszawa => SAP WM Consultant / Execution <=
- 2024-05-18 Wrocław => Consultant/Implementer Comarch ERP XL <=
- 2024-05-18 Gdańsk => Head of International Freight Forwarding Department <=
- 2024-05-18 Warszawa => Account Manager (Recruitment Services) <=
- 2024-05-18 Łódź => Salesperson - CRM Systems <=