enh(CPlusPlus): Binary Search Tree and Iterative Traversal (#434)
Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com>pull/441/head
parent
ad6073753a
commit
2c8c9c011c
|
@ -1,6 +1,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
|
|
||||||
//Structure of the Tree
|
//Structure of the Tree
|
||||||
struct TreeNode{
|
struct TreeNode{
|
||||||
int data;
|
int data;
|
||||||
|
@ -161,6 +162,114 @@ bool isperfect(TreeNode* root){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void leaf_nodes(TreeNode* root) //Print all leafnode in BST
|
||||||
|
{
|
||||||
|
/* * Node which does not have a child is called as LEAF Node.
|
||||||
|
* Printing the nodes whose left and right pointer are null.
|
||||||
|
* @params: `root` root/parent node of the tree
|
||||||
|
*/
|
||||||
|
if (!root)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (root->left==NULL && root->right==NULL)
|
||||||
|
{
|
||||||
|
std::cout<<root->data<< " ";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (root->left)
|
||||||
|
leaf_nodes(root->left);
|
||||||
|
|
||||||
|
if (root->right)
|
||||||
|
leaf_nodes(root->right);
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeNode* f_min(TreeNode* root) //Find Minimum element from root
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Print the minimum value of the tree
|
||||||
|
*
|
||||||
|
* @params: `root` root/parent node of the tree
|
||||||
|
* @return: void
|
||||||
|
*/
|
||||||
|
if(root==NULL)
|
||||||
|
{
|
||||||
|
std::cout<<"No value present in the tree"<< std::endl;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
TreeNode* p=root;
|
||||||
|
while(p->left!=NULL)
|
||||||
|
{
|
||||||
|
p=p->left;
|
||||||
|
}
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeNode* f_max(TreeNode* root) //Find Maximum element from root
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Print the maximum value of the tree
|
||||||
|
*
|
||||||
|
* @params: `root` root/parent node of the tree
|
||||||
|
* @return: void
|
||||||
|
*/
|
||||||
|
if(root==NULL)
|
||||||
|
{
|
||||||
|
std::cout<<"No value present in the tree"<< std::endl;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
TreeNode* p=root;
|
||||||
|
while(p->right!=NULL)
|
||||||
|
{
|
||||||
|
p=p->right;
|
||||||
|
}
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeNode* bstdelete(TreeNode* root, int x)
|
||||||
|
{
|
||||||
|
TreeNode* m;
|
||||||
|
if(root==NULL)
|
||||||
|
{
|
||||||
|
std::cout<<"NOT FOUND!!";
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
if(x < root->data)
|
||||||
|
{
|
||||||
|
root->left=bstdelete(root->left,x);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
if(x>root->data)
|
||||||
|
{
|
||||||
|
root->right=bstdelete(root->right,x);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
if(root->left==NULL && root->right==NULL)
|
||||||
|
{
|
||||||
|
m=root;
|
||||||
|
delete m;
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
else if(root->left==NULL)
|
||||||
|
{
|
||||||
|
m=root;
|
||||||
|
root=root->right;
|
||||||
|
delete m;
|
||||||
|
return (root);
|
||||||
|
}
|
||||||
|
else if(root->right==NULL)
|
||||||
|
{
|
||||||
|
m=root;
|
||||||
|
root=root->left;
|
||||||
|
delete m;
|
||||||
|
return (root);
|
||||||
|
}
|
||||||
|
m=f_min(root->right);
|
||||||
|
root->data=m->data;
|
||||||
|
root->right=bstdelete(root->right, m->data);
|
||||||
|
return (root);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void print(TreeNode* root){
|
void print(TreeNode* root){
|
||||||
/**
|
/**
|
||||||
|
@ -216,8 +325,29 @@ int main(){
|
||||||
if(isperfect(root)) { std::cout << "The binary tree is PERFECT" << std::endl; }
|
if(isperfect(root)) { std::cout << "The binary tree is PERFECT" << std::endl; }
|
||||||
else { std::cout << "The binary tree is not PERFECT" << std::endl; }
|
else { std::cout << "The binary tree is not PERFECT" << std::endl; }
|
||||||
|
|
||||||
|
std::cout << "Leaf Nodes present in the binary tree are : ";
|
||||||
|
leaf_nodes(root);
|
||||||
|
std::cout<< std::endl;
|
||||||
|
|
||||||
|
n = f_max(root);
|
||||||
|
if(n)
|
||||||
|
{
|
||||||
|
std::cout<<"Maximum Value Present in the tree is : "<<n->data<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
n=f_min(root);
|
||||||
|
if(n)
|
||||||
|
{
|
||||||
|
std::cout<<"Minimum Value Present in the tree is : "<<n->data<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
root = bstdelete(root, 19);
|
||||||
|
std::cout<<"Binary search Tree after Deletion is : "<<std::endl;;
|
||||||
|
print(root);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Tree structure
|
Tree structure
|
||||||
|
|
||||||
37
|
37
|
||||||
/ \
|
/ \
|
||||||
19 51
|
19 51
|
||||||
|
@ -231,8 +361,22 @@ int main(){
|
||||||
Value of n: 19
|
Value of n: 19
|
||||||
The binary tree is not FULL
|
The binary tree is not FULL
|
||||||
The binary tree is not PERFECT
|
The binary tree is not PERFECT
|
||||||
*/
|
Leaf Nodes present in the binary tree are : 2 11 20 42 55
|
||||||
|
Maximum Value Present in the tree is : 55
|
||||||
|
Minimum Value Present in the tree is : 2
|
||||||
|
Binary search Tree after Deletion is :
|
||||||
|
2 4 11 20 22 37 42 51 55
|
||||||
|
|
||||||
|
Tree structure after deletion
|
||||||
|
37
|
||||||
|
/ \
|
||||||
|
11 51
|
||||||
|
/ \ / \
|
||||||
|
4 22 42 55
|
||||||
|
/ /
|
||||||
|
2 20
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
// free the memory
|
// free the memory
|
||||||
free(root);
|
free(root);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include<iostream>
|
#include<iostream>
|
||||||
|
#include<stack>
|
||||||
#define endl "\n"
|
#define endl "\n"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -49,6 +50,82 @@ void post_order(struct Node* root){
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Pre, In and Post order Traversal of a Binary Tree using Iteration */
|
||||||
|
/* Complexity for each Traversal : O(n) */
|
||||||
|
|
||||||
|
|
||||||
|
void NRpreorder(struct Node* root) //Iterative Preorder Traversal
|
||||||
|
{
|
||||||
|
stack <Node*> s;
|
||||||
|
while(!s.empty() || root!=NULL)
|
||||||
|
{
|
||||||
|
while(root!=NULL)
|
||||||
|
{
|
||||||
|
cout<<root->data<<" ";
|
||||||
|
s.push(root);
|
||||||
|
root=root->left;
|
||||||
|
}
|
||||||
|
root=s.top();
|
||||||
|
s.pop();
|
||||||
|
root=root->right;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void NRinorder(struct Node* root) //Iterative Inorder Traversal
|
||||||
|
{
|
||||||
|
stack <Node*> s;
|
||||||
|
while(!s.empty() || root!=NULL)
|
||||||
|
{
|
||||||
|
while(root!=NULL)
|
||||||
|
{
|
||||||
|
s.push(root);
|
||||||
|
root=root->left;
|
||||||
|
}
|
||||||
|
root=s.top();
|
||||||
|
s.pop();
|
||||||
|
cout<<root->data<<" ";
|
||||||
|
root=root->right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void NRpostorder(struct Node* root) //Iterative Postorder Traversal
|
||||||
|
{
|
||||||
|
stack <Node*> s1;
|
||||||
|
stack <int> s2;
|
||||||
|
while(root!=NULL)
|
||||||
|
{
|
||||||
|
s1.push(root);
|
||||||
|
s2.push(0);
|
||||||
|
root=root->left;
|
||||||
|
}
|
||||||
|
while(!s1.empty())
|
||||||
|
{
|
||||||
|
root=s1.top();
|
||||||
|
if(s2.top()==1)
|
||||||
|
{
|
||||||
|
s1.pop();
|
||||||
|
s2.pop();
|
||||||
|
cout<<root->data<<" ";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
s2.top()=1;
|
||||||
|
root=root->right;
|
||||||
|
while(root!=NULL)
|
||||||
|
{
|
||||||
|
s1.push(root);
|
||||||
|
s2.push(0);
|
||||||
|
root=root->left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
Node* root = new Node(1);
|
Node* root = new Node(1);
|
||||||
root->left = new Node(2);
|
root->left = new Node(2);
|
||||||
|
@ -63,14 +140,28 @@ int main(){
|
||||||
2 3
|
2 3
|
||||||
/ \ \
|
/ \ \
|
||||||
4 5 7
|
4 5 7
|
||||||
Expected-> pre-order : 1,2,4,5,3,7
|
Expected-> From Recursive calls :
|
||||||
in-order : 4,2,5,1,3,7
|
pre-order : 1 2 4 5 3 7
|
||||||
post-order: 4,5,2,7,3,1
|
in-order : 4 2 5 1 3 7
|
||||||
|
post-order : 4 5 2 7 3 1
|
||||||
|
|
||||||
|
From Non-Recursive/Iterative calls :
|
||||||
|
preorder : 1 2 4 5 3 7
|
||||||
|
inorder : 4 2 5 1 3 7
|
||||||
|
postorder : 4 5 2 7 3 1
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
// Tree Traversal using recursive calls
|
||||||
|
cout<<"From Recursive calls : "; cout<<endl;
|
||||||
cout<<"pre-order : "; pre_order(root); cout<<endl;
|
cout<<"pre-order : "; pre_order(root); cout<<endl;
|
||||||
cout<<"in-order : "; in_order(root); cout<<endl;
|
cout<<"in-order : "; in_order(root); cout<<endl;
|
||||||
cout<<"post-order : "; post_order(root); cout<<endl;
|
cout<<"post-order : "; post_order(root); cout<<endl<<endl;
|
||||||
|
|
||||||
|
// ~~~~~~ Tree Traversal using non-recusive/Iterative calls ~~~~~~
|
||||||
|
cout<<"From Non-Recursive/Iterative calls : "; cout<<endl;
|
||||||
|
cout<<"preorder : "; NRpreorder(root); cout<<endl;
|
||||||
|
cout<<"inorder : "; NRinorder(root); cout<<endl;
|
||||||
|
cout<<"postorder : "; NRpostorder(root); cout<<endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue