Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void printLevelOrder (node* head) {
- queue <node*> black;
- queue <int> niveau;
- int hoeheMax = 0;
- if (head->right != nullptr) {
- black.push(head->right);
- niveau.push(0);
- }
- while (!(black.empty())) {
- node* tmp = black.front(); // Aktueller Knoten ist immer das vorderste Element der FIFO Queue "black"
- black.pop();
- int aktNiveau = niveau.front(); // Aktuelles Niveau ist immer das vorderste Element der FIFO Queue "Niveau"
- niveau.pop();
- if (hoeheMax < aktNiveau) {
- hoeheMax = aktNiveau;
- }
- cout << "(";
- if (tmp->left->red == true) { // Rote Knoten kommen in gar keine Queue sondern werden einfach nur ausgegeben
- cout << tmp->left->item;
- }
- cout << ", "<< tmp->item; // Schwarzer Knoten kommt in die Mitte
- if (tmp->right->red == true) {
- cout << "," <<tmp->right->item;
- }
- cout << ")";
- if (tmp->right != nullptr ) {
- if (tmp->right->red == false) {
- black.push(tmp->right); // Nachfolger rechts ist ein schwarz Knoten => direkt pushen
- niveau.push(aktNiveau +1); // Niveau des Knoten ist immer 1 höher als das Niveau des Derzeitigem
- }
- else { //Nachfolger rechts ist ein Rot
- if (tmp->right->left != nullptr) { // Nachfolger überspringen und links nach einem Knoten suchen
- black.push(tmp->right->left);
- niveau.push(aktNiveau + 1);
- }
- if (tmp->right->right != nullptr) { // Das gleiche für rechts
- black.push(tmp->right->right);
- niveau.push(aktNiveau + 1);
- }
- }
- }
- // ... Das gleiche nochmal für tmp->left ...
- cout << endl; // Zeilenumbruch bei Niveauwechsel
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement