#include #include using namespace std; class Element { public: int value; Element* left; Element* right; Element* parent; Element() { value = 0; left = NULL; right = NULL; parent = NULL; } }; // wlasna lista jest szybsza od 'gotowych' rozwiazan class Lista { public: Element* value; Lista* next; Lista() { next = NULL; } }; /* // funkcja do wyswietlania drzewa void showTree(Element* element) { if(element->left) showTree(element->left); if(element->right) showTree(element->right); cout << element->value << " "; } */ int main() { char buffer[12]; // maks. dlugosc liczby 32bit z minusem i NULL na koncu int bufferPozycja = 0; char tmpChar; Element* root = new Element(); Element* currentNode = root; Lista* poczatekListy = new Lista(); poczatekListy->value = root; Lista* aktualnyLista = poczatekListy; // magiczny trick z google, zeby wczytywalo 'enter' (new line) jako znak while(cin >> noskipws >> tmpChar) { if(tmpChar == 'L') // w lewo { if(currentNode->left == NULL) { currentNode->left = new Element(); currentNode->left->parent = currentNode; aktualnyLista->next = new Lista(); aktualnyLista->next->value = currentNode->left; aktualnyLista = aktualnyLista->next; } currentNode = currentNode->left; } else if(tmpChar == 'R') // w prawo { if(currentNode->right == NULL) { currentNode->right = new Element(); currentNode->right->parent = currentNode; aktualnyLista->next = new Lista(); aktualnyLista->next->value = currentNode->right; aktualnyLista = aktualnyLista->next; } currentNode = currentNode->right; } else if(tmpChar == '\n') // nowa linia { buffer[bufferPozycja] = 0; bufferPozycja = 0; currentNode->value = atoi(buffer); currentNode = root;; } else // cyfra lub znak minus lub spacja ? { buffer[bufferPozycja++] = tmpChar; } } // zapisuje ostatnia liczbe buffer[bufferPozycja] = 0; currentNode->value = atoi(buffer); // koniec wczytywania /* //teraz mozna wyswietlic drzewo dla testu showTree(root); */ /* // albo przeleciec po elementach w kolejnosci ich wczytania i pokazac tylko liście aktualnyLista = poczatekListy; cout << endl << "LISCIE:" << endl; while(aktualnyLista) // na koncu listy element wskazuje na NULL { currentNode = aktualnyLista->value; if(!currentNode->left && !currentNode->right) // liść { cout << currentNode->value << " "; // tutaj mozna by wykonac przejscie do 'parent' w while i // policzenie kosztu zapisujac to do jakiegos max/min jesli jest 'nowy rekord' } aktualnyLista = aktualnyLista->next; } */ return 0; }