Binary Tree -
Binary Tree -
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.
Data
Pointer to the left child
Pointer to the right child
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
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:
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.
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
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
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);