Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 91d9195

Browse files
Binary Tree Basic Traversal Notes
1 parent 1a19946 commit 91d9195

10 files changed

+262
-0
lines changed

‎1introductionToTress.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//Tree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes.
2+
// A tree data structure is a hierarchical structure that is used to represent and organize data in a way that is easy to navigate and search. It is a collection of nodes that are connected by edges and has a hierarchical relationship between the nodes. 0
3+
4+
// Parent Node: The node which is a predecessor of a node is called the parent node of that node. {B} is the parent node of {D, E}.
5+
// Child Node: The node which is the immediate successor of a node is called the child node of that node. Examples: {D, E} are the child nodes of {B}.
6+
// Root Node: The topmost node of a tree or the node which does not have any parent node is called the root node. {A} is the root node of the tree. A non-empty tree must contain exactly one root node and exactly one path from the root to all other nodes of the tree.
7+
// Leaf Node or External Node: The nodes which do not have any child nodes are called leaf nodes. {K, L, M, N, O, P, G} are the leaf nodes of the tree.
8+
// Ancestor of a Node: Any predecessor nodes on the path of the root to that node are called Ancestors of that node. {A,B} are the ancestor nodes of the node {E}
9+
// Descendant: A node x is a descendant of another node y if and only if y is an ancestor of y.
10+
// Sibling: Children of the same parent node are called siblings. {D,E} are called siblings.
11+
// Level of a node: The count of edges on the path from the root node to that node. The root node has level 0.
12+
// Internal node: A node with at least one child is called Internal Node.
13+
// Neighbor of a Node: Parent or child nodes of that node are called neighbors of that node.
14+
// Subtree: Any node of the tree along with its descendant.Parent Node: The node which is a predecessor of a node is called the parent node of that node. {B} is the parent node of {D, E}.
15+
// Child Node: The node which is the immediate successor of a node is called the child node of that node. Examples: {D, E} are the child nodes of {B}.
16+
// Root Node: The topmost node of a tree or the node which does not have any parent node is called the root node. {A} is the root node of the tree. A non-empty tree must contain exactly one root node and exactly one path from the root to all other nodes of the tree.
17+
// Leaf Node or External Node: The nodes which do not have any child nodes are called leaf nodes. {K, L, M, N, O, P, G} are the leaf nodes of the tree.
18+
// Ancestor of a Node: Any predecessor nodes on the path of the root to that node are called Ancestors of that node. {A,B} are the ancestor nodes of the node {E}
19+
// Descendant: A node x is a descendant of another node y if and only if y is an ancestor of y.
20+
// Sibling: Children of the same parent node are called siblings. {D,E} are called siblings.
21+
// Level of a node: The count of edges on the path from the root node to that node. The root node has level 0.
22+
// Internal node: A node with at least one child is called Internal Node.
23+
// Neighbor of a Node: Parent or child nodes of that node are called neighbors of that node.
24+
// // Subtree: Any node of the tree along with its descendant.
25+

‎2BinaryTreeRepresentation.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
struct Node
4+
{
5+
int data;
6+
struct Node* left;
7+
struct Node* right;
8+
9+
Node(int val)
10+
{
11+
data = val;
12+
left = NULL;
13+
right = NULL;
14+
}
15+
16+
};
17+
18+
int main()
19+
{
20+
struct Node* root = new Node(1);
21+
root->left = new Node(2);
22+
root->right = new Node(5);
23+
root->left->right = new Node(3);
24+
25+
}

‎4BinaryTreversalinBInaryTree.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways.
2+
3+
// Generally, there are 2 widely used ways for traversing trees:
4+
5+
// DFS or Depth-First Search
6+
// // BFS or Breadth-First Search
7+
8+
// DFS (Depth-first search) is a technique used for traversing trees or graphs. Here backtracking is used for traversal. In this traversal first, the deepest node is visited and then backtracks to its parent node if no sibling of that node exists
9+
10+
11+
// 1. Inorder Traversal
12+
// Traverse the left subtree, i.e., call Inorder(left-subtree)
13+
// Visit the root
14+
// Traverse the right subtree, i.e., call Inorder(right-subtree)
15+
// Time Complexity: O(N)
16+
// Auxiliary Space: O(log N)
17+
18+
// Uses of Inorder traversal:
19+
// In the case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order. To get nodes of BST in non-increasing order, a variation of Inorder traversal where Inorder traversal is reversed can be used.
20+
21+
// 2. Preorder Traversal
22+
// Visit the root
23+
// Traverse the left subtree, i.e., call Preorder(left-subtree)
24+
// Traverse the right subtree, i.e., call Preorder(right-subtree)
25+
26+
// Time Complexity: O(N)
27+
// Auxiliary Space: O(log N)
28+
29+
// Uses of Preorder:
30+
// Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get prefix expressions of an expression tree.
31+
32+
33+
// 3. Postorder Traversal (Practice):
34+
// Follow the below steps to solve the problem:
35+
36+
// Traverse the left subtree, i.e., call Postorder(left-subtree)
37+
// Traverse the right subtree, i.e., call Postorder(right-subtree)
38+
// Visit the root
39+
40+
41+
// Time Complexity: O(N)
42+
// Auxiliary Space: O(log N)
43+
44+
// Uses of Postorder:
45+
// Postorder traversal is used to delete the tree. Please see the question for the deletion of the tree for details. Postorder traversal is also useful to get the postfix expression of an expression tree

‎5PreOrderTraversal.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
struct Node
5+
{
6+
/* data */
7+
int data;
8+
Node* left;
9+
Node* right;
10+
11+
Node(int val)
12+
{
13+
data = val;
14+
left = right = NULL;
15+
}
16+
};
17+
void PrintPreOrderTraversal(Node* root)
18+
{
19+
if(root == NULL)
20+
{
21+
return;
22+
}
23+
24+
cout<<root->data<<" ";
25+
26+
PrintPreOrderTraversal(root->left);
27+
PrintPreOrderTraversal(root->right);
28+
}
29+
int main()
30+
{
31+
struct Node* root = new Node(2);
32+
root->left = new Node(5);
33+
root->right = new Node(7);
34+
root->right->left = new Node(9);
35+
root->right->left->left = new Node(2);
36+
root->right->left->right = new Node(3);
37+
root->right->right = new Node(1);
38+
root->right->left->right->left = new Node(1);
39+
root->right->left->right->right = new Node(4);
40+
41+
PrintPreOrderTraversal(root);
42+
}

‎5PreOrderTraversal.exe

46.5 KB
Binary file not shown.

‎6InOrderTraversal.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
struct Node
6+
{
7+
int data;
8+
Node* left;
9+
Node* right;
10+
11+
Node(int val)
12+
{
13+
data = val;
14+
left = right = NULL;
15+
}
16+
17+
};
18+
void printInOrderTraversal(Node* root)
19+
{
20+
if(root == NULL)
21+
{
22+
return;
23+
}
24+
25+
printInOrderTraversal(root->left);
26+
cout<<root->data<<" ";
27+
printInOrderTraversal(root->right);
28+
}
29+
int main()
30+
{
31+
struct Node* root = new Node(2);
32+
root->left = new Node(5);
33+
root->right = new Node(7);
34+
root->right->left = new Node(9);
35+
root->right->left->left = new Node(2);
36+
root->right->left->right = new Node(3);
37+
root->right->right = new Node(1);
38+
root->right->left->right->left = new Node(1);
39+
root->right->left->right->right = new Node(4);
40+
41+
printInOrderTraversal(root);
42+
}

‎6InOrderTraversal.exe

46.5 KB
Binary file not shown.

‎7postOrderTraversal.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
struct Node{
4+
int data;
5+
Node* left;
6+
Node* right;
7+
8+
Node(int val)
9+
{
10+
data = val;
11+
left = right = NULL;
12+
}
13+
};
14+
void postOrderTraversal(Node* root)
15+
{
16+
if(root == NULL)
17+
return;
18+
19+
postOrderTraversal(root->left);
20+
postOrderTraversal(root->right);
21+
22+
cout<<root->data<<" ";
23+
}
24+
int main()
25+
{
26+
struct Node* root = new Node(2);
27+
root->left = new Node(5);
28+
root->right = new Node(7);
29+
root->right->left = new Node(9);
30+
root->right->left->left = new Node(2);
31+
root->right->left->right = new Node(3);
32+
root->right->right = new Node(1);
33+
root->right->left->right->left = new Node(1);
34+
root->right->left->right->right = new Node(4);
35+
36+
postOrderTraversal(root);
37+
}

‎7postOrderTraversal.exe

46.5 KB
Binary file not shown.

‎levelOrderTraversal.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
//Level Order Traversal technique is defined as a method to traverse a Tree such that all nodes present in the same level are traversed completely before traversing the next level.
5+
struct Node
6+
{
7+
int data;
8+
Node* left;
9+
Node* right;
10+
11+
Node(int val)
12+
{
13+
data = val;
14+
left = right = NULL;
15+
}
16+
};
17+
//We need to visit the nodes in a lower level before any node in a higher level, this idea is quite similar to that of a queue. Push the nodes of a lower level in the queue. When any node is visited, pop that node from the queue and push the child of that node in the queue.
18+
19+
// This ensures that the node of a lower level are visited prior to any node of a higher level.
20+
vector<vector<int>> levelOrderTraversal(Node* root)
21+
{
22+
vector<vector<int>> ans;
23+
if(root == NULL ) return ans;
24+
25+
queue<Node*> q;
26+
q.push(root);
27+
while(!q.empty())
28+
{
29+
int size = q.size();
30+
vector<int> level;
31+
for(int i=0;i<size;i++)
32+
{
33+
Node* node = q.front();
34+
q.pop();
35+
if(node->left!=NULL) q.push(node->left);
36+
if(node->right!=NULL) q.push(node->right);
37+
level.push_back(node->data);
38+
}
39+
ans.push_back(level);
40+
}
41+
return ans;
42+
}
43+
int main()
44+
{
45+
46+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /