Advertisement
JonathanA007

Tree 10.9 Latihan (Kelompok 1)

May 16th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | Source Code | 0 0
  1. #include <stdlib.h>  // srand, rand
  2. #include <time.h>    // time
  3.  
  4. #include <iostream>
  5. using namespace std;
  6. struct Node {
  7.   int data;
  8.   Node *anakKiri;
  9.   Node *anakKanan;
  10.   Node(int);
  11. };
  12. struct Tree {
  13.   Node *root;
  14.   Tree();
  15.   void sisipDtNode(int);
  16.   void preorderTraversal();
  17.   void inorderTraversal();
  18.   void postorderTraversal();
  19.   void countNode(Node *, int &);
  20.   int TotalLeaf(Node *);
  21.   int Height(Node *);
  22.  
  23.  private:
  24.   void sisipDt(Node *, int);
  25.   void preorder(Node *);
  26.   void inorder(Node *);
  27.   void postorder(Node *);
  28. };
  29. Node::Node(int dt) {
  30.   data = dt;
  31.   anakKiri = anakKanan = NULL;
  32. }
  33. Tree::Tree() { root = NULL; }
  34. void Tree::sisipDtNode(int dtSisip) {
  35.   if (root == NULL)
  36.     root = new Node(dtSisip);
  37.   else
  38.     sisipDt(root, dtSisip);
  39. }
  40. void Tree::preorderTraversal() { preorder(root); }
  41. void Tree::inorderTraversal() { inorder(root); }
  42. void Tree::postorderTraversal() { postorder(root); }
  43. void Tree::sisipDt(Node *node, int dtSisip) {
  44.   if (dtSisip < node->data) {
  45.     if (node->anakKiri == NULL)
  46.       node->anakKiri = new Node(dtSisip);
  47.     else
  48.       sisipDt(node->anakKiri, dtSisip);
  49.   } else if (dtSisip > node->data) {
  50.     if (node->anakKanan == NULL)
  51.       node->anakKanan = new Node(dtSisip);
  52.     else
  53.       sisipDt(node->anakKanan, dtSisip);
  54.   }
  55. }
  56. void Tree::preorder(Node *node) {
  57.   if (node == NULL) return;
  58.   cout << node->data << ", ";
  59.   preorder(node->anakKiri);
  60.   preorder(node->anakKanan);
  61. }
  62. void Tree::inorder(Node *node) {
  63.   if (node == NULL) return;
  64.   inorder(node->anakKiri);
  65.   cout << node->data << ", ";
  66.   inorder(node->anakKanan);
  67. }
  68. void Tree::postorder(Node *node) {
  69.   if (node == NULL) return;
  70.   postorder(node->anakKiri);
  71.   postorder(node->anakKanan);
  72.   cout << node->data << ", ";
  73. }
  74.  
  75. void Tree::countNode(Node *node, int &count) {
  76.   if (node == NULL) return;
  77.   countNode(node->anakKiri, count);
  78.   count++;
  79.   countNode(node->anakKanan, count);
  80. }
  81.  
  82. int Tree::TotalLeaf(Node *node) {
  83.   if (node == NULL) return 0;
  84.   if (node->anakKiri == NULL && node->anakKanan == NULL) return 1;
  85.   else
  86.     return TotalLeaf(node->anakKiri) + TotalLeaf(node->anakKanan);
  87. }
  88.  
  89. int Tree::Height(Node *node) {
  90.   if (node == NULL) return 0;
  91.   int lDepth = Height(node->anakKiri);
  92.   int rDepth = Height(node->anakKanan);
  93.   if (lDepth > rDepth) {
  94.     return (lDepth + 1);
  95.   } else {
  96.     return (rDepth + 1);
  97.   }
  98. }
  99.  
  100. int main() {
  101.   Tree *tree = new Tree();
  102.   int nilai;
  103.   cout << "Sisip nilai data berikut: " << endl;
  104.   // sisip data 10 bilangan acak dari 0-99 ke dalam tree
  105.   srand(time(NULL));
  106.   for (int i = 0; i < 10; i++) {
  107.     nilai = rand() % 100;
  108.     cout << nilai << ",";
  109.     tree->sisipDtNode(nilai);
  110.   }
  111.   cout << "\n\nPreorder Traversal" << endl;
  112.   tree->preorderTraversal();
  113.   cout << "\n\nInorder Traversal" << endl;
  114.   tree->inorderTraversal();
  115.   cout << "\n\nPostorder Traversal" << endl;
  116.   tree->postorderTraversal();
  117.  
  118.   int countNode = 0;
  119.   tree->countNode(tree->root, countNode);
  120.   cout << "\n\nJumlah node: " << countNode << endl;
  121.  
  122.   int countLeaf = 0;
  123.   countLeaf = tree->TotalLeaf(tree->root);
  124.   cout << "Jumlah leaf: " << countLeaf << endl;
  125.  
  126.   int height = 0;
  127.   height = tree->Height(tree->root);
  128.   cout << "Tinggi pohon: " << height << endl;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement