Added build Binary Tree function in Cpp (#160)
* added max subarray sum * added pre,in,post traversals * Update pre-in-post-traversal.cpp formatted the output * Update pre-in-post-traversal.cpp formatted output * added build binary tree function Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com>pull/173/head
parent
3d81af66e5
commit
7afe26d505
|
@ -4,3 +4,4 @@
|
|||
|
||||
1. [Pre, In & Post Order Traversals](c-or-cpp/pre-in-post-traversal.cpp)
|
||||
2. [Level Order Traversal](c-or-cpp/level-order-traversal.cpp)
|
||||
3. [Build Binary Tree using Pre,In & Post Order](c-or-cpp/build-binary-tree.cpp)
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
#include<iostream>
|
||||
#define endl "\n"
|
||||
using namespace std;
|
||||
|
||||
|
||||
// Basic Structure of a Tree
|
||||
struct Node{
|
||||
int data;
|
||||
struct Node *left;
|
||||
struct Node *right;
|
||||
|
||||
Node(int val){
|
||||
data = val;
|
||||
left = NULL;
|
||||
right = NULL;
|
||||
}
|
||||
};
|
||||
|
||||
/* Construct a binary tree using Pre, In and Post order traversals */
|
||||
/* Complexity in Both cases : O(n^2) */
|
||||
|
||||
// Utility function to search the element in Inorder
|
||||
int search (int in[],int start ,int end,int curr){
|
||||
for(int i=start;i<=end;++i){
|
||||
if(in[i] == curr)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Build a binary Tree using Pre & In order (using Recursion)
|
||||
Node* buildTreePreIn(int pre[],int in[],int start, int end){
|
||||
static int idx = 0;
|
||||
if(start > end)
|
||||
return NULL;
|
||||
|
||||
int curr = pre[idx];
|
||||
idx++;
|
||||
Node* node = new Node(curr);
|
||||
|
||||
if(start == end)
|
||||
return node;
|
||||
|
||||
int pos = search(in,start,end,curr);
|
||||
node->left = buildTreePreIn(pre,in,start,pos-1);
|
||||
node->right = buildTreePreIn(pre,in,pos+1,end);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
// Build a binary Tree using Post & In order (using Recursion)
|
||||
Node* buildTreePostIn(int post[],int in[],int start,int end){
|
||||
static int idx = end;
|
||||
if(start > end)
|
||||
return NULL;
|
||||
|
||||
int curr = post[idx];
|
||||
idx--;
|
||||
Node* node = new Node(curr);
|
||||
|
||||
if(start == end)
|
||||
return node;
|
||||
|
||||
int pos = search(in,start,end,curr);
|
||||
node->right = buildTreePostIn(post,in,pos+1,end);
|
||||
node->left = buildTreePostIn(post,in,start,pos-1);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
// Inorder function to check if tree was built successfully!!
|
||||
void inOrder(struct Node* root){
|
||||
if(root == NULL)
|
||||
return;
|
||||
inOrder(root->left);
|
||||
cout<<root->data<<" ";
|
||||
inOrder(root->right);
|
||||
}
|
||||
|
||||
int main(){
|
||||
//n=5
|
||||
int preorder[] = {1,2,4,3,5};
|
||||
int inorder[] = {4,2,1,5,3}; // This order should match the output
|
||||
int postorder[] = {4,2,5,3,1};
|
||||
Node* t1 = buildTreePreIn(preorder,inorder,0,4);
|
||||
Node* t2 = buildTreePostIn(postorder,inorder,0,4);
|
||||
|
||||
/* Tree t
|
||||
1
|
||||
/ \
|
||||
2 3
|
||||
/ /
|
||||
4 5
|
||||
|
||||
*/
|
||||
cout<<"Output Inorder : ";
|
||||
inOrder(t2);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue