From 7afe26d5055741ff04d81ac901b721f15ee600bf Mon Sep 17 00:00:00 2001 From: Ritish Sehgal <59483233+apex-blaze@users.noreply.github.com> Date: Tue, 13 Apr 2021 20:30:41 +0530 Subject: [PATCH] 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> --- trees/README.md | 1 + trees/c-or-cpp/build-binary-tree.cpp | 99 ++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 trees/c-or-cpp/build-binary-tree.cpp diff --git a/trees/README.md b/trees/README.md index 88fa780e..69807e1d 100644 --- a/trees/README.md +++ b/trees/README.md @@ -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) diff --git a/trees/c-or-cpp/build-binary-tree.cpp b/trees/c-or-cpp/build-binary-tree.cpp new file mode 100644 index 00000000..ba524c95 --- /dev/null +++ b/trees/c-or-cpp/build-binary-tree.cpp @@ -0,0 +1,99 @@ +#include +#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<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; +} \ No newline at end of file