sidrs

DSAL_AS Ass 2

Jan 19th, 2025
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Node {
  5. public:
  6.     int data;
  7.     Node *left;
  8.     Node *right;
  9. };
  10.  
  11. class BST {
  12. private:   
  13.     Node *root;
  14. public:
  15.     BST() {
  16.         root = nullptr;
  17.     }
  18.  
  19.     void createTree() {
  20.  
  21.         Node *temp, *curr;
  22.    
  23.         do {
  24.             temp = new Node;
  25.             cout << "Enter data: "; cin >> temp->data;
  26.             temp->left = temp->right = nullptr;
  27.            
  28.             if (root == nullptr) {
  29.                 root = temp;   
  30.             } else {
  31.                 curr = root;
  32.                 while (1) {
  33.                     // Left
  34.                     if (temp->data < curr->data) {
  35.                         if (curr->left == nullptr) {
  36.                             curr->left = temp; 
  37.                             break;
  38.                         } else {
  39.                             curr = curr->left; 
  40.                         }
  41.                     }
  42.                     // Right
  43.                     if (temp->data >= curr->data) {
  44.                         if (curr->right == nullptr) {
  45.                             curr->right = temp;
  46.                             break;
  47.                         } else {
  48.                             curr = curr->right;
  49.                         }
  50.                     }
  51.                 }
  52.  
  53.             }
  54.  
  55.             int flag;
  56.             cout << "Want to enter a new node? (0 for NO): "; cin >> flag;
  57.             if (flag == 0) break;
  58.  
  59.         } while (true);
  60.  
  61.     }
  62.  
  63.     void preorder(Node *root) {
  64.         if (root == nullptr) return;
  65.  
  66.         cout << root->data << "\t";
  67.         preorder(root->left);
  68.         preorder(root->right);
  69.     }
  70.  
  71.     void postorder(Node *root) {
  72.         if (root == nullptr) return;   
  73.  
  74.         preorder(root->left);
  75.         preorder(root->right);
  76.         cout << root->data << "\t";
  77.     }
  78.  
  79.     void inorder(Node *root) {
  80.         if (root == nullptr) return;
  81.         preorder(root->left);
  82.         cout << root->data << "\t";
  83.         preorder(root->right);
  84.     }
  85.  
  86.     void displayTree() {
  87.         if (root == nullptr) {
  88.             cout << "Tree empty" << endl;
  89.         } else {
  90.             cout << "Display" << endl;
  91.             int choice;
  92.             cout << "1. Postorder" << endl;
  93.             cout << "2. Inorder" << endl;
  94.             cout << "3. Preorder" << endl;
  95.             cout << "4. All" << endl;
  96.             cout << "Enter choice: " << endl; cin >> choice;
  97.    
  98.             if (choice == 1) {
  99.                 cout << "Postorder Traversal: " << endl;
  100.                 postorder(root);
  101.             cout << endl;
  102.             } else if (choice == 2) {
  103.                 cout << "Inorder Traversal: " << endl;
  104.                 inorder(root);
  105.                 cout << endl;
  106.             } else if (choice == 3) {
  107.                 cout << "Preorder Traversal: " << endl;
  108.                 preorder(root);
  109.                 cout << endl;
  110.             } else if (choice == 4) {
  111.                 cout << "Postorder Traversal: " << endl;
  112.                 postorder(root);
  113.                 cout << endl;
  114.                 cout << "Inorder Traversal: " << endl;
  115.                 inorder(root);
  116.                 cout << endl;
  117.                 cout << "Preorder Traversal: " << endl;
  118.                 preorder(root);
  119.                 cout << endl;
  120.             } else {
  121.                 cout << "Invalid Input" << endl;
  122.             }
  123.         }
  124.     }
  125. };
  126.  
  127.  
  128. int main() {
  129.     BST Tree;
  130.     Tree.createTree();
  131.     Tree.displayTree();
  132.    
  133.     cout << "\nEnding program" << endl;
  134.     return 0;
  135. }
  136.  
  137.  
Advertisement
Add Comment
Please, Sign In to add comment