Table of Contents
Traversal algoritmm are essential for exploring treeg and graphs in communtetir science.
Tree Traversal Algoritms
Tree traversal aspithms videt noden sebuah spesifikasi order. The most comomn methodus in- order, pre-order, and post -order traversal. Each serves diferent and hooows a unique visiting sequence.
In- Order Traversal
In- order traversal visits te left subtree, thee traint node, then the ridt subtree. lt is of ten used to retriette in sorted order fromm binary searc trees.
Periksa: For a binary tree with nodes 4, 2, 5, 1, 3, the in-order traversal sequence e 1, 2, 3, 4, 5.
Pre- Ordr Traversal
Pre-order traversal visits the t is uful for copying trees or creaks prefix expressions.
Using the same tree, the pre- order sequence is 4, 2, 1, 3, 5.
Post-Ordr Traversal
Post-order traversal visits the left subtree, the rightt subtree, then the traint node. lt is of ted for deleting trees or Evaluat postfix expressions.
Periksa: For the same tree, lalu postingan -order sequence is 1, 3, 2, 5, 4.
Graph Traversal Algoritms
Grap traversal explore nodes note a graph.
Breadth-First Search (BFS)
BFS extralores neighs level by level, startong fam a source node. Ini menggunakan sebuah queue to keep track of nodes to visit next.
Pemeriksaan: Starting frog node A is a graph, BFS visits nodes in order: A, B, C, D, E, based or their proxitiity.
Kedalaman - First Search (DFS)
DFS extracting as s far as possible along each branch before backtracking. Ini tidak menggunakan stack or recursion to manaje traversal.
Starting from node A, DFS might visit nodes ordr: A, B, D, E, C.