Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h> // srand, rand
- #include <time.h> // time
- #include <iostream>
- using namespace std;
- struct Node {
- int data;
- Node *anakKiri;
- Node *anakKanan;
- Node(int);
- };
- struct Tree {
- Node *root;
- Tree();
- void sisipDtNode(int);
- void preorderTraversal();
- void inorderTraversal();
- void postorderTraversal();
- void countNode(Node *, int &);
- int TotalLeaf(Node *);
- int Height(Node *);
- private:
- void sisipDt(Node *, int);
- void preorder(Node *);
- void inorder(Node *);
- void postorder(Node *);
- };
- Node::Node(int dt) {
- data = dt;
- anakKiri = anakKanan = NULL;
- }
- Tree::Tree() { root = NULL; }
- void Tree::sisipDtNode(int dtSisip) {
- if (root == NULL)
- root = new Node(dtSisip);
- else
- sisipDt(root, dtSisip);
- }
- void Tree::preorderTraversal() { preorder(root); }
- void Tree::inorderTraversal() { inorder(root); }
- void Tree::postorderTraversal() { postorder(root); }
- void Tree::sisipDt(Node *node, int dtSisip) {
- if (dtSisip < node->data) {
- if (node->anakKiri == NULL)
- node->anakKiri = new Node(dtSisip);
- else
- sisipDt(node->anakKiri, dtSisip);
- } else if (dtSisip > node->data) {
- if (node->anakKanan == NULL)
- node->anakKanan = new Node(dtSisip);
- else
- sisipDt(node->anakKanan, dtSisip);
- }
- }
- void Tree::preorder(Node *node) {
- if (node == NULL) return;
- cout << node->data << ", ";
- preorder(node->anakKiri);
- preorder(node->anakKanan);
- }
- void Tree::inorder(Node *node) {
- if (node == NULL) return;
- inorder(node->anakKiri);
- cout << node->data << ", ";
- inorder(node->anakKanan);
- }
- void Tree::postorder(Node *node) {
- if (node == NULL) return;
- postorder(node->anakKiri);
- postorder(node->anakKanan);
- cout << node->data << ", ";
- }
- void Tree::countNode(Node *node, int &count) {
- if (node == NULL) return;
- countNode(node->anakKiri, count);
- count++;
- countNode(node->anakKanan, count);
- }
- int Tree::TotalLeaf(Node *node) {
- if (node == NULL) return 0;
- if (node->anakKiri == NULL && node->anakKanan == NULL) return 1;
- else
- return TotalLeaf(node->anakKiri) + TotalLeaf(node->anakKanan);
- }
- int Tree::Height(Node *node) {
- if (node == NULL) return 0;
- int lDepth = Height(node->anakKiri);
- int rDepth = Height(node->anakKanan);
- if (lDepth > rDepth) {
- return (lDepth + 1);
- } else {
- return (rDepth + 1);
- }
- }
- int main() {
- Tree *tree = new Tree();
- int nilai;
- cout << "Sisip nilai data berikut: " << endl;
- // sisip data 10 bilangan acak dari 0-99 ke dalam tree
- srand(time(NULL));
- for (int i = 0; i < 10; i++) {
- nilai = rand() % 100;
- cout << nilai << ",";
- tree->sisipDtNode(nilai);
- }
- cout << "\n\nPreorder Traversal" << endl;
- tree->preorderTraversal();
- cout << "\n\nInorder Traversal" << endl;
- tree->inorderTraversal();
- cout << "\n\nPostorder Traversal" << endl;
- tree->postorderTraversal();
- int countNode = 0;
- tree->countNode(tree->root, countNode);
- cout << "\n\nJumlah node: " << countNode << endl;
- int countLeaf = 0;
- countLeaf = tree->TotalLeaf(tree->root);
- cout << "Jumlah leaf: " << countLeaf << endl;
- int height = 0;
- height = tree->Height(tree->root);
- cout << "Tinggi pohon: " << height << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement