< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page
  • Inorder Traversal
  • Preorder Traversal
  • Postorder Traversal
  • Level Order Traversal

Was this helpful?

  1. Binary Tree
  2. What's Binary Tree & Implementation?

Traversal

In a Binary Search Tree (BST), there are several ways to traverse the nodes in a specific order. The most common traversal methods are Inorder, Preorder, Postorder, and Level Order traversals. Here's a detailed explanation of each:

Inorder Traversal

In an inorder traversal, the nodes are visited in the following order: Left subtree -> Root -> Right subtree. This traversal method results in the nodes being visited in ascending order if it's a BST.

Algorithm:

  1. Traverse the left subtree recursively.

  2. Visit the root node.

  3. Traverse the right subtree recursively.

Example: For the following BST:

    4
   / \
  2   6
 / \ / \
1  3 5  7

The inorder traversal would be: 1, 2, 3, 4, 5, 6, 7

Preorder Traversal

In a preorder traversal, the nodes are visited in the following order: Root -> Left subtree -> Right subtree. This traversal method is often used to create a copy of the tree.

Algorithm:

  1. Visit the root node.

  2. Traverse the left subtree recursively.

  3. Traverse the right subtree recursively.

Example: For the same BST:

    4
   / \
  2   6
 / \ / \
1  3 5  7

The preorder traversal would be: 4, 2, 1, 3, 6, 5, 7

Postorder Traversal

In a postorder traversal, the nodes are visited in the following order: Left subtree -> Right subtree -> Root. This traversal method is useful for deleting nodes in a tree.

Algorithm:

  1. Traverse the left subtree recursively.

  2. Traverse the right subtree recursively.

  3. Visit the root node.

Example: For the same BST:

    4
   / \
  2   6
 / \ / \
1  3 5  7

The postorder traversal would be: 1, 3, 2, 5, 7, 6, 4

Level Order Traversal

In a level order traversal, the nodes are visited level by level from left to right. This traversal method is also known as Breadth-First Search (BFS).

Algorithm:

  1. Start from the root node.

  2. Use a queue to keep track of nodes at each level.

  3. Dequeue a node, visit it, and enqueue its left and right children.

  4. Repeat the process until the queue is empty.

Example: For the same BST:

    4
   / \
  2   6
 / \ / \
1  3 5  7

The level order traversal would be: 4, 2, 6, 1, 3, 5, 7

These traversal methods allow different ways of accessing and processing all the nodes in a BST, each with its own unique order and use cases.

PreviousWhat's Binary Tree & Implementation?NextRed-Black BST

Last updated 10 months ago

Was this helpful?

🎋