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..9eee9b3d0 --- /dev/null +++ b/Competitive Coding/Tree/Binary Tree/inorder_traversal/Readme.md @@ -0,0 +1,9 @@ +# 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) + 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); + +} + + + + + + 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..81fad41ab --- /dev/null +++ b/Competitive Coding/Tree/Binary Tree/postorder_traversal/readme.md @@ -0,0 +1,8 @@ +# 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. 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..830c585ed --- /dev/null +++ b/Competitive Coding/Tree/Binary Tree/preorder_traversal/readme.md @@ -0,0 +1,8 @@ +# 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)