Linear data structures like arrays, stacks, queues, and linked list have only one way to read the data. But a hierarchical data structure like a tree can be traversed in different ways.</p>
1. Depth First Search or DFS
Inorder Traversal
Preorder Traversal
Postorder Traversal
2. Level Order Traversal or Breadth First Search or BFS
Boundary Traversal
Diagonal Traversal
<img src="./print level order inorder/pic/TreeTraversal.png" title="Stack Image->Sakib" >
<h2>Operation Details</h2>
<h3>Inorder traversal</h3>
<p>
1. First, visit all the nodes in the left subtree<br>
2. Then the root node<br>
3. Visit all the nodes in the right subtree<br>
</p>
inorder(root->left)
display(root->data)
inorder(root->right)
<img src="./print level order inorder/pic/f inOrder traversal.jpg" title="Stack Image->Sakib" >
<h3>Preorder traversal</h3>
<p>
1. Visit root node<br>
2. Visit all the nodes in the left subtree<br>
3. Visit all the nodes in the right subtree
</p>
display(root->data)
preorder(root->left)
preorder(root->right)
<img src="./print level order inorder/pic/preorder Traversal.jpg" title="Stack Image->Sakib" >
<h3>Postorder traversal</h3>
<p>
1. Visit all the nodes in the left subtree<br>
2. Visit all the nodes in the right subtree<br>
3. Visit the root node
</p>
display(root->data)
preorder(root->left)
preorder(root->right)
<img src="./print level order inorder/pic/postOrder-f.jpg" title="Stack Image->Sakib" >
<h2>Source Code </h2>
<b> <a href="./print level order inorder/print_and_check_bst.cpp">Code File </a>
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int val;
Node *left;
Node *right;
Node(int val)
{
this->val = val;
this->left = NULL;
this->right = NULL;
}
};
Node *convert(vector<int> v, int n, int l, int r)
{
if (l > r)
return NULL;
int mid = (l + r) / 2;
Node *root = new Node(v[mid]);
Node *left_Root = convert(v, n, l, mid - 1);
Node *right_Root = convert(v, n, mid + 1, r);
root->left = left_Root;
root->right = right_Root;
return root;
}
// in order
void in_Order(Node *root)
{
if (root == NULL)
return;
in_Order(root->left);
cout << root->val << " ";
in_Order(root->right);
}
// pre Oreder
void pre_order(Node *root)
{
if (root == NULL)
return;
cout << root->val << " ";
pre_order(root->left);
pre_order(root->right);
}
// Post Order
void post_Order(Node *root)
{
if (root == NULL)
return;
post_Order(root->left);
post_Order(root->right);
cout << root->val << " ";
}
int main()
{
// Md. Nazmus Sakib
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
vector<int> v;
int data;
while (cin >> data)
{
v.emplace_back(data);
}
sort(v.begin(), v.end());
Node *root = convert(v, v.size(), 0, v.size() - 1);
cout << "print Pre_Order: " << endl;
pre_order(root);
cout << endl
<< "Print In_Order: " << endl;
in_Order(root);
cout << endl
<< "Print Post_Order: " << endl;
post_Order(root);
return 0;
}
<h2>Binary Search Tree(BST)</h2>
<p>Binary search tree is a data structure that quickly allows us to maintain a sorted list of numbers.</p>
<p>
<li> It is called a binary tree because each tree node has a maximum of two children.<br></li>
<li> It is called a search tree because it can be used to search for the presence of a number in <b><i>O(log(n))</i></b> time.</li>
</p>
<p>
The properties that separate a binary search tree from a regular binary tree is
<li>All nodes of left subtree are less than the root node
<li>All nodes of right subtree are more than the root node
<li>Both subtrees of each node are also BSTs i.e. they have the above two properties
</p>
<img src="./print level order inorder/pic/bst-vs-not-bst.webp">
<h2>Search Operation</h2>
<p>
The algorithm depends on the property of BST that if each left subtree has values below root and each right subtree has values above the root.
If the value is below the root, we can say for sure that the value is not in the right subtree; we need to only search in the left subtree and if the value is above the root, we can say for sure that the value is not in the left subtree; we need to only search in the right subtree.
</p> <h2> code</h2><a href="./print level order inorder/print_and_check_bst.cpp">Traversal and Check BST or Not ?</a>
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int val;
Node *left;
Node *right;
Node(int val)
{
this->val = val;
this->left = NULL;
this->right = NULL;
}
};
vector<int>vv;
Node *convert(vector<int> v, int n, int l, int r)
{
if (l > r)
return NULL;
int mid = (l + r) / 2;
Node *root = new Node(v[mid]);
Node *left_Root = convert(v, n, l, mid - 1);
Node *right_Root = convert(v, n, mid + 1, r);
root->left = left_Root;
root->right = right_Root;
return root;
}
// in order
void in_Order(Node *root)
{
if (root == NULL)
return;
in_Order(root->left);
cout << root->val << " ";
vv.emplace_back(root->val);
in_Order(root->right);
}
// pre Oreder
void pre_order(Node *root)
{
if (root == NULL)
return;
cout << root->val << " ";
pre_order(root->left);
pre_order(root->right);
}
// Post Order
void post_Order(Node *root)
{
if (root == NULL)
return;
post_Order(root->left);
post_Order(root->right);
cout << root->val << " ";
}
// Check if the tree is a binary search tree (BST)
// Check BST is or Not
bool is_BST(Node *root)
{
for(int i = 0; i < vv.size(); i++)
{
for(int j = i + 1; j < vv.size()-1; j++)
{
if(vv[i] > vv[j]) return false;
}
}
return true;
}
int main()
{
// Md. Nazmus Sakib
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
vector<int> v;
int data;
while (cin >> data)
{
v.emplace_back(data);
}
//sort(v.begin(), v.end());
Node *root = convert(v, v.size(), 0, v.size() - 1);
cout << "print Pre_Order: " << endl;
pre_order(root);
cout << endl
<< "Print In_Order: " << endl;
in_Order(root);
cout << endl
<< "Print Post_Order: " << endl;
post_Order(root);
if (is_BST(root))
cout << endl << endl
<< "This is a Binary Search Tree" << endl;
else
cout << endl << endl
<< "This is not a Binary Search Tree" << endl;
return 0;
}
<h2>Binary Search Tree Complexities</h2>
<img src="./print level order inorder/pic/Capture.PNG">
| Name | Link |
|---|---|
| Md. Nazmus Sakib | BSc In Engr. |
| [email protected] | |
| Code Force | Code Force |
| CodeChef | CodeChef |
| LeetCode | Code Force |
