![]() |
VOOZH | about |
Binary Tree is a non-linear and hierarchical data structure where each node has at most two children referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves.
Each node in a Binary Tree has three parts:
Syntax to declare a Node of Binary Tree in Python:
Here’s an example of creating a Binary Tree with four nodes (2, 3, 4, 5)
In the above code, we have created four tree nodes firstNode, secondNode, thirdNode and fourthNode having values 2, 3, 4 and 5 respectively.
Binary Tree can be classified into multiples types based on multiple factors:
read more about - Terminologies and properties of Binary Tree
Following is a list of common operations that can be performed on a binary tree:
Traversal in Binary Tree involves visiting all the nodes of the binary tree. Tree Traversal algorithms can be classified broadly into two categories, DFS and BFS:
Depth-First Search (DFS) algorithms: DFS explores as far down a branch as possible before backtracking. It is implemented using recursion. The main traversal methods in DFS for binary trees are:
Breadth-First Search (BFS) algorithms: BFS explores all nodes at the present depth before moving on to nodes at the next depth level. It is typically implemented using a queue. BFS in a binary tree is commonly referred to as Level Order Traversal.
Below is the implementation of traversals algorithm in binary tree:
In-order DFS: 5 3 2 4 Pre-order DFS: 2 3 5 4 Post-order DFS: 5 3 4 2 Level order: 2 3 4 5
Inserting elements means add a new node into the binary tree. As we know that there is no such ordering of elements in the binary tree, So we do not have to worry about the ordering of node in the binary tree. We would first creates a root node in case of empty tree. Then subsequent insertions involve iteratively searching for an empty place at each level of the tree. When an empty left or right child is found then new node is inserted there. By convention, insertion always starts with the left child node.
Inorder traversal before insertion: 5 3 2 4 Inorder traversal after insertion: 5 3 6 2 4
Searching for a value in a binary tree means looking through the tree to find a node that has that value. Since binary trees do not have a specific order like binary search trees, we typically use any traversal method to search. The most common methods are depth-first search (DFS) and breadth-first search (BFS). In DFS, we start from the root and explore the depth nodes first. In BFS, we explore all the nodes at the present depth level before moving on to the nodes at the next level. We continue this process until we either find the node with the desired value or reach the end of the tree. If the tree is empty or the value isn’t found after exploring all possibilities, we conclude that the value does not exist in the tree.
Here is the implementation of searching in a binary tree using Depth-First Search (DFS):
6 is found in the binary tree
Deleting a node from a binary tree means removing a specific node while keeping the tree’s structure. First, we need to find the node that want to delete by traversing through the tree using any traversal method. Then replace the node’s value with the value of the last node in the tree (found by traversing to the rightmost leaf), and then delete that last node. This way, the tree structure won’t be effected. And remember to check for special cases, like trying to delete from an empty tree, to avoid any issues.
Note: There is no specific rule of deletion but we always make sure that during deletion the binary tree proper should be preserved.
Original tree (in-order): 5 3 6 2 4 Tree after deleting 3 (in-order): 5 6 2 4