Tutorial Playlist
Binary trees are an essential data structure used in computer science and programming. They offer a productive means of storing and organizing data hierarchically. We might lead various procedures on the tree's elements by visiting every node in a specific request while navigating a binary tree. In this article, we will analyze the possibility of binary tree crossing, as well as its many structures and uses. We will likewise go through a few other tree-crossing techniques,
Binary trees are hierarchical data structures made up of nodes. Every node in a binary tree has worth and, if relevant, pointers on its left side and right child nodes. The root node of the tree is the principal node, and every node has a limit of two children. Because of their flexibility and effortlessness in portraying different genuine settings, including record frameworks, dynamic cycles, and hierarchical connections, binary trees are frequently utilized.
A binary tree consists of nodes connected by edges. Each node includes references to its left and right child nodes and the ability to store data. A binary tree's structure ensures that each node has a maximum of two offspring, which facilitates efficient navigation and operation. This is an illustration of a binary tree:
Example:
7
/ \
4 9
/ \ /
2 5 8
The "7" node in the model above is the root node. It has a right youngster node with the mark "9" and a left kid node with the name "4". The node "4" contains two kid nodes with the marks "2" and "5" on the left and right, individually. The node "9" has a left kid node named "8".
The act of viewing each node in a data structure in a certain sequence is known as traversing. In the context of binary trees, traversing entails precisely one visit to each node. This operation is crucial for accessing and processing the data stored in the tree effectively. By applying different traversal techniques, we can explore the nodes of a binary tree in different orders.
A binary tree may be traversed by going through each node in turn. Inorder, preorder, and postorder traversals are the three most prevalent forms. Nodes are visited in the following order during inorder traversal: left subtree, current node, then right subtree. Preorder traversal visits the ongoing node before its youngsters, and postorder crossing visits the ongoing node after its kids.
Here's an example:
It is possible to conduct operations like finding, adding, removing, or printing the data contained in the tree thanks to traversal of binary tree in C++, which enables us to process the nodes in a certain sequence.
There are several types of binary tree traversals as listed below:
The left subtree is visited first during the inorder traversal, then the root node, and finally the right subtree. When used with binary search trees, this traversal strategy produces nodes in non-decreasing order.
Here is the algorithm for inorder traversal:
Algorithm Inorder(tree):
Java example of inorder traversal:
java
class Node {Â
  int data;Â
  Node left, right;Â
Â
  public Node(int item) {Â
    data = item;Â
    left = right = null;Â
  }Â
}Â
Â
class BinaryTree {Â
  Node root;Â
Â
  BinaryTree() {Â
    root = null;Â
  }Â
Â
  void inorder traversal(Node node) {Â
    if (node == null)Â
      return;Â
Â
    inorderTraversal(node.left);Â
    System.out.print(node.data " ");Â
    inorderTraversal(node.right);Â
  }Â
Â
  public static void main(String[] args) {Â
    BinaryTree tree = new BinaryTree();Â
    tree.root = new Node(7);Â
    tree.root.left = new Node(4);Â
    tree.root.right = new Node(9);Â
    tree.root.left.left = new Node(2);Â
    tree.root.left.right = new Node(5);Â
    tree.root.right.left = new Node(8);Â
Â
    System.out.println("Inorder traversal: ");Â
    tree.inorderTraversal(tree.root);Â
  }Â
} Â
Output:
yamlCopy codeÂ
Inorder traversal:Â
2 4 5 7 8 9 Â
Uses of Inorder Traversal:
In the preorder traversal, the root node is visited first, followed by the left subtree and then the right subtree. Here is the algorithm for preorder traversal:
Algorithm Preorder(tree):
Java example of preorder traversal:
java
class Node {Â
  int data;Â
  Node left, right;Â
Â
  public Node(int item) {Â
    data = item;Â
    left = right = null;Â
  }Â
}Â
Â
class BinaryTree {Â
  Node root;Â
Â
  BinaryTree() {Â
    root = null;Â
  }Â
Â
  void preorderTraversal(Node node) {Â
    if (node == null)Â
      return;Â
Â
    System.out.print(node.data " ");Â
    preorderTraversal(node.left);Â
    preorderTraversal(node.right);Â
  }Â
Â
  public static void main(String[] args) {Â
    BinaryTree tree = new BinaryTree();Â
    tree.root = new Node(7);Â
    tree.root.left = new Node(4);Â
    tree.root.right = new Node(9);Â
    tree.root.left.left = new Node(2);Â
    tree.root.left.right = new Node(5);Â
    tree.root.right.left = new Node(8);Â
Â
    System.out.println("Preorder traversal: ");Â
    tree.preorderTraversal(tree.root);Â
  }Â
} Â
Output:
yamlCopy codeÂ
Preorder traversal:Â
7 4 2 5 9 8
Uses of Preorder Traversal:
In the postorder traversal of binary tree, the left and right subtrees are visited before the root node. Here is the algorithm for postorder traversal:
Algorithm Postorder(tree):
Java example of postorder traversal:
java
class Node {Â
  int data;Â
  Node left, right;Â
Â
  public Node(int item) {Â
    data = item;Â
    left = right = null;Â
  }Â
}Â
Â
class BinaryTree {Â
  Node root;Â
Â
  BinaryTree() {Â
    root = null;Â
  }Â
Â
  void postorderTraversal(Node node) {Â
    if (node == null)Â
      return;Â
Â
    postorderTraversal(node.left);Â
    postorderTraversal(node.right);Â
    System.out.print(node.data " ");Â
  }Â
Â
  public static void main(String[] args) {Â
    BinaryTree tree = new BinaryTree();Â
    tree.root = new Node(7);Â
    tree.root.left = new Node(4);Â
    tree.root.right = new Node(9);Â
    tree.root.left.left = new Node(2);Â
    tree.root.left.right = new Node(5);Â
    tree.root.right.left = new Node(8);Â
Â
    System.out.println("Postorder traversal: ");Â
    tree.postorderTraversal(tree.root);Â
  }Â
}
Output:
yamlCopy codeÂ
Postorder traversal:Â
2 5 4 8 9 7
Uses of Postorder Traversal:
There are some additional tree traversing techniques as mentioned below:
Understanding and working with these hierarchical data structures require the traversal of binary tree example. In this article, we looked at the inorder, preorder, and postorder traversal approaches. We also spoke about each traversal's applications and gave a quick introduction to alternative traversal strategies such as level order, boundary, and diagonal traversals. By mastering these traversal of binary tree in C techniques, developers can efficiently navigate binary trees and perform various operations on their nodes.
1. Why is traversal important in binary trees?
Traversal is important in binary trees as it allows us to access and process each node in a specific order. It makes it possible to do operations like finding, removing, adding, or printing values kept in the tree.
2. What distinguishes inorder, preorder, and postorder traversal from one another?
In an inorder traversal, the left subtree, the ongoing node, and the right subtree are visited in a specific order. Preorder crossing visits the ongoing node before its kids, while postorder crossing visits the ongoing node after its youngsters.
3. Can I perform different traversals on the same binary tree?
Yes, you can perform different traversals on the same binary tree. Each traversal visits the nodes in a specific order, allowing you to process the tree's elements differently based on your requirements.
4. How is traversal useful in binary tree algorithms?
Traversal is essential in binary tree algorithms as it provides a way to explore the structure of the tree and perform operations on its nodes. Algorithms like tree sorting, expression evaluation, and constructing tree representations rely on traversal.
5. Are there any further tree traversal types?
In addition to inorder, preorder, and postorder tree crossings, there are extra sorts too, including level request crossing (expansiveness first crossing), in which nodes are visited by levels going from start to finish and left to right. These crossings have various purposes and can be applied given the specific states of the recent concern.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...