forked from harshilp24/Hacktoberfest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added c++ programs for tree related problems
- Loading branch information
Showing
3 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#include<iostream> | ||
#include<algorithm> // for library function sort() | ||
using namespace std; | ||
|
||
/* Defining the structue of a node */ | ||
struct node | ||
{ | ||
int data; | ||
struct node *left, *right; | ||
}; | ||
|
||
|
||
/* A function to create a node with the key passed to it */ | ||
struct node *newNode(int data) | ||
{ | ||
struct node *temp = (struct node *)malloc(sizeof(struct node)); | ||
temp->data = data; | ||
temp->left = temp->right = NULL; | ||
return temp; | ||
} | ||
|
||
/* Prints inorder traversal*/ | ||
void printInorder(struct node *root) | ||
{ | ||
if(root != NULL) | ||
{ | ||
printInorder(root->left); | ||
cout << root->data << " "; | ||
printInorder(root->right); | ||
} | ||
} | ||
|
||
/* Creates the inorder traversal array */ | ||
void createInorderArray(struct node *root, int inorder[], int *i) | ||
{ | ||
if(root != NULL) | ||
{ | ||
createInorderArray(root->left, inorder, i); | ||
inorder[*i]=root->data; | ||
(*i)++; | ||
createInorderArray(root->right, inorder, i); | ||
} | ||
} | ||
|
||
/* Counts the number of nodes in the tree, to get the size of array | ||
for creating it dynamically to store inorder traversal*/ | ||
int countNodes(struct node* root) | ||
{ | ||
if(root==NULL) | ||
return 0; | ||
// recursively returns sum of left and right sub-tree | ||
return countNodes(root->left)+countNodes(root->right)+1; | ||
} | ||
|
||
/* Change the values of the inorder traversal of tree with the given inorder traversal */ | ||
void changeNodes(struct node *root, int inorder[], int *i) | ||
{ | ||
if(root!=NULL) | ||
{ | ||
changeNodes(root->left, inorder, i); | ||
root->data=inorder[*i]; | ||
(*i)++; | ||
changeNodes(root->right, inorder, i); | ||
} | ||
} | ||
|
||
/* Converts binary tree to BST */ | ||
void bTree_to_BST(struct node* root) | ||
{ | ||
if(root==NULL) | ||
return; | ||
|
||
// creates a dynamic array to store inorder traversal of the tree | ||
int n=countNodes(root); | ||
int *in = new int[n]; | ||
int i=0; | ||
createInorderArray(root, in, &i); | ||
|
||
// Sort the array using library function | ||
sort(in, in+n); | ||
|
||
// change each element of inorder traversal | ||
// of the tree with sorted inorder array | ||
i=0; | ||
changeNodes(root, in, &i); | ||
|
||
// delete dynamically created array. | ||
delete [] in; | ||
|
||
} | ||
|
||
int main() | ||
{ | ||
struct node* root=newNode(0); | ||
root->left=newNode(1); | ||
root->right=newNode(2); | ||
root->left->left=newNode(3); | ||
root->left->right=newNode(4); | ||
root->right->left=newNode(5); | ||
root->left->left->right=newNode(6); | ||
root->right->left->right=newNode(7); | ||
root->left->left->right->right=newNode(8); | ||
/* The following tree is constructed | ||
0 | ||
/ \ | ||
1 2 | ||
/ \ / | ||
3 4 5 | ||
\ \ | ||
6 7 | ||
\ | ||
8 | ||
*/ | ||
|
||
cout << "Inorder traversal of the constructed binary tree:" << endl; | ||
printInorder(root); | ||
cout << endl; | ||
|
||
cout << "Inorder traversal of the converted binary search tree:" << endl; | ||
bTree_to_BST(root); | ||
printInorder(root); | ||
|
||
return 0; | ||
} |
108 changes: 108 additions & 0 deletions
108
C++/deepest_right_leafNode_in_BST(without_recursion).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include<iostream> | ||
#include<queue> | ||
using namespace std; | ||
|
||
/* This class encapsulates the methods used for the BST */ | ||
template<class T> | ||
class Tree | ||
{ | ||
/* Defines the structure of the node */ | ||
struct node | ||
{ | ||
T data; | ||
node *left, *right; | ||
}; | ||
node *root; | ||
/* Creates new node and assign value to it */ | ||
node* newNode(int val) | ||
{ | ||
node* temp=new node; | ||
temp->data=val; | ||
temp->left=temp->right=nullptr; | ||
|
||
return temp; | ||
} | ||
/* Utility function to get the deepest left node in a binary tree */ | ||
node* getDeepestNode(node* root) | ||
{ | ||
if(root==nullptr) | ||
return nullptr; | ||
|
||
queue<node*> q; | ||
q.push(root); | ||
|
||
node* result = nullptr; | ||
|
||
// traverse until the queue is empty | ||
while (!q.empty()) | ||
{ | ||
node* temp = q.front(); | ||
q.pop(); | ||
|
||
if (temp->left) | ||
{ | ||
q.push(temp->left); | ||
|
||
} | ||
// In level by level traversal, the last | ||
// right leaf node will be the deepest one, | ||
if (temp->right) | ||
{ | ||
temp=temp->right; | ||
q.push(temp); | ||
// if left tree is pushed, it is checked if it is a leaf | ||
if (temp->left==nullptr && temp->right==nullptr) | ||
result = temp; | ||
} | ||
} | ||
return result; | ||
} | ||
public: | ||
void construct(); | ||
void getDeepestNode(); | ||
}; | ||
|
||
/* function to construct the BST */ | ||
template<class T> | ||
void Tree<T>::construct() | ||
{ | ||
// construct a tree | ||
root = newNode(1); | ||
root->left = newNode(2); | ||
root->right = newNode(3); | ||
root->left->left = newNode(4); | ||
root->left->right = newNode(5); | ||
root->right->left = newNode(6); | ||
root->right->right = newNode(7); | ||
root->left->left->left = newNode(8); | ||
root->left->left->right = newNode(9); | ||
} | ||
|
||
template<class T> | ||
void Tree<T>::getDeepestNode() | ||
{ | ||
node* result=getDeepestNode(root); | ||
|
||
if (result) | ||
cout << "Deepest right leaf node in the binary tree is " << result->data << endl; | ||
else | ||
cout << "Right leaf not found" << endl; | ||
} | ||
|
||
int main() | ||
{ | ||
Tree<int> tree; | ||
tree.construct(); | ||
/* The following tree is constructed: | ||
1 | ||
/ \ | ||
2 3 | ||
/ \ / \ | ||
4 5 6 7 | ||
/ \ | ||
8 9 | ||
*/ | ||
tree.getDeepestNode(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
/* Defines the structure of the node */ | ||
struct node | ||
{ | ||
int data; | ||
node *left, *right; | ||
}; | ||
|
||
/* A function to create a node with the key passed to it */ | ||
struct node *newNode(int data) | ||
{ | ||
struct node *temp = (struct node *)malloc(sizeof(struct node)); | ||
temp->data = data; | ||
temp->left = temp->right = NULL; | ||
return temp; | ||
} | ||
|
||
/* Prints the inorder traversal of the BST */ | ||
void printInorder(struct node *root) | ||
{ | ||
if(root != NULL) | ||
{ | ||
printInorder(root->left); | ||
cout << root->data << " "; | ||
printInorder(root->right); | ||
} | ||
} | ||
|
||
/* Returns the index of the element of the postorder in the inorder traversal */ | ||
int getInorderIndex(int in[], int l, int h, int data) | ||
{ | ||
for(int j=l; j<h; j++) | ||
//checks until the element is found in the inorder traversal | ||
if(data==in[j]) | ||
return j; | ||
} | ||
|
||
/* A recursive function to construct the binary tree from the inorder traversal | ||
and preorder traversal */ | ||
struct node *construct(int in[], int pre[], int l, int h) | ||
{ | ||
//a static variable, so that the preIndex doesn't change throughout the | ||
//program | ||
static int preIndex=0; | ||
|
||
//creates a node with elements of preorder traversal pre[] | ||
//and index of preorder is incremented | ||
struct node *temp=newNode(pre[preIndex++]); | ||
|
||
//if the start and the end is equal, that means this node has no children | ||
if(l==h) | ||
return temp; | ||
|
||
//gets the index from the inorder traversal in[] | ||
int inoderIndex=getInorderIndex(in, l, h, temp->data); | ||
//elements to the left of the index are pushed into left sub-tree | ||
temp->left=construct(in, pre, l, inoderIndex-1); | ||
//elements to the right of the index are pushed into right sub-tree | ||
temp->right=construct(in, pre, inoderIndex+1, h); | ||
|
||
return temp; | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
int inorder[]={35,30,18,21,15,20,16,17,39,12,45}; | ||
int preorder[]={20,21,30,35,18,15,12,17,16,39,45}; | ||
int size=sizeof(preorder)/sizeof(preorder[0]); | ||
|
||
struct node *root=construct(inorder, preorder,0,size-1); | ||
|
||
cout << "Inorder traversal is:" << endl; | ||
printInorder(root); | ||
|
||
return 0; | ||
} |