Advertisement
JonathanA007

235150301111015-Tugas8-GraphBFSnDFS

Jun 1st, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | Source Code | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5.     int key;
  6.     struct Node *left, *right;
  7. };
  8.  
  9.  
  10. Node *newNode(int item) {
  11.     Node *temp = new Node();
  12.     temp->key = item;
  13.     temp->left = temp->right = nullptr;
  14.     return temp;
  15. }
  16.  
  17.  
  18. void inorder(Node *root) {
  19.     if (root != nullptr) {
  20.         inorder(root->left);
  21.         cout << root->key << " ";
  22.         inorder(root->right);
  23.     }
  24. }
  25.  
  26.  
  27. void reverseInorder(Node *root) {
  28.     if (root != nullptr) {
  29.         reverseInorder(root->right);
  30.         cout << root->key << " ";
  31.         reverseInorder(root->left);
  32.     }
  33. }
  34.  
  35.  
  36. Node* insert(Node* node, int key) {
  37.     if (node == nullptr) return newNode(key);
  38.     if (key < node->key)
  39.         node->left = insert(node->left, key);
  40.     else if (key > node->key)
  41.         node->right = insert(node->right, key);
  42.  
  43.  
  44.     return node;
  45. }
  46.  
  47. Node* minValueNode(Node* node) {
  48.     Node* current = node;
  49.     while (current && current->left != nullptr)
  50.         current = current->left;
  51.  
  52.     return current;
  53. }
  54.  
  55. Node* deleteNode(Node* root, int key) {
  56.     if (root == nullptr) return root;
  57.  
  58.     if (key < root->key)
  59.         root->left = deleteNode(root->left, key);
  60.     else if (key > root->key)
  61.         root->right = deleteNode(root->right, key);
  62.  
  63.     else {
  64.         if (root->left == nullptr) {
  65.             Node *temp = root->right;
  66.             delete root;
  67.             return temp;
  68.         } else if (root->right == nullptr) {
  69.             Node *temp = root->left;
  70.             delete root;
  71.             return temp;
  72.         }
  73.  
  74.         Node* temp = minValueNode(root->right);
  75.         root->key = temp->key;
  76.         root->right = deleteNode(root->right, temp->key);
  77.     }
  78.     return root;
  79. }
  80.  
  81. int main() {
  82.     Node *root = nullptr;
  83.     root = insert(root, 50);
  84.     insert(root, 30);
  85.     insert(root, 20);
  86.     insert(root, 40);
  87.     insert(root, 70);
  88.     insert(root, 60);
  89.     insert(root, 80);
  90.  
  91.     cout << "Mencetak data yang ada: " << endl;
  92.     inorder(root);
  93.  
  94.  
  95.     root = deleteNode(root, 20);
  96.     root = deleteNode(root, 30);
  97.     root = deleteNode(root, 40);
  98.     root = deleteNode(root, 50);
  99.     root = deleteNode(root, 60);
  100.     root = deleteNode(root, 70);
  101.     root = deleteNode(root, 80);
  102.  
  103.     cout << "\nData Setelah Menghapus semua node:" << endl;
  104.     inorder(root);
  105.  
  106.  
  107.     root = insert(root, 2);
  108.     root = insert(root, 7);
  109.     root = insert(root, 1);
  110.     root = insert(root, 89);
  111.     root = insert(root, 5);
  112.     root = insert(root, 23);
  113.     root = insert(root, 1);
  114.     cout << "\nData Setelah Menambahkan node 2, 7, 1, 89, 5, 23, 1:" << endl;
  115.     inorder(root);
  116.  
  117.  
  118.     root = deleteNode(root, 89);
  119.     root = deleteNode(root, 5);
  120.     root = deleteNode(root, 23);
  121.  
  122.     cout << "\nData Setelah Menghapus nilai 89, 5, 23:" << endl;
  123.     inorder(root);
  124.  
  125.     cout << "\nData setelah menghapus node terkecil right subtree:" << endl;
  126.     inorder(root);
  127.  
  128.     cout << "\nTraversal dari nilai terbesar ke terkecil:" << endl;
  129.     reverseInorder(root);
  130.  
  131.     return 0;
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement