Pertemuan 11
Universitas Pertahanan RI
Setelah pertemuan ini, mahasiswa mampu:
Tree adalah struktur data non-linear yang terdiri dari node-node yang terhubung secara hierarkis.
Setiap tree memiliki:
| Aspek | Array/List | Tree |
|---|---|---|
| Struktur | Linear | Hierarkis (non-linear) |
| Hubungan | Predecessor-Successor | Parent-Child |
| Searching | O(n) unsorted | O(log n) BST seimbang |
| Representasi | Flat | Berlevel |
📁 File System
Struktur folder & file
🏛️ Organisasi
Hierarki komando militer
🎯 Decision Tree
Analisis ancaman taktis
💾 Database
B-Tree indexing
🔧 Parsing
Expression tree
🤖 AI/ML
Decision tree classifier
Contoh binary tree dengan 7 node
| Istilah | Definisi |
|---|---|
| Root | Node paling atas tanpa parent |
| Parent | Node yang memiliki child di bawahnya |
| Child | Node yang memiliki parent di atasnya |
| Sibling | Node-node dengan parent yang sama |
| Leaf | Node tanpa child (terminal) |
| Internal Node | Node yang bukan leaf (punya child) |
| Properti | Definisi | Contoh |
|---|---|---|
| Depth | Jumlah edge dari root ke node | Depth(A)=0, Depth(D)=2 |
| Height | Jumlah edge terpanjang ke leaf | Height(A)=2, Height(D)=0 |
| Level | Semua node dengan depth sama | Level 0: {A} |
| Degree | Jumlah child dari node | Degree(A)=2 |
Tentukan: leaves, height, depth node 15!
Binary Tree adalah tree di mana setiap node memiliki maksimal 2 child — disebut left child dan right child.
struct Node {
int data;
Node* left; // Pointer ke left child
Node* right; // Pointer ke right child
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
Full Binary Tree
Setiap node punya 0 atau 2 child
Complete Binary Tree
Semua level penuh kecuali terakhir (kiri ke kanan)
Perfect Binary Tree
Semua level terisi penuh
n = 2h+1 - 1
Skewed Binary Tree
Semua node hanya punya satu child
Worst case: O(n)!
| Properti | Formula |
|---|---|
| Max nodes di level l | 2l |
| Max nodes dengan height h | 2h+1 - 1 |
| Min height dengan n nodes | ⌈log₂(n+1)⌉ - 1 |
| Jumlah leaf (perfect tree) | 2h |
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
class BinaryTree {
private:
Node* root;
public:
BinaryTree() : root(nullptr) {}
Node* getRoot() { return root; }
void setRoot(Node* node) { root = node; }
};
// Penggunaan
int main() {
BinaryTree tree;
tree.setRoot(new Node(10));
tree.getRoot()->left = new Node(5);
tree.getRoot()->right = new Node(15);
}
Array: [10, 5, 15, 3, 7, 12, 20]
| Relasi | Formula |
|---|---|
| Parent dari node i | (i - 1) / 2 |
| Left child dari node i | 2 * i + 1 |
| Right child dari node i | 2 * i + 2 |
💡 Contoh: Node index 2 (nilai 15)
Left child: 2*2+1 = 5 → arr[5] = 12
| Aspek | Linked | Array |
|---|---|---|
| Memory | Fleksibel | Fixed/resize |
| Akses parent | Perlu pointer tambahan | O(1) formula |
| Insertion | Mudah | Sulit jika tidak complete |
| Wasted space | Tidak ada | Ada jika sparse |
| Best for | General tree | Complete tree, Heap |
Traversal adalah proses mengunjungi semua node dalam tree dengan urutan tertentu.
Depth-First (DFS):
Breadth-First (BFS):
Urutan: Root → Left → Right
void preorder(Node* root) {
if (root == nullptr) return;
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
void preorderIterative(Node* root) {
if (!root) return;
stack s;
s.push(root);
while (!s.empty()) {
Node* curr = s.top();
s.pop();
cout << curr->data << " ";
if (curr->right) s.push(curr->right);
if (curr->left) s.push(curr->left);
}
}
Urutan: Left → Root → Right
📌 Untuk BST: menghasilkan urutan terurut!
void inorder(Node* root) {
if (root == nullptr) return;
inorder(root->left); // Traverse subtree kiri
cout << root->data << " "; // Kunjungi root
inorder(root->right); // Traverse subtree kanan
}
💡 Kegunaan Inorder:
Urutan: Left → Right → Root
📌 Berguna untuk menghapus tree (delete children dulu)
void postorder(Node* root) {
if (root == nullptr) return;
postorder(root->left); // Traverse subtree kiri
postorder(root->right); // Traverse subtree kanan
cout << root->data << " "; // Kunjungi root
}
// Untuk menghapus tree dengan aman
void deleteTree(Node* root) {
if (root == nullptr) return;
deleteTree(root->left);
deleteTree(root->right);
delete root; // Hapus setelah children dihapus
}
Urutan: Level by level, kiri ke kanan
📌 Menggunakan Queue
#include
void levelOrder(Node* root) {
if (root == nullptr) return;
queue q;
q.push(root);
while (!q.empty()) {
Node* current = q.front();
q.pop();
cout << current->data << " ";
if (current->left) q.push(current->left);
if (current->right) q.push(current->right);
}
}
| Traversal | Urutan | Kegunaan |
|---|---|---|
| Preorder | Root → L → R | Copy tree, prefix notation |
| Inorder | L → Root → R | BST sorted, infix notation |
| Postorder | L → R → Root | Delete tree, postfix notation |
| Level-order | Level by level | Print by level, BFS |
Kompleksitas: Semua O(n) waktu, O(h) atau O(w) ruang
Tuliskan hasil Preorder, Inorder, dan Postorder!
Expression Tree adalah binary tree yang merepresentasikan ekspresi matematika.
| Preorder (Prefix): | * + 3 5 2 |
| Inorder (Infix): | 3 + 5 * 2 |
| Postorder (Postfix): | 3 5 + 2 * |
Node* buildExpressionTree(string postfix) {
stack st;
for (char c : postfix) {
if (c == ' ') continue;
Node* node = new Node(c);
if (isdigit(c) || isalpha(c)) {
// Operand: langsung push
st.push(node);
} else {
// Operator: pop 2 operand sebagai children
node->right = st.top(); st.pop();
node->left = st.top(); st.pop();
st.push(node);
}
}
return st.top(); // Root dari expression tree
}
Kegunaan: Representasi struktur organisasi, distribusi perintah, analisis jalur komando
| Konsep | Deskripsi | Kompleksitas |
|---|---|---|
| Tree | Struktur hierarkis dengan root, parent, child | - |
| Binary Tree | Max 2 children per node | - |
| Preorder | Root → Left → Right | O(n) |
| Inorder | Left → Root → Right (sorted for BST) | O(n) |
| Postorder | Left → Right → Root (delete tree) | O(n) |
| Level-order | BFS menggunakan Queue | O(n) |
| Expression Tree | Representasi ekspresi matematika | O(n) |
Silakan bertanya atau diskusi
Referensi: Cormen Ch.12 | Weiss Ch.4 | Hubbard Ch.9-10
Struktur Data dan Algoritma
Pertemuan 11: Tree dan Binary Tree
© 2026 - CC BY 4.0