JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

Binary Tree Traversal

Author:JIYIK Last Updated:2025/03/18 Views:

A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left and right children. It can also be interpreted as an undirected graph where the topmost node is called the root. Unlike linear data structures that can be traversed in only one way, a tree can be traversed in different ways. We can traverse a tree by exploring along the depth or the breadth. The first method is called depth-first traversal and the second method is called breadth-first traversal. In this article, we will discuss depth-first traversal.

There are 3 types of depth-first traversal - in-order traversal, pre-order traversal and post-order traversal. We will discuss them one by one.

Binary Tree Traversal

In-order traversal

In this traversal, we first visit the left subtree, then the root, and finally the right subtree. Each node is similar to a subtree. For BST, in-order traversal returns all elements in ascending order.

Pre-order traversal

In this traversal, we first visit the root, then the left subtree and finally the right subtree. Each node resembles a subtree. It is often used for replication, i.e. creating a copy of the tree. Prefix traversal also helps in generating prefix expressions from expression trees.

Post-order traversal

In this traversal, we first visit the left subtree, then the right subtree and finally the root. Each node is similar to a subtree. It is used to delete the tree efficiently. It also helps in generating suffix expressions from expression trees.

Binary tree traversal diagram

Binary Tree

In-order traversal:(4、2、1、5、3、6、7、8、9)

We 3call an in-order traversal on the root node . Recursively traverse leftward to reach the node 4, which is the leftmost node and include it in our output; since it is the root node and has no left node, we visit its rightmost node 2and include it in our traversal. In this way, we traverse the entire tree and get the above order as our output.

Pre-order traversal:(3,1,4,2,5,7,6,9,8)

We 3call a pre-order traversal on the root node and include it in our output. We then recursively traverse left to the next root node 1, followed by 4. Since 4has no left child, we visit the node to the right 2. Now that we have covered 4the subtree under the root node , we backtrack to the node 1and walk right to the node 5. In this way, we traverse the entire tree and get the above order as our output.

Post-order traversal:(2,4,5,1,6,8,9,7,3)

We 3call post-order traversal on the root node , recursively traversing left to reach the node 4. Before 4we include in our traversal, we must visit its right node 2. 1's right node 5is not visited, so we 5include first, and then 1output . We then trace back to the root node 3, and continue traversing the right subtree. In this way, we traverse the entire tree and get the above order as our output.

Binary Tree Traversal Algorithm

In-order traversal

  • Traverse the left subtree by recursively calling the in-order function.
  • Visit the root node.
  • The right subtree is traversed by recursively calling the in-order function.

Pre-order traversal

  • Visit the root node.
  • Traverse the left subtree by recursively calling the in-order function.
  • Traverse the right subtree by recursively calling the sequential inner function.

Post-order traversal

  • Traverse the left subtree by recursively calling the in-order function.
  • The right subtree is traversed by recursively calling the in-order function.
  • Visit the root node.

Implementation of binary tree traversal

#include <iostream>
using namespace std;

class Node {
public:
    int key;
    Node *left, *right;
    Node(int x) {
        this->key = x;
        this->left = this->right = NULL;
    }
};

void inorder(Node *root) {
    if (root != NULL)
    {
        inorder(root->left);
        cout << root->key << " ";
        inorder(root->right);
    }
}
void preorder(Node*root) {
    if (root != NULL) {
        cout << root->key << " ";
        preorder(root->left);
        preorder(root->right);
    }
}

void postorder(Node* root) {
    if (root != NULL) {
        postorder(root->left);
        postorder(root->right);
        cout << root->key << " ";
    }
}

int main() {
    Node* root = new Node(3);
    root->left = new Node(1);
    root->right = new Node(7);
    root->left->left = new Node(4);
    root->left->right = new Node(5);
    root->left->left->right = new Node(2);
    root->right->left = new Node(6);
    root->right->right = new Node(9);
    root->right->right->left = new Node(8);
    cout << "The inorder traversal of the tree is : "; inorder(root); cout << endl;
    cout << "The preorder traversal of the tree is : "; preorder(root); cout << endl;
    cout << "The postorder traversal of the tree is : "; postorder(root); cout << endl;
}

Complexity of binary tree traversal algorithm

Time Complexity

  • Average situation

nThere are nodes in a tree . 3In all traversals, we must visit each node. Since we niterate over nodes, although the order is different, the time complexity of the three traversals is O(n). The time complexity in the average case is O(n).

  • Best Case

The best case time complexity is O(n). It 3is the same as the average case time complexity of all traversals.

  • Worst case scenario

The worst case time complexity is O(n). It 3is the same as the worst case time complexity of all traversals.

Space complexity

Due to the additional space required for the recursive calls, the space complexity of this algorithm is O(n).

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert a binary tree to a binary search tree

Publish Date:2025/03/18 Views:52 Category:ALGORITHM

A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left and right children. It can also be interpreted as an undirected graph where the topmost node

Binary Search Tree

Publish Date:2025/03/18 Views:181 Category:ALGORITHM

A binary search tree (BST) is an ordered binary tree data structure based on nodes. A node has a value and two child nodes (a binary tree has at most two child nodes) connected to each other. A node has a value and two child nodes (a binary

Circular Doubly Linked List

Publish Date:2025/03/18 Views:67 Category:ALGORITHM

A circular doubly linked list is a combination of a circular linked list and a doubly linked list. Its two nodes are connected by a previous and next pointer. The next pointer of the last node points to the first node, and the previous poin

Circular Linked List

Publish Date:2025/03/18 Views:172 Category:ALGORITHM

A circular linked list is a data structure that is slightly more complex than a linked list. It is a linked list where all the nodes are connected in a loop and form a chain. The last node does not have a NULL . Instead, it stores the addre

Doubly Linked List

Publish Date:2025/03/18 Views:69 Category:ALGORITHM

Doubly linked list is a linear data structure. It is a collection of objects defined as nodes. But unlike a linked list, the node has two pointers, one is the previous pointer and the other is the next pointer. Just like the linked list nod

Linked List Merge Sort

Publish Date:2025/03/18 Views:138 Category:ALGORITHM

In this article, we will learn how to sort a linked list using merge sort algorithm. It is one of the most preferred algorithms to sort a linked list because slow pointer random access makes the performance of other algorithms worse (for ex

C++中逐级打印二叉树中的数据

Publish Date:2023/08/27 Views:206 Category:C++

二叉树的逐层遍历称为广度优先遍历。 本教程将教您如何使用 C++ 逐级打印二叉树中的数据,并让您熟悉执行此任务的不同方法。

Java 中的 Trie 数据结构

Publish Date:2023/08/05 Views:129 Category:Java

本文介绍了 Java 中的 Trie 数据结构。Java 中的 Trie 数据结构 Trie 词是从单词 Retrieval 中提取出来的,它是一种用于存储字符串集合的排序数据结构。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial