From 8f1e8121e54ece72531a0692054d405ad2ac0c1d Mon Sep 17 00:00:00 2001 From: Jatin Agrawal Date: Thu, 14 Dec 2017 18:34:19 +0530 Subject: [PATCH 1/6] inorder traversal added --- .../Binary Tree/inorder_traversal/Readme.md | 1 + .../inorder_traversal/inorder_traversal.cpp | 133 ++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 Competitive Coding/Tree/Binary Tree/inorder_traversal/Readme.md create mode 100644 Competitive Coding/Tree/Binary Tree/inorder_traversal/inorder_traversal.cpp diff --git a/Competitive Coding/Tree/Binary Tree/inorder_traversal/Readme.md b/Competitive Coding/Tree/Binary Tree/inorder_traversal/Readme.md new file mode 100644 index 000000000..5926f8db8 --- /dev/null +++ b/Competitive Coding/Tree/Binary Tree/inorder_traversal/Readme.md @@ -0,0 +1 @@ +readme.md \ No newline at end of file diff --git a/Competitive Coding/Tree/Binary Tree/inorder_traversal/inorder_traversal.cpp b/Competitive Coding/Tree/Binary Tree/inorder_traversal/inorder_traversal.cpp new file mode 100644 index 000000000..bdabd8aad --- /dev/null +++ b/Competitive Coding/Tree/Binary Tree/inorder_traversal/inorder_traversal.cpp @@ -0,0 +1,133 @@ +#include +#include +using namespace std; +#define nullptr 0 +typedef struct elem +{ + int val; + struct elem* leftchild; + struct elem* rightchild; + struct elem* parent; +}node;//This is the node for the binary tree. Each element in the binary tree contains a value, a pointer to left child,right child and parent. +class BinaryTree +{ +private: + int n=0; + node* root=nullptr; +public: + void insert(int x)//Insert function first searches for the place where the node can be inserted and then it inserts it there. + { + + if(n==0) + { + root=new node; + root->val=x; + root->leftchild=nullptr; + root->rightchild=nullptr; + root->parent=nullptr; + } + else + { + node* temp=root; + node* newnode=searchnode(x,temp); + if(x>newnode->val) + { + newnode->rightchild=new node; + newnode->rightchild->val=x; + newnode->rightchild->parent=newnode; + newnode->rightchild->leftchild=nullptr; + newnode->rightchild->rightchild=nullptr; + } + else + { + newnode->leftchild=new node; + newnode->leftchild->val=x; + newnode->leftchild->parent=newnode; + newnode->leftchild->leftchild=nullptr; + newnode->leftchild->rightchild=nullptr; + } + + } + n++; + + } + + node* searchnode(int x,node* temproot)//The search function searches for the node whose value is equal to the value it is searching for.It takes O(log n) as it is proportional to the height of the binary tree. + { + + if(temproot->valrightchild!=nullptr) + searchnode(x,temproot->rightchild); + else + return temproot; + } + else + { + if(temproot->val>x)//If value if larger it goes to the left child + { + if(temproot->leftchild!=nullptr) + searchnode(x,temproot->leftchild); + else + return temproot; + } + else + { + if(temproot->val==x)//It returns the value when it is equal + return temproot; + } + } + + } + void printtree(node* temp) + { + if(temp==nullptr) + return; + else + { + printtree(temp->leftchild); + cout<val; + printtree(temp->rightchild); + } + }//This is the function to print the tree. It prints the tree in a sorted order. + node* giveroot() + { + return root; + }//This function returns the pointer to root + void inorder_traversal(node* temp) + { + if(temp==nullptr) + return ; + else + { + inorder_traversal(temp->leftchild); + cout<val<rightchild); + } + } + +}; +int main() +{ + int n; + cin>>n; + BinaryTree bst; + for(int i=0;i>temp; + bst.insert(temp); + } + node* temp=bst.giveroot(); + + // bst.printtree(temp); + //temp = bst.giveroot(); + bst.inorder_traversal(temp); + +} + + + + + + From e8a6dfc0cccf94c31e79bf1a2d057a509fc50fb2 Mon Sep 17 00:00:00 2001 From: Jatin Agrawal Date: Thu, 14 Dec 2017 18:36:47 +0530 Subject: [PATCH 2/6] preorder traversal file added --- .../preorder_traversal/preorder_traversal.cpp | 133 ++++++++++++++++++ .../Binary Tree/preorder_traversal/readme.md | 1 + 2 files changed, 134 insertions(+) create mode 100644 Competitive Coding/Tree/Binary Tree/preorder_traversal/preorder_traversal.cpp create mode 100644 Competitive Coding/Tree/Binary Tree/preorder_traversal/readme.md diff --git a/Competitive Coding/Tree/Binary Tree/preorder_traversal/preorder_traversal.cpp b/Competitive Coding/Tree/Binary Tree/preorder_traversal/preorder_traversal.cpp new file mode 100644 index 000000000..5c7f4d30c --- /dev/null +++ b/Competitive Coding/Tree/Binary Tree/preorder_traversal/preorder_traversal.cpp @@ -0,0 +1,133 @@ +#include +#include +using namespace std; +#define nullptr 0 +typedef struct elem +{ + int val; + struct elem* leftchild; + struct elem* rightchild; + struct elem* parent; +}node;//This is the node for the binary tree. Each element in the binary tree contains a value, a pointer to left child,right child and parent. +class BinaryTree +{ +private: + int n=0; + node* root=nullptr; +public: + void insert(int x)//Insert function first searches for the place where the node can be inserted and then it inserts it there. + { + + if(n==0) + { + root=new node; + root->val=x; + root->leftchild=nullptr; + root->rightchild=nullptr; + root->parent=nullptr; + } + else + { + node* temp=root; + node* newnode=searchnode(x,temp); + if(x>newnode->val) + { + newnode->rightchild=new node; + newnode->rightchild->val=x; + newnode->rightchild->parent=newnode; + newnode->rightchild->leftchild=nullptr; + newnode->rightchild->rightchild=nullptr; + } + else + { + newnode->leftchild=new node; + newnode->leftchild->val=x; + newnode->leftchild->parent=newnode; + newnode->leftchild->leftchild=nullptr; + newnode->leftchild->rightchild=nullptr; + } + + } + n++; + + } + + node* searchnode(int x,node* temproot)//The search function searches for the node whose value is equal to the value it is searching for.It takes O(log n) as it is proportional to the height of the binary tree. + { + + if(temproot->valrightchild!=nullptr) + searchnode(x,temproot->rightchild); + else + return temproot; + } + else + { + if(temproot->val>x)//If value if larger it goes to the left child + { + if(temproot->leftchild!=nullptr) + searchnode(x,temproot->leftchild); + else + return temproot; + } + else + { + if(temproot->val==x)//It returns the value when it is equal + return temproot; + } + } + + } + void printtree(node* temp) + { + if(temp==nullptr) + return; + else + { + printtree(temp->leftchild); + cout<val; + printtree(temp->rightchild); + } + }//This is the function to print the tree. It prints the tree in a sorted order. + node* giveroot() + { + return root; + }//This function returns the pointer to root + void preorder_traversal(node* temp) + { + if(temp==nullptr) + return ; + else + { + cout<val<leftchild); + preorder_traversal(temp->rightchild); + } + } + +}; +int main() +{ + int n; + cin>>n; + BinaryTree bst; + for(int i=0;i>temp; + bst.insert(temp); + } + node* temp=bst.giveroot(); + + // bst.printtree(temp); + //temp = bst.giveroot(); + //bst.inorder_traversal(temp); + bst.preorder_traversal(temp); +} + + + + + + diff --git a/Competitive Coding/Tree/Binary Tree/preorder_traversal/readme.md b/Competitive Coding/Tree/Binary Tree/preorder_traversal/readme.md new file mode 100644 index 000000000..ea786ff2c --- /dev/null +++ b/Competitive Coding/Tree/Binary Tree/preorder_traversal/readme.md @@ -0,0 +1 @@ +readme \ No newline at end of file From 58d122072b106d62d3899328246510357236b261 Mon Sep 17 00:00:00 2001 From: Jatin Agrawal Date: Thu, 14 Dec 2017 18:38:53 +0530 Subject: [PATCH 3/6] postorder traversal added --- .../postorder_traversal.cpp | 135 ++++++++++++++++++ .../Binary Tree/postorder_traversal/readme.md | 1 + 2 files changed, 136 insertions(+) create mode 100644 Competitive Coding/Tree/Binary Tree/postorder_traversal/postorder_traversal.cpp create mode 100644 Competitive Coding/Tree/Binary Tree/postorder_traversal/readme.md diff --git a/Competitive Coding/Tree/Binary Tree/postorder_traversal/postorder_traversal.cpp b/Competitive Coding/Tree/Binary Tree/postorder_traversal/postorder_traversal.cpp new file mode 100644 index 000000000..eec9c12ff --- /dev/null +++ b/Competitive Coding/Tree/Binary Tree/postorder_traversal/postorder_traversal.cpp @@ -0,0 +1,135 @@ +#include +#include +using namespace std; +#define nullptr 0 +typedef struct elem +{ + int val; + struct elem* leftchild; + struct elem* rightchild; + struct elem* parent; +}node;//This is the node for the binary tree. Each element in the binary tree contains a value, a pointer to left child,right child and parent. +class BinaryTree +{ +private: + int n=0; + node* root=nullptr; +public: + void insert(int x)//Insert function first searches for the place where the node can be inserted and then it inserts it there. + { + + if(n==0) + { + root=new node; + root->val=x; + root->leftchild=nullptr; + root->rightchild=nullptr; + root->parent=nullptr; + } + else + { + node* temp=root; + node* newnode=searchnode(x,temp); + if(x>newnode->val) + { + newnode->rightchild=new node; + newnode->rightchild->val=x; + newnode->rightchild->parent=newnode; + newnode->rightchild->leftchild=nullptr; + newnode->rightchild->rightchild=nullptr; + } + else + { + newnode->leftchild=new node; + newnode->leftchild->val=x; + newnode->leftchild->parent=newnode; + newnode->leftchild->leftchild=nullptr; + newnode->leftchild->rightchild=nullptr; + } + + } + n++; + + } + + node* searchnode(int x,node* temproot)//The search function searches for the node whose value is equal to the value it is searching for.It takes O(log n) as it is proportional to the height of the binary tree. + { + + if(temproot->valrightchild!=nullptr) + searchnode(x,temproot->rightchild); + else + return temproot; + } + else + { + if(temproot->val>x)//If value if larger it goes to the left child + { + if(temproot->leftchild!=nullptr) + searchnode(x,temproot->leftchild); + else + return temproot; + } + else + { + if(temproot->val==x)//It returns the value when it is equal + return temproot; + } + } + + } + void printtree(node* temp) + { + if(temp==nullptr) + return; + else + { + printtree(temp->leftchild); + cout<val; + printtree(temp->rightchild); + } + }//This is the function to print the tree. It prints the tree in a sorted order. + node* giveroot() + { + return root; + }//This function returns the pointer to root + + void postorder_traversal(node* temp) + { + if(temp==nullptr) + return ; + else + { + postorder_traversal(temp->leftchild); + postorder_traversal(temp->rightchild); + cout<val<>n; + BinaryTree bst; + for(int i=0;i>temp; + bst.insert(temp); + } + node* temp=bst.giveroot(); + + // bst.printtree(temp); + //temp = bst.giveroot(); + //bst.inorder_traversal(temp); + bst.preorder_traversal(temp); +} + + + + + + diff --git a/Competitive Coding/Tree/Binary Tree/postorder_traversal/readme.md b/Competitive Coding/Tree/Binary Tree/postorder_traversal/readme.md new file mode 100644 index 000000000..ea786ff2c --- /dev/null +++ b/Competitive Coding/Tree/Binary Tree/postorder_traversal/readme.md @@ -0,0 +1 @@ +readme \ No newline at end of file From 4a2d2dc5ef3e95e662306d6b3ff3fb994516a3bc Mon Sep 17 00:00:00 2001 From: Jatin Agrawal <33143221+Jatin86400@users.noreply.github.com> Date: Thu, 14 Dec 2017 18:50:50 +0530 Subject: [PATCH 4/6] update Readme.md --- .../Tree/Binary Tree/inorder_traversal/Readme.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Competitive Coding/Tree/Binary Tree/inorder_traversal/Readme.md b/Competitive Coding/Tree/Binary Tree/inorder_traversal/Readme.md index 5926f8db8..9eee9b3d0 100644 --- a/Competitive Coding/Tree/Binary Tree/inorder_traversal/Readme.md +++ b/Competitive Coding/Tree/Binary Tree/inorder_traversal/Readme.md @@ -1 +1,9 @@ -readme.md \ No newline at end of file +# Inorder Traversal + +![alt text](https://javabeat.net/wp-content/uploads/2013/11/BST_Inorder.jpg) + +Algorithm Inorder(tree) + * 1. Traverse the left subtree, i.e., call Inorder(left-subtree) + * 2. Visit the root. + * 3. Traverse the right subtree, i.e., call Inorder(right-subtree) + From ee9738b16bd743802d4436363c3d4a91eb09b7fe Mon Sep 17 00:00:00 2001 From: Jatin Agrawal <33143221+Jatin86400@users.noreply.github.com> Date: Thu, 14 Dec 2017 18:53:40 +0530 Subject: [PATCH 5/6] updated readme.md --- .../Tree/Binary Tree/postorder_traversal/readme.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Competitive Coding/Tree/Binary Tree/postorder_traversal/readme.md b/Competitive Coding/Tree/Binary Tree/postorder_traversal/readme.md index ea786ff2c..81fad41ab 100644 --- a/Competitive Coding/Tree/Binary Tree/postorder_traversal/readme.md +++ b/Competitive Coding/Tree/Binary Tree/postorder_traversal/readme.md @@ -1 +1,8 @@ -readme \ No newline at end of file +# Postorder Traversal + +![alt text](https://www.java2blog.com/wp-content/uploads/2014/07/PostOrderTraversalBinaryTree-1.jpg) + +Algorithm Postorder(tree) + * Traverse the left subtree, i.e., call Postorder(left-subtree) + * Traverse the right subtree, i.e., call Postorder(right-subtree) + * Visit the root. From 1aeb5cee7fb7feba1d7c64cf39727baa739bd7c6 Mon Sep 17 00:00:00 2001 From: Jatin Agrawal <33143221+Jatin86400@users.noreply.github.com> Date: Thu, 14 Dec 2017 18:55:45 +0530 Subject: [PATCH 6/6] updated readme.md --- .../Tree/Binary Tree/preorder_traversal/readme.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Competitive Coding/Tree/Binary Tree/preorder_traversal/readme.md b/Competitive Coding/Tree/Binary Tree/preorder_traversal/readme.md index ea786ff2c..830c585ed 100644 --- a/Competitive Coding/Tree/Binary Tree/preorder_traversal/readme.md +++ b/Competitive Coding/Tree/Binary Tree/preorder_traversal/readme.md @@ -1 +1,8 @@ -readme \ No newline at end of file +# Preorder Traversal + +![alt text](https://javabeat.net/wp-content/uploads/2013/11/BST_Preorder.jpg) + +Algorithm Preorder(tree) + * Visit the root. + * Traverse the left subtree, i.e., call Preorder(left-subtree) + * Traverse the right subtree, i.e., call Preorder(right-subtree)