From 6675545bdb58748e21e2baabfa2c5abef046b353 Mon Sep 17 00:00:00 2001 From: sri-samurai28 Date: Sat, 3 Oct 2020 00:55:12 +0530 Subject: [PATCH 1/2] added common tree traversal methods i.e inorder , preorder , postorder , levelorder --- README.md | 5 + Trees_Algo/common_tree_traversal_methods.cpp | 111 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100755 Trees_Algo/common_tree_traversal_methods.cpp diff --git a/README.md b/README.md index 39cb373..22f2fc5 100644 --- a/README.md +++ b/README.md @@ -34,3 +34,8 @@ The purpose of this repository is to get all the Algorithms required for Competi 1)Spiral search in 2d array +## Trees_Algo +1) LCA.cpp +2) Topview.cpp +3) Bottomview.cpp +4) Common_tree_traversal.cpp \ No newline at end of file diff --git a/Trees_Algo/common_tree_traversal_methods.cpp b/Trees_Algo/common_tree_traversal_methods.cpp new file mode 100755 index 0000000..05b2cfa --- /dev/null +++ b/Trees_Algo/common_tree_traversal_methods.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node +{ + int data; + struct node *left; + struct node *right; +}; + +struct node *newnode(int data) +{ + struct node *p = new (struct node); + //struct node* p = (struct node*)malloc(sizeof(struct node)); + + p->data = data; + p->left = NULL; + p->right = NULL; + return p; +} + +//Inorder (Left, Root, Right) +//Preorder (Root, Left, Right) +//Postorder (Left, Right, Root) +//Levelorder +int height(node *root) +{ + // Write your code here. + if (root == NULL) + { + return 0; + } + if (root->left == NULL && root->right == NULL) + { + return 0; + } + return std::max(height(root->left) + 1, height(root->right) + 1); +} + +int printlevel(struct node *root, int level) +{ + if (root == NULL) + return 0; + if (level == 0) + cout << root->data << " "; + else + { + printlevel(root->left, level - 1); + printlevel(root->right, level - 1); + } +} + +void levelOrder(struct node *root) + +{ + int h = height(root); + for (int i = 0; i <= h; i++) + { + printlevel(root, i); + } +} + +int preorder(struct node *root) +{ + if (root == NULL) + return 0; + cout << root->data << " "; + preorder(root->left); + preorder(root->right); +} + +int inorder(struct node *root) +{ + if (root == NULL) + return 0; + inorder(root->left); + cout << root->data << " "; + inorder(root->right); +} +int postorder(struct node *root) +{ + if (root == NULL) + return 0; + postorder(root->left); + postorder(root->right); + cout << root->data << " "; +} + +int main() +{ + struct node *root = newnode(3); + root->left = newnode(7); + root->right = newnode(5); + root->right->right = newnode(11); + root->left->left = newnode(9); + + printf("\nTree traversed in preorder=\n"); + preorder(root); + printf("\nTree traversed in inorder=\n"); + inorder(root); + printf("\nTree traversed in postorder=\n"); + postorder(root); + printf("\nTree traversed in levelorder=\n"); + levelOrder(root); + + return 0; +} From 7737c17191ab5b8b863675fe8737582d80c29696 Mon Sep 17 00:00:00 2001 From: sri-samurai28 Date: Sat, 3 Oct 2020 01:10:44 +0530 Subject: [PATCH 2/2] =?UTF-8?q?//Computes=20the=20last=20digit=20of=20=20F?= =?UTF-8?q?ibonacci=20series=20square=20sum-=20F0^2=20+=20F1^2=20+=20?= =?UTF-8?q?=C2=B7=20=C2=B7=20=C2=B7=20+=20Fn^2=20in=20constant=20time.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Mathematics_Algo/fibonacci_sum_squares.cpp | 32 ++++++++++++++++++++++ README.md | 1 + 2 files changed, 33 insertions(+) create mode 100755 Mathematics_Algo/fibonacci_sum_squares.cpp diff --git a/Mathematics_Algo/fibonacci_sum_squares.cpp b/Mathematics_Algo/fibonacci_sum_squares.cpp new file mode 100755 index 0000000..2e4e67e --- /dev/null +++ b/Mathematics_Algo/fibonacci_sum_squares.cpp @@ -0,0 +1,32 @@ +#include + +//Computes the last digit of Fibonacci series square sum- F0^2 + F1^2 + · · · + Fn^2 in constant time. +long long fibonacci_fast(int n) +{ + + long long f[n]; + f[0] = 0; + f[1] = 1; + + for (int i = 2; i <= n; i++) + { + f[i] = (f[i - 1] + f[i - 2]) % 10; + } + + return f[n]; +} +long long fib_square(long long n) +{ + if (n < 1) + return 0; + long long x = fibonacci_fast(n % 60); + long long y = fibonacci_fast((n + 1) % 60); + long long z = (x * y) % 10; + return z; +} +int main() +{ + long long n = 0; + std::cin >> n; + std::cout << fib_square(n); +} diff --git a/README.md b/README.md index 22f2fc5..5970627 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ The purpose of this repository is to get all the Algorithms required for Competi 13) totient :- Total numbers from 1 to N whose gcd with N is 1. 14) fibonacciNumber(n) :- Returns the nth Fibonacci Number in constant time. 15)Spiral Search in 2d array (C++ code) + 16) Computes the last digit of fibonacci square series F0^2 + F1^2 + · · · + Fn^2 in constant time. ## String_Algo 1) isVowel :- Check whether a character is vowel or not.