Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม

Preorder Tree Traversal ในโครงสร้างข้อมูล


Pre-order Traversal (Recursive)

ลำดับ: Root → Left → Right

Preorder Tree Traversal ในโครงสร้างข้อมูล

ลำดับ: 10, 5, 8, 16, 15, 20, 23

อัลกอริทึม

preorderTraverse(root):
  if root not empty:
    print root value
    preorderTraversal(left of root)
    preorderTraversal(right of root)

โค้ด C++ และ Output

// ใช้โครงสร้าง node, tree เดียวกับ BST
// preorder_traversal: print root → left → right
// Output: Pre-Order Traversal: 10 5 8 16 15 20 23