Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. void printLevelOrder (node* head) {
  2.     queue <node*> black;
  3.     queue <int> niveau;
  4.     int hoeheMax = 0;
  5.  
  6.     if (head->right != nullptr) {
  7.         black.push(head->right);
  8.         niveau.push(0);
  9.     }
  10.  
  11.     while (!(black.empty())) {
  12.         node* tmp = black.front();          // Aktueller Knoten ist immer das vorderste Element der FIFO Queue "black"
  13.         black.pop();               
  14.        
  15.         int aktNiveau = niveau.front();     // Aktuelles Niveau ist immer das vorderste Element der FIFO Queue "Niveau"
  16.         niveau.pop();
  17.        
  18.         if (hoeheMax < aktNiveau) {
  19.             hoeheMax = aktNiveau;
  20.         }
  21.  
  22.         cout << "(";
  23.         if (tmp->left->red == true) {       // Rote Knoten kommen in gar keine Queue sondern werden einfach nur ausgegeben
  24.             cout << tmp->left->item;
  25.         }
  26.        
  27.         cout << ", "<< tmp->item;           // Schwarzer Knoten kommt in die Mitte
  28.  
  29.         if (tmp->right->red == true) {
  30.             cout << "," <<tmp->right->item;
  31.         }
  32.         cout << ")";
  33.  
  34.         if (tmp->right != nullptr ) {
  35.             if (tmp->right->red == false) {
  36.                 black.push(tmp->right);         // Nachfolger rechts ist ein schwarz Knoten => direkt pushen
  37.                 niveau.push(aktNiveau +1);      // Niveau des Knoten ist immer 1 höher als das Niveau des Derzeitigem
  38.             }
  39.             else {                              //Nachfolger rechts ist ein Rot
  40.                 if (tmp->right->left != nullptr) {      // Nachfolger überspringen und links nach einem Knoten suchen
  41.                     black.push(tmp->right->left);
  42.                     niveau.push(aktNiveau + 1);
  43.                 }
  44.                 if (tmp->right->right != nullptr) {     // Das gleiche für rechts
  45.                     black.push(tmp->right->right);
  46.                     niveau.push(aktNiveau + 1);
  47.                 }
  48.             }
  49.  
  50.         }
  51.  
  52.         // ... Das gleiche nochmal für tmp->left ...
  53.    
  54.         cout << endl; // Zeilenumbruch bei Niveauwechsel
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement