0% found this document useful (0 votes)
14 views

Binary Tree -

Uploaded by

nitinraj3009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Binary Tree -

Uploaded by

nitinraj3009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

57

Introduction to Binary Tree


Last Updated : 12 Dec, 2024

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.

Introduction to Binary Tree

Representation of Binary Tree


Each node in a Binary Tree has three parts:

Data
Pointer to the left child
Pointer to the right child

Binary Tree Representation


Create/Declare a Node of a Binary Tree

Syntax to declare a Node of Binary Tree in different languages:

C++ C Java Python C# JavaScript

1 // Use any below method to implement Nodes of binary tree


2
3 // 1: Using struct
4 struct Node {
5 int data;
6 Node* left, * right;
7
8 Node(int key) {
9 data = key;
10 left = nullptr;
11 right = nullptr;
12 }
13 };
14
15 // 2: Using class
16 class Node {
17 public:
18 int data;
19 Node* left, * right;
20
21 Node(int key) {
22 data = key;
23 left = nullptr;
24 right = nullptr;
25 }
26 };

Example for Creating a Binary Tree


Here’s an example of creating a Binary Tree with four nodes (2, 3, 4, 5)
Creating a Binary Tree having three nodes

C++ C Java Python C# JavaScript

1 #include <iostream>
2 using namespace std;
3
4 struct Node{
5 int data;
6 Node *left, *right;
7 Node(int d){
8 data = d;
9 left = nullptr;
10 right = nullptr;
11 }
12 };
13
14 int main(){
15 // Initilize and allocate memory for tree nodes
16 Node* firstNode = new Node(2);
17 Node* secondNode = new Node(3);
18 Node* thirdNode = new Node(4);
19 Node* fourthNode = new Node(5);
20
21 // Connect binary tree nodes
22 firstNode->left = secondNode;
23 firstNode->right = thirdNode;
24 secondNode->left = fourthNode;
25 return 0;
26 }

In the above code, we have created four tree nodes firstNode, secondNode,
thirdNode and fourthNode having values 2, 3, 4 and 5 respectively.

After creating three nodes, we have connected these node to form the tree
structure like mentioned in above image.
Connect the secondNode to the left of firstNode by firstNode->left =
secondNode
Connect the thirdNode to the right of firstNode by firstNode->right = thirdNode
Connect the fourthNode to the left of secondNode by secondNode->left =
fourthNode

Terminologies in Binary Tree


Nodes: The fundamental part of a binary tree, where each node contains data and
link to two child nodes.
Root: The topmost node in a tree is known as the root node. It has no parent and
serves as the starting point for all nodes in the tree.
Parent Node: A node that has one or more child nodes. In a binary tree, each node
can have at most two children.
Child Node: A node that is a descendant of another node (its parent).
Leaf Node: A node that does not have any children or both children are null.
Internal Node: A node that has at least one child. This includes all nodes except
the root and the leaf nodes.
Depth of a Node: The number of edges from a specific node to the root node. The
depth of the root node is zero.
Height of a Binary Tree: The number of nodes from the deepest leaf node to the
root node.

The diagram below shows all these terms in a binary tree.

Terminologies in Binary Tree in Data Structure

Properties of Binary Tree


The maximum number of nodes at level L of a binary tree is 2L
The maximum number of nodes in a binary tree of height H is 2H – 1
Total number of leaf nodes in a binary tree = total number of nodes with 2 children
+1
In a Binary Tree with N nodes, the minimum possible height or the minimum
number of levels is Log2(N+1)
A Binary Tree with L leaves has at least | Log2L |+ 1 levels

Please refer Properties of Binary Tree for more details.

Types of Binary Tree


Binary Tree can be classified into multiples types based on multiple factors:

On the basis of Number of Children


Full Binary Tree
Degenerate Binary Tree
Skewed Binary Trees
On the basis of Completion of Levels
Complete Binary Tree
Perfect Binary Tree
Balanced Binary Tree
On the basis of Node Values:
Binary Search Tree
AVL Tree
Red Black Tree
B Tree
B+ Tree
Segment Tree

Operations On Binary Tree


Following is a list of common operations that can be performed on a binary tree:

1. Traversal in 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:

Preorder Traversal (current-left-right): Visits the node first, then left subtree,
then right subtree.
Inorder Traversal (left-current-right): Visits left subtree, then the node, then the
right subtree.
Postorder Traversal (left-right-current): Visits left subtree, then right subtree,
then the node.

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:

C++ C Java Python C# JavaScript

1 #include <bits/stdc++.h>
2 using namespace std;
3
4 struct Node {
5 int data;
6 Node* left, * right;
7
Node(int d) {
9 data = d;
10 left = nullptr;
11 right = nullptr;
12 }
13 };
14
15 // In-order DFS: Left, Root, Right
16 void inOrderDFS(Node* node) {
17 if (node == nullptr) return;
18
19 inOrderDFS(node->left);
20 cout << node->data << " ";
21 inOrderDFS(node->right);
22 }
23
24 // Pre-order DFS: Root, Left, Right
25 void preOrderDFS(Node* node) {
26 if (node == nullptr) return;
27
28 cout << node->data << " ";
29 preOrderDFS(node->left);
30 preOrderDFS(node->right);
31 }
32
33 // Post-order DFS: Left, Right, Root
34 void postOrderDFS(Node* node) {
35 if (node == nullptr) return;
36
37 postOrderDFS(node->left);
38 postOrderDFS(node->right);
39 cout << node->data << " ";
40 }
41
42 void BFS(Node* root) {
43
44 if (root == nullptr) return;
45 queue<Node*> q;
46 q.push(root);
47
48 while (!q.empty()) {
49 Node* node = q.front();
50 q.pop();
51 cout << node->data << " ";
52 if (node->left != nullptr) q.push(node->left);
53 if (node->right != nullptr) q.push(node->right);
54
55 }
56 }
57
58 int main() {
59 Node* root = new Node(2);
root->left = new Node(3);
61 root->right = new Node(4);
62 root->left->left = new Node(5);
63
64 cout << "In-order DFS: ";
65 inOrderDFS(root);
66
67 cout << "\nPre-order DFS: ";
68 preOrderDFS(root);
69
70 cout << "\nPost-order DFS: ";
71 postOrderDFS(root);
72
73 cout << "\nLevel order: ";
74 BFS(root);
75
76 return 0;
77 }

Output

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

2. Insertion in Binary Tree

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.
DSA Course DSA Interview Problems on Tree Practice Tree MCQs on Tree Tutorial on Tree Types of Trees Basic oper

Insertion in Binary Tree

C++ C Java Python C# JavaScript

1 #include <bits/stdc++.h>
2 using namespace std;
3
4 struct Node {
5 int data;
6 Node* left, * right;
7 Node(int d) {
8 data = d;
9 left = right = nullptr;
10 }
11 };
12
13 // Function to insert a new node in the binary tree
14 Node* insert(Node* root, int key) {
15 // If the tree is empty, create the root node
16 if (root == nullptr) {
17 root = new Node(key);
18 return root;
19 }
20 // Create a queue for level order traversal
21 queue<Node*> q;
22 q.push(root);
23
24 // Do level order traversal until we find an empty place
25 while (!q.empty()) {
26 Node* temp = q.front();
27 q.pop();
28
29 // If left child is empty, insert the new node here
30 if (temp->left == nullptr) {
31 temp->left = new Node(key);
32 break;
33 } else {
34 q.push(temp->left);

You might also like