Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Element {
  6.     int value = 0;
  7.    
  8.     bool IsRed = false;
  9.    
  10.     Element* nextl = NULL;
  11.     Element* nextr = NULL;
  12.     Element* previous = NULL;
  13. };
  14.  
  15. void Walk(Element *&Way, int value) {
  16.     Element* Current = Way;
  17.     do {
  18.         if((Current->nextr == NULL && Current->value < value) || (Current->nextl == NULL && Current->value > value))
  19.             break;
  20.        
  21.         if (Current->value < value)
  22.             Current = Current->nextr;
  23.        
  24.         if(Current->value > value)
  25.             Current = Current->nextl;
  26.     }while(true);
  27.     Way = Current;
  28. }
  29.  
  30. void SetElement (int value, Element* root){
  31.     Element* newElement = new Element;
  32.     newElement->value = value;
  33.     Element* tail = root;
  34.    
  35.     Walk(tail, value);
  36.    
  37.     if(tail->value < value) {
  38.         tail->nextr = newElement;
  39.         newElement->previous = tail;
  40.     }
  41.     if(tail->value > value) {
  42.         tail->nextl = newElement;
  43.         newElement->previous = tail;
  44.     }
  45. }
  46.  
  47. void TreeOut(Element* root) {
  48.     if(root == NULL)
  49.         return;
  50.     cout << root -> value <<"  ";
  51.     TreeOut(root->nextl);
  52.     TreeOut(root->nextr);
  53.    
  54. }
  55.  
  56. int main()
  57. {
  58.    
  59.     int value = 0;
  60.    
  61.     Element* root = new Element;
  62.    
  63.     cin >> value;
  64.     cout <<endl;
  65.    
  66.     root->value = value;
  67.    
  68.    
  69.    
  70.     do {
  71.         cin >> value;
  72.         if(value == 95)
  73.             break;
  74.         SetElement(value, root);
  75.     } while(true);
  76.    
  77.     TreeOut(root);
  78.    
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement