From 6523f226a97c70fe102c5f1650d73ae11052601e Mon Sep 17 00:00:00 2001 From: OO7kartik Date: Thu, 17 Oct 2019 21:41:41 +0530 Subject: [PATCH 1/2] Added tree traversals using recursion --- Data Structures/Stack/C++/Test/test | Bin Data Structures/Stack/Python/stack.py | 76 +++++++++--------- Data Structures/treeTraversalRecur/stack.c | 48 +++++++++++ Data Structures/treeTraversalRecur/stack.h | 18 +++++ Data Structures/treeTraversalRecur/tree.c | 59 ++++++++++++++ Data Structures/treeTraversalRecur/tree.h | 16 ++++ .../treeTraversalRecur/treeTraversal.c | 60 ++++++++++++++ .../Fibonacci Sequence/Fibonacci_series.txt | 56 ++++++------- .../jan-ken-puzzle/python/test/heavy/01.in | 10 +-- .../jan-ken-puzzle/python/test/heavy/02.in | 10 +-- .../jan-ken-puzzle/python/test/heavy/03.in | 10 +-- .../jan-ken-puzzle/python/test/heavy/04.in | 10 +-- .../jan-ken-puzzle/python/test/heavy/05.in | 10 +-- .../jan-ken-puzzle/python/test/heavy/06.in | 12 +-- .../jan-ken-puzzle/python/test/heavy/07.in | 10 +-- .../jan-ken-puzzle/python/test/heavy/08.in | 8 +- .../jan-ken-puzzle/python/test/heavy/09.in | 8 +- .../jan-ken-puzzle/python/test/heavy/10.in | 6 +- .../jan-ken-puzzle/python/test/light/01.in | 10 +-- .../jan-ken-puzzle/python/test/light/02.in | 10 +-- .../jan-ken-puzzle/python/test/light/03.in | 10 +-- .../jan-ken-puzzle/python/test/light/04.in | 10 +-- .../jan-ken-puzzle/python/test/light/05.in | 10 +-- .../jan-ken-puzzle/python/test/light/06.in | 10 +-- .../jan-ken-puzzle/python/test/light/07.in | 8 +- .../jan-ken-puzzle/python/test/light/08.in | 6 +- .../jan-ken-puzzle/python/test/light/09.in | 6 +- .../jan-ken-puzzle/python/test/light/10.in | 6 +- Fibonacci_series.py | 56 ++++++------- Java Design Patterns/AbstractFactory/App.java | 0 .../AbstractFactory/FactoryCar.java | 0 .../AbstractFactory/GMCFactory.java | 0 .../AbstractFactory/Minivan.java | 0 .../AbstractFactory/Pickup.java | 0 .../AbstractFactory/Savana.java | 0 .../AbstractFactory/Sienna.java | 0 .../AbstractFactory/Sierra.java | 0 .../AbstractFactory/Tacoma.java | 0 .../AbstractFactory/ToyotaFactory.java | 0 .../AbstractFactory/readme.md | 0 Java Design Patterns/DAO /DaoPatternDemo.java | 20 ----- Java Design Patterns/DAO /README.md | 14 ---- Java Design Patterns/DAO /Student.java | 25 ------ Java Design Patterns/DAO /StudentDao.java | 8 -- Java Design Patterns/DAO /StudentDaoImpl.java | 38 --------- Java Design Patterns/builder /README.md | 17 ---- Java Design Patterns/builder /builder.java | 70 ---------------- Maths/Fibonacci/C++/Fibonacci.cpp | 54 ++++++------- Maths/Fibonacci/Python/Fibonacci_series.txt | 56 ++++++------- .../Insertion Sort/Python/insertion_sort.py | 40 ++++----- .../Merge Sort/C++/Arrays/MergeSortArray.cpp | 22 ++--- .../C++/Linked Lists/MergeSortLinkedList.cpp | 16 ++-- Sorting/Selection Sort/C++/SelectionSort | Bin .../{python => Python}/selection_sort.py | 0 54 files changed, 479 insertions(+), 470 deletions(-) mode change 100755 => 100644 Data Structures/Stack/C++/Test/test create mode 100644 Data Structures/treeTraversalRecur/stack.c create mode 100644 Data Structures/treeTraversalRecur/stack.h create mode 100644 Data Structures/treeTraversalRecur/tree.c create mode 100644 Data Structures/treeTraversalRecur/tree.h create mode 100644 Data Structures/treeTraversalRecur/treeTraversal.c mode change 100755 => 100644 Java Design Patterns/AbstractFactory/App.java mode change 100755 => 100644 Java Design Patterns/AbstractFactory/FactoryCar.java mode change 100755 => 100644 Java Design Patterns/AbstractFactory/GMCFactory.java mode change 100755 => 100644 Java Design Patterns/AbstractFactory/Minivan.java mode change 100755 => 100644 Java Design Patterns/AbstractFactory/Pickup.java mode change 100755 => 100644 Java Design Patterns/AbstractFactory/Savana.java mode change 100755 => 100644 Java Design Patterns/AbstractFactory/Sienna.java mode change 100755 => 100644 Java Design Patterns/AbstractFactory/Sierra.java mode change 100755 => 100644 Java Design Patterns/AbstractFactory/Tacoma.java mode change 100755 => 100644 Java Design Patterns/AbstractFactory/ToyotaFactory.java mode change 100755 => 100644 Java Design Patterns/AbstractFactory/readme.md delete mode 100644 Java Design Patterns/DAO /DaoPatternDemo.java delete mode 100644 Java Design Patterns/DAO /README.md delete mode 100644 Java Design Patterns/DAO /Student.java delete mode 100644 Java Design Patterns/DAO /StudentDao.java delete mode 100644 Java Design Patterns/DAO /StudentDaoImpl.java delete mode 100644 Java Design Patterns/builder /README.md delete mode 100644 Java Design Patterns/builder /builder.java mode change 100755 => 100644 Sorting/Selection Sort/C++/SelectionSort rename Sorting/Selection Sort/{python => Python}/selection_sort.py (100%) diff --git a/Data Structures/Stack/C++/Test/test b/Data Structures/Stack/C++/Test/test old mode 100755 new mode 100644 diff --git a/Data Structures/Stack/Python/stack.py b/Data Structures/Stack/Python/stack.py index 5dd8d953..86e25f87 100644 --- a/Data Structures/Stack/Python/stack.py +++ b/Data Structures/Stack/Python/stack.py @@ -1,38 +1,38 @@ - - -class Stack: - - def __init__(self, size = 1000000): - self.stack = [] - self.stack_size = size - - # Add element to stack - def push(self, element): - # Checking stack is full or not - if len(self.stack) <= self.stack_size: - self.stack.append(element) - else: - print('Stack Full. Overflow') - - # Delete element from stack - def pop(self): - if len(self.stack) == 0: - print('Empty Stack. Underflow') - else: - self.stack.pop() - - # Returns top element of stack - def top(self): - return self.stack[-1] - - -# Driver Program -if __name__ == '__main__': - stack = Stack() - stack.pop() - stack.push(1) - stack.push(2) - stack.push(3) - print(stack.top()) - stack.pop() - print(stack.top()) + + +class Stack: + + def __init__(self, size = 1000000): + self.stack = [] + self.stack_size = size + + # Add element to stack + def push(self, element): + # Checking stack is full or not + if len(self.stack) <= self.stack_size: + self.stack.append(element) + else: + print('Stack Full. Overflow') + + # Delete element from stack + def pop(self): + if len(self.stack) == 0: + print('Empty Stack. Underflow') + else: + self.stack.pop() + + # Returns top element of stack + def top(self): + return self.stack[-1] + + +# Driver Program +if __name__ == '__main__': + stack = Stack() + stack.pop() + stack.push(1) + stack.push(2) + stack.push(3) + print(stack.top()) + stack.pop() + print(stack.top()) diff --git a/Data Structures/treeTraversalRecur/stack.c b/Data Structures/treeTraversalRecur/stack.c new file mode 100644 index 00000000..14c9fbe2 --- /dev/null +++ b/Data Structures/treeTraversalRecur/stack.c @@ -0,0 +1,48 @@ +#include +#include + +#include "stack.h" +#include "tree.h" + +node *getNode(tree *c) +{ + node *nn = (node *)malloc(sizeof(node)); + nn->next = NULL; + nn->top = c; + return nn; +} + +int isempty(node *st) +{ + if (st == NULL) + return 1; + return 0; +} + +void push(tree *adr, node **st) +{ + node *nn = getNode(adr); + nn->next = (*st); + (*st) = nn; +} + +void pop(node **st) +{ + if (isempty(*st)) + { + printf("Stack empty can't pop"); + } + node *temp = (*st); + (*st) = (*st)->next; + free(temp); +} + +void display(node *st) +{ + while (!isempty(st)) + { + printf("%c ", st->top); + st = st->next; + } + printf("\n"); +} diff --git a/Data Structures/treeTraversalRecur/stack.h b/Data Structures/treeTraversalRecur/stack.h new file mode 100644 index 00000000..6083765c --- /dev/null +++ b/Data Structures/treeTraversalRecur/stack.h @@ -0,0 +1,18 @@ +#ifndef STACK_HEADER +#define STACK_HEADER + +#include "tree.h" + +typedef struct Node +{ + tree *top; + struct Node *next; +} node; + +node *getNode(tree *c); +void display(node *st); +int isempty(node *st); +void push(tree *adr, node **st); +void pop(node **st); + +#endif \ No newline at end of file diff --git a/Data Structures/treeTraversalRecur/tree.c b/Data Structures/treeTraversalRecur/tree.c new file mode 100644 index 00000000..dbbd601c --- /dev/null +++ b/Data Structures/treeTraversalRecur/tree.c @@ -0,0 +1,59 @@ +#include +#include + +#include "tree.h" + +tree *getTreeNode(int data) +{ + tree *nn = (tree *)malloc(sizeof(tree)); + nn->left = nn->right = NULL; + nn->data = data; + return nn; +} + +tree *insert(tree *root, int data) +{ + if (!root) + { + return getTreeNode(data); + } + else if (root->data < data) + { + root->right = insert(root->right, data); + } + else + { + root->left = insert(root->left, data); + } + return root; +} + +void inorder(tree *root) +{ + if (root) + { + inorder(root->left); + printf("%d ", root->data); + inorder(root->right); + } +} + +void preorder(tree *root) +{ + if (root) + { + printf("%d ", root->data); + preorder(root->left); + preorder(root->right); + } +} + +void postorder(tree *root) +{ + if (root) + { + postorder(root->left); + postorder(root->right); + printf("%d ", root->data); + } +} diff --git a/Data Structures/treeTraversalRecur/tree.h b/Data Structures/treeTraversalRecur/tree.h new file mode 100644 index 00000000..618636b6 --- /dev/null +++ b/Data Structures/treeTraversalRecur/tree.h @@ -0,0 +1,16 @@ +#ifndef TREE_HEADER +#define TREE_HEADER + +typedef struct Tree +{ + int data; + struct Tree *left, *right; +} tree; + +tree *insert(tree *root, int data); +void inorder(tree *root); +void postorder(tree *root); +void preorder(tree *root); +void inorder(tree *root); + +#endif \ No newline at end of file diff --git a/Data Structures/treeTraversalRecur/treeTraversal.c b/Data Structures/treeTraversalRecur/treeTraversal.c new file mode 100644 index 00000000..5636cdab --- /dev/null +++ b/Data Structures/treeTraversalRecur/treeTraversal.c @@ -0,0 +1,60 @@ +// to run type "gcc treeTraversal.c tree.c stack.c" +// then run the executable file and enter your input + +#include + +#include "tree.h" +#include "stack.h" + +void printstack(node *stack) +{ + printf("stack: "); + while (!isempty(stack)) + { + printf("%p ", stack->top->data); + } + printf("\n"); +} + +int main() +{ + tree *root = NULL; + + int n, val; + printf("Enter number of nodes: "); + scanf("%d", &n); + + for (int i = 0; i < n; i++) + { + printf("Enter node %d: ", i+1); + scanf("%d", &val); + root = insert(root, val); + } + + //using binary search tree. + //for an input of 2 1 4 3 5 + // 2 + // / \ + // 1 3 + // / \ + // 4 5 + + // inorder: 1 2 3 4 5 + // preorder: 2 1 4 3 5 + // postorder: 1 3 5 4 2 + + + printf("inorder: "); + inorder(root); + printf("\n"); + + printf("preorder: "); + preorder(root); + printf("\n"); + + printf("postorder: "); + postorder(root); + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/Dynamic Programming/Fibonacci Sequence/Fibonacci_series.txt b/Dynamic Programming/Fibonacci Sequence/Fibonacci_series.txt index b1f557f8..472b4f77 100644 --- a/Dynamic Programming/Fibonacci Sequence/Fibonacci_series.txt +++ b/Dynamic Programming/Fibonacci Sequence/Fibonacci_series.txt @@ -1,28 +1,28 @@ -# Program to display the Fibonacci sequence up to n-th term where n is provided by the user - -# change this value for a different result -nterms = int (input("enter the no. of terms " )) - -# uncomment to take input from the user -#nterms = int(input("How many terms? ")) - -# first two terms -n1 = 0 -n2 = 1 -count = 0 - -# check if the number of terms is valid -if nterms <= 0: - print("Please enter a positive integer") -elif nterms == 1: - print("Fibonacci sequence upto",nterms,":") - print(n1) -else: - print("Fibonacci sequence upto",nterms,":") - while count < nterms: - print(n1,end=' , ') - nth = n1 + n2 - # update values - n1 = n2 - n2 = nth - count += 1 +# Program to display the Fibonacci sequence up to n-th term where n is provided by the user + +# change this value for a different result +nterms = int (input("enter the no. of terms " )) + +# uncomment to take input from the user +#nterms = int(input("How many terms? ")) + +# first two terms +n1 = 0 +n2 = 1 +count = 0 + +# check if the number of terms is valid +if nterms <= 0: + print("Please enter a positive integer") +elif nterms == 1: + print("Fibonacci sequence upto",nterms,":") + print(n1) +else: + print("Fibonacci sequence upto",nterms,":") + while count < nterms: + print(n1,end=' , ') + nth = n1 + n2 + # update values + n1 = n2 + n2 = nth + count += 1 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/01.in b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/01.in index 88d73ff8..28254cd1 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/01.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/01.in @@ -1,5 +1,5 @@ -4 4 -2 3 3 3 -1 3 2 3 -2 2 1 2 -1 2 1 1 +4 4 +2 3 3 3 +1 3 2 3 +2 2 1 2 +1 2 1 1 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/02.in b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/02.in index e1aff55c..a305a0c0 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/02.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/02.in @@ -1,5 +1,5 @@ -4 5 -1 1 2 2 1 -1 3 1 1 1 -3 3 1 3 3 -0 0 3 2 0 +4 5 +1 1 2 2 1 +1 3 1 1 1 +3 3 1 3 3 +0 0 3 2 0 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/03.in b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/03.in index 6b0c36d4..f650f397 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/03.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/03.in @@ -1,5 +1,5 @@ -4 5 -1 2 1 3 1 -2 2 3 3 1 -2 1 3 0 3 -1 1 1 0 2 +4 5 +1 2 1 3 1 +2 2 3 3 1 +2 1 3 0 3 +1 1 1 0 2 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/04.in b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/04.in index 0efdafdf..729fec43 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/04.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/04.in @@ -1,5 +1,5 @@ -4 5 -2 3 1 3 1 -3 1 2 2 2 -2 3 1 3 2 -1 3 2 1 0 +4 5 +2 3 1 3 1 +3 1 2 2 2 +2 3 1 3 2 +1 3 2 1 0 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/05.in b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/05.in index 8442a416..3d067a39 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/05.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/05.in @@ -1,5 +1,5 @@ -4 5 -3 1 2 2 1 -1 2 2 2 1 -1 2 1 1 3 -3 1 3 3 2 +4 5 +3 1 2 2 1 +1 2 2 2 1 +1 2 1 1 3 +3 1 3 3 2 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/06.in b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/06.in index d73e8fd9..a66a645b 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/06.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/06.in @@ -1,6 +1,6 @@ -5 5 -2 3 3 3 2 -2 3 2 3 2 -0 1 2 2 2 -0 2 2 1 1 -0 1 0 3 1 +5 5 +2 3 3 3 2 +2 3 2 3 2 +0 1 2 2 2 +0 2 2 1 1 +0 1 0 3 1 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/07.in b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/07.in index 9730680b..8e2e2490 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/07.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/07.in @@ -1,5 +1,5 @@ -4 6 -1 3 2 3 3 0 -3 2 2 3 1 3 -1 2 2 3 2 0 -1 2 1 3 3 2 +4 6 +1 3 2 3 3 0 +3 2 2 3 1 3 +1 2 2 3 2 0 +1 2 1 3 3 2 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/08.in b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/08.in index 18fa10cc..cb86a187 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/08.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/08.in @@ -1,4 +1,4 @@ -3 8 -1 3 3 1 1 1 1 0 -1 3 2 2 1 1 2 3 -3 2 2 2 3 2 1 1 +3 8 +1 3 3 1 1 1 1 0 +1 3 2 2 1 1 2 3 +3 2 2 2 3 2 1 1 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/09.in b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/09.in index 1e8305f5..29e2c45b 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/09.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/09.in @@ -1,4 +1,4 @@ -3 8 -3 3 3 3 1 3 1 3 -2 2 2 1 3 3 1 0 -1 3 2 3 3 1 1 2 +3 8 +3 3 3 3 1 3 1 3 +2 2 2 1 3 3 1 0 +1 3 2 3 3 1 1 2 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/10.in b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/10.in index 28df6724..27168e09 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/heavy/10.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/heavy/10.in @@ -1,3 +1,3 @@ -2 3 -0 0 3 -2 3 3 +2 3 +0 0 3 +2 3 3 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/light/01.in b/Dynamic Programming/jan-ken-puzzle/python/test/light/01.in index 098d6d87..a670f543 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/light/01.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/light/01.in @@ -1,5 +1,5 @@ -4 4 -1 0 0 0 -2 3 2 1 -2 2 2 1 -2 2 2 2 +4 4 +1 0 0 0 +2 3 2 1 +2 2 2 1 +2 2 2 2 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/light/02.in b/Dynamic Programming/jan-ken-puzzle/python/test/light/02.in index 8c25a41a..2b640f69 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/light/02.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/light/02.in @@ -1,5 +1,5 @@ -4 4 -2 1 3 3 -0 3 3 1 -0 3 1 2 -1 2 1 1 +4 4 +2 1 3 3 +0 3 3 1 +0 3 1 2 +1 2 1 1 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/light/03.in b/Dynamic Programming/jan-ken-puzzle/python/test/light/03.in index 38a1c5a8..6deebc7c 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/light/03.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/light/03.in @@ -1,5 +1,5 @@ -4 4 -1 1 1 3 -1 2 2 2 -2 2 1 1 -2 2 1 0 +4 4 +1 1 1 3 +1 2 2 2 +2 2 1 1 +2 2 1 0 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/light/04.in b/Dynamic Programming/jan-ken-puzzle/python/test/light/04.in index 14e01fe6..bc30e569 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/light/04.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/light/04.in @@ -1,5 +1,5 @@ -4 4 -1 2 1 3 -2 3 2 1 -2 2 1 3 -2 3 3 2 +4 4 +1 2 1 3 +2 3 2 1 +2 2 1 3 +2 3 3 2 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/light/05.in b/Dynamic Programming/jan-ken-puzzle/python/test/light/05.in index 915810ea..6df68106 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/light/05.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/light/05.in @@ -1,5 +1,5 @@ -4 5 -2 0 2 0 2 -3 3 1 1 3 -3 2 3 3 3 -3 3 3 2 0 +4 5 +2 0 2 0 2 +3 3 1 1 3 +3 2 3 3 3 +3 3 3 2 0 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/light/06.in b/Dynamic Programming/jan-ken-puzzle/python/test/light/06.in index a2ca83e2..8960c508 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/light/06.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/light/06.in @@ -1,5 +1,5 @@ -4 5 -1 1 2 2 1 -3 2 3 1 3 -0 3 3 1 3 -2 1 1 2 0 +4 5 +1 1 2 2 1 +3 2 3 1 3 +0 3 3 1 3 +2 1 1 2 0 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/light/07.in b/Dynamic Programming/jan-ken-puzzle/python/test/light/07.in index 5df70701..c68df9cc 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/light/07.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/light/07.in @@ -1,4 +1,4 @@ -3 8 -0 1 3 1 1 2 2 2 -0 2 2 3 1 1 1 2 -0 0 0 2 1 2 1 1 +3 8 +0 1 3 1 1 2 2 2 +0 2 2 3 1 1 1 2 +0 0 0 2 1 2 1 1 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/light/08.in b/Dynamic Programming/jan-ken-puzzle/python/test/light/08.in index 3489c543..b8c30363 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/light/08.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/light/08.in @@ -1,3 +1,3 @@ -2 11 -1 1 1 2 3 2 2 2 1 0 0 -2 1 1 3 2 2 3 3 3 3 2 +2 11 +1 1 1 2 3 2 2 2 1 0 0 +2 1 1 3 2 2 3 3 3 3 2 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/light/09.in b/Dynamic Programming/jan-ken-puzzle/python/test/light/09.in index e81638dc..e6693fdd 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/light/09.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/light/09.in @@ -1,3 +1,3 @@ -2 11 -0 2 1 3 2 1 1 1 1 3 3 -2 3 2 2 2 2 3 3 3 1 2 +2 11 +0 2 1 3 2 1 1 1 1 3 3 +2 3 2 2 2 2 3 3 3 1 2 diff --git a/Dynamic Programming/jan-ken-puzzle/python/test/light/10.in b/Dynamic Programming/jan-ken-puzzle/python/test/light/10.in index 28df6724..27168e09 100644 --- a/Dynamic Programming/jan-ken-puzzle/python/test/light/10.in +++ b/Dynamic Programming/jan-ken-puzzle/python/test/light/10.in @@ -1,3 +1,3 @@ -2 3 -0 0 3 -2 3 3 +2 3 +0 0 3 +2 3 3 diff --git a/Fibonacci_series.py b/Fibonacci_series.py index b1f557f8..472b4f77 100644 --- a/Fibonacci_series.py +++ b/Fibonacci_series.py @@ -1,28 +1,28 @@ -# Program to display the Fibonacci sequence up to n-th term where n is provided by the user - -# change this value for a different result -nterms = int (input("enter the no. of terms " )) - -# uncomment to take input from the user -#nterms = int(input("How many terms? ")) - -# first two terms -n1 = 0 -n2 = 1 -count = 0 - -# check if the number of terms is valid -if nterms <= 0: - print("Please enter a positive integer") -elif nterms == 1: - print("Fibonacci sequence upto",nterms,":") - print(n1) -else: - print("Fibonacci sequence upto",nterms,":") - while count < nterms: - print(n1,end=' , ') - nth = n1 + n2 - # update values - n1 = n2 - n2 = nth - count += 1 +# Program to display the Fibonacci sequence up to n-th term where n is provided by the user + +# change this value for a different result +nterms = int (input("enter the no. of terms " )) + +# uncomment to take input from the user +#nterms = int(input("How many terms? ")) + +# first two terms +n1 = 0 +n2 = 1 +count = 0 + +# check if the number of terms is valid +if nterms <= 0: + print("Please enter a positive integer") +elif nterms == 1: + print("Fibonacci sequence upto",nterms,":") + print(n1) +else: + print("Fibonacci sequence upto",nterms,":") + while count < nterms: + print(n1,end=' , ') + nth = n1 + n2 + # update values + n1 = n2 + n2 = nth + count += 1 diff --git a/Java Design Patterns/AbstractFactory/App.java b/Java Design Patterns/AbstractFactory/App.java old mode 100755 new mode 100644 diff --git a/Java Design Patterns/AbstractFactory/FactoryCar.java b/Java Design Patterns/AbstractFactory/FactoryCar.java old mode 100755 new mode 100644 diff --git a/Java Design Patterns/AbstractFactory/GMCFactory.java b/Java Design Patterns/AbstractFactory/GMCFactory.java old mode 100755 new mode 100644 diff --git a/Java Design Patterns/AbstractFactory/Minivan.java b/Java Design Patterns/AbstractFactory/Minivan.java old mode 100755 new mode 100644 diff --git a/Java Design Patterns/AbstractFactory/Pickup.java b/Java Design Patterns/AbstractFactory/Pickup.java old mode 100755 new mode 100644 diff --git a/Java Design Patterns/AbstractFactory/Savana.java b/Java Design Patterns/AbstractFactory/Savana.java old mode 100755 new mode 100644 diff --git a/Java Design Patterns/AbstractFactory/Sienna.java b/Java Design Patterns/AbstractFactory/Sienna.java old mode 100755 new mode 100644 diff --git a/Java Design Patterns/AbstractFactory/Sierra.java b/Java Design Patterns/AbstractFactory/Sierra.java old mode 100755 new mode 100644 diff --git a/Java Design Patterns/AbstractFactory/Tacoma.java b/Java Design Patterns/AbstractFactory/Tacoma.java old mode 100755 new mode 100644 diff --git a/Java Design Patterns/AbstractFactory/ToyotaFactory.java b/Java Design Patterns/AbstractFactory/ToyotaFactory.java old mode 100755 new mode 100644 diff --git a/Java Design Patterns/AbstractFactory/readme.md b/Java Design Patterns/AbstractFactory/readme.md old mode 100755 new mode 100644 diff --git a/Java Design Patterns/DAO /DaoPatternDemo.java b/Java Design Patterns/DAO /DaoPatternDemo.java deleted file mode 100644 index 09db9922..00000000 --- a/Java Design Patterns/DAO /DaoPatternDemo.java +++ /dev/null @@ -1,20 +0,0 @@ -public class DaoPatternDemo { - public static void main(String[] args) { - StudentDao studentDao = new StudentDaoImpl(); - - //print all students - for (Student student : studentDao.getAllStudents()) { - System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]"); - } - - - //update student - Student student =studentDao.getAllStudents().get(0); - student.setName("Michael"); - studentDao.updateStudent(student); - - //get the student - studentDao.getStudent(0); - System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]"); - } -} \ No newline at end of file diff --git a/Java Design Patterns/DAO /README.md b/Java Design Patterns/DAO /README.md deleted file mode 100644 index badba483..00000000 --- a/Java Design Patterns/DAO /README.md +++ /dev/null @@ -1,14 +0,0 @@ -## Data Access Object Pattern aka DAO - -Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or operations from high level business services. Following are the participants in Data Access Object Pattern. - -> **Data Access Object Interface** - This interface defines the standard operations to be performed on a model object(s). - -> **Data Access Object concrete class** - This class implements above interface. This class is responsible to get data from a data source which can be database / xml or any other storage mechanism. - -> **Model Object or Value Object** - This object is simple POJO containing get/set methods to store data retrieved using DAO class. - -We are going to create a Student object acting as a Model or Value Object.StudentDao is -Data Access Object Interface.StudentDaoImpl is concrete class implementing Data Access Object Interface. -DaoPatternDemo, our demo class, will use StudentDao to demonstrate the use of Data Access Object pattern. - diff --git a/Java Design Patterns/DAO /Student.java b/Java Design Patterns/DAO /Student.java deleted file mode 100644 index f82409e2..00000000 --- a/Java Design Patterns/DAO /Student.java +++ /dev/null @@ -1,25 +0,0 @@ -public class Student { - private String name; - private int rollNo; - - Student(String name, int rollNo){ - this.name = name; - this.rollNo = rollNo; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getRollNo() { - return rollNo; - } - - public void setRollNo(int rollNo) { - this.rollNo = rollNo; - } -} \ No newline at end of file diff --git a/Java Design Patterns/DAO /StudentDao.java b/Java Design Patterns/DAO /StudentDao.java deleted file mode 100644 index d1a7a511..00000000 --- a/Java Design Patterns/DAO /StudentDao.java +++ /dev/null @@ -1,8 +0,0 @@ -import java.util.List; - -public interface StudentDao { - public List getAllStudents(); - public Student getStudent(int rollNo); - public void updateStudent(Student student); - public void deleteStudent(Student student); -} \ No newline at end of file diff --git a/Java Design Patterns/DAO /StudentDaoImpl.java b/Java Design Patterns/DAO /StudentDaoImpl.java deleted file mode 100644 index 1c1663c0..00000000 --- a/Java Design Patterns/DAO /StudentDaoImpl.java +++ /dev/null @@ -1,38 +0,0 @@ -import java.util.ArrayList; -import java.util.List; - -public class StudentDaoImpl implements StudentDao { - - //list is working as a database - List students; - - public StudentDaoImpl(){ - students = new ArrayList(); - Student student1 = new Student("Robert",0); - Student student2 = new Student("John",1); - students.add(student1); - students.add(student2); - } - @Override - public void deleteStudent(Student student) { - students.remove(student.getRollNo()); - System.out.println("Student: Roll No " + student.getRollNo() + ", deleted from database"); - } - - //retrive list of students from the database - @Override - public List getAllStudents() { - return students; - } - - @Override - public Student getStudent(int rollNo) { - return students.get(rollNo); - } - - @Override - public void updateStudent(Student student) { - students.get(student.getRollNo()).setName(student.getName()); - System.out.println("Student: Roll No " + student.getRollNo() + ", updated in the database"); - } -} \ No newline at end of file diff --git a/Java Design Patterns/builder /README.md b/Java Design Patterns/builder /README.md deleted file mode 100644 index fdf97f1d..00000000 --- a/Java Design Patterns/builder /README.md +++ /dev/null @@ -1,17 +0,0 @@ -## Builder Pattern - Builder pattern was introduced to solve some of the problems with Factory and Abstract Factory design patterns when the Object contains a lot of attributes. - There are three major issues with Factory and Abstract Factory design patterns when the Object contains a lot of attributes. - -> Too Many arguments to pass from client program to the Factory class that can be error prone because most of the time, the type of arguments are same and from client side its hard to maintain the order of the argument. -> Some of the parameters might be optional but in Factory pattern, we are forced to send all the parameters and optional parameters need to send as NULL. -> If the object is heavy and its creation is complex, then all that complexity will be part of Factory classes that is confusing. - - We can solve the issues with large number of parameters by providing a constructor with required parameters and then different setter methods to set the optional parameters. The problem with this approach is that the Object state will be inconsistent until unless all the attributes are set explicitly. - Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object. - -## How to implement the design pattern in Java - -> First of all you need to create a static nested class and then copy all the arguments from the outer class to the Builder class. We should follow the naming convention and if the class name is Computer then builder class should be named as ComputerBuilder. -> Java Builder class should have a public constructor with all the required attributes as parameters. -> Java Builder class should have methods to set the optional parameters and it should return the same Builder object after setting the optional attribute. -> The final step is to provide a build() method in the builder class that will return the Object needed by client program. For this we need to have a private constructor in the Class with Builder class as argument. diff --git a/Java Design Patterns/builder /builder.java b/Java Design Patterns/builder /builder.java deleted file mode 100644 index 485c9171..00000000 --- a/Java Design Patterns/builder /builder.java +++ /dev/null @@ -1,70 +0,0 @@ - -class Computer { - - //required parameters - private String HDD; - private String RAM; - - //optional parameters - private boolean isGraphicsCardEnabled; - private boolean isBluetoothEnabled; - public String getHDD() { - return HDD; - } - public String getRAM() { - return RAM; - } - public boolean isGraphicsCardEnabled() { - return isGraphicsCardEnabled; - } - public boolean isBluetoothEnabled() { - return isBluetoothEnabled; - } - private Computer(ComputerBuilder builder) { - this.HDD=builder.HDD; - this.RAM=builder.RAM; - this.isGraphicsCardEnabled=builder.isGraphicsCardEnabled; - this.isBluetoothEnabled=builder.isBluetoothEnabled; - } - - //Builder Class - public static class ComputerBuilder{ - - // required parameters - private String HDD; - private String RAM; - - // optional parameters - private boolean isGraphicsCardEnabled; - private boolean isBluetoothEnabled; - - public ComputerBuilder(String hdd, String ram){ - this.HDD=hdd; - this.RAM=ram; - } - public ComputerBuilder setGraphicsCardEnabled(boolean isGraphicsCardEnabled) { - this.isGraphicsCardEnabled = isGraphicsCardEnabled; - return this; - } - public ComputerBuilder setBluetoothEnabled(boolean isBluetoothEnabled) { - this.isBluetoothEnabled = isBluetoothEnabled; - return this; - } - public Computer build(){ - return new Computer(this); - } - } -} - - - - -public class TestBuilderPattern { - public static void main(String[] args) { - // Using builder to get the object in a single line of code and - // without any inconsistent state or arguments management issues - Computer comp = new Computer.ComputerBuilder( - "500 GB", "2 GB").setBluetoothEnabled(true) - .setGraphicsCardEnabled(true).build(); - } -} diff --git a/Maths/Fibonacci/C++/Fibonacci.cpp b/Maths/Fibonacci/C++/Fibonacci.cpp index 61a8d341..0ba6762f 100644 --- a/Maths/Fibonacci/C++/Fibonacci.cpp +++ b/Maths/Fibonacci/C++/Fibonacci.cpp @@ -1,27 +1,27 @@ -#include - -using namespace std; - -void fibonacci(int number) -{ - int first = 0, second = 1, next; - - for (int i = 0; i < number; i++) - { - cout << "\n" << first; - next = first + second; - first = second; - second = next; - } -} - -int main() -{ - int number; - - cout << "Enter number of terms for Series: "; - cin >> number; - - cout << "Fibonacci Series are: \n"; - fibonacci(number); -} +#include + +using namespace std; + +void fibonacci(int number) +{ + int first = 0, second = 1, next; + + for (int i = 0; i < number; i++) + { + cout << "\n" << first; + next = first + second; + first = second; + second = next; + } +} + +int main() +{ + int number; + + cout << "Enter number of terms for Series: "; + cin >> number; + + cout << "Fibonacci Series are: \n"; + fibonacci(number); +} diff --git a/Maths/Fibonacci/Python/Fibonacci_series.txt b/Maths/Fibonacci/Python/Fibonacci_series.txt index b1f557f8..472b4f77 100644 --- a/Maths/Fibonacci/Python/Fibonacci_series.txt +++ b/Maths/Fibonacci/Python/Fibonacci_series.txt @@ -1,28 +1,28 @@ -# Program to display the Fibonacci sequence up to n-th term where n is provided by the user - -# change this value for a different result -nterms = int (input("enter the no. of terms " )) - -# uncomment to take input from the user -#nterms = int(input("How many terms? ")) - -# first two terms -n1 = 0 -n2 = 1 -count = 0 - -# check if the number of terms is valid -if nterms <= 0: - print("Please enter a positive integer") -elif nterms == 1: - print("Fibonacci sequence upto",nterms,":") - print(n1) -else: - print("Fibonacci sequence upto",nterms,":") - while count < nterms: - print(n1,end=' , ') - nth = n1 + n2 - # update values - n1 = n2 - n2 = nth - count += 1 +# Program to display the Fibonacci sequence up to n-th term where n is provided by the user + +# change this value for a different result +nterms = int (input("enter the no. of terms " )) + +# uncomment to take input from the user +#nterms = int(input("How many terms? ")) + +# first two terms +n1 = 0 +n2 = 1 +count = 0 + +# check if the number of terms is valid +if nterms <= 0: + print("Please enter a positive integer") +elif nterms == 1: + print("Fibonacci sequence upto",nterms,":") + print(n1) +else: + print("Fibonacci sequence upto",nterms,":") + while count < nterms: + print(n1,end=' , ') + nth = n1 + n2 + # update values + n1 = n2 + n2 = nth + count += 1 diff --git a/Sorting/Insertion Sort/Python/insertion_sort.py b/Sorting/Insertion Sort/Python/insertion_sort.py index 727b8d1d..a4235c7a 100644 --- a/Sorting/Insertion Sort/Python/insertion_sort.py +++ b/Sorting/Insertion Sort/Python/insertion_sort.py @@ -1,21 +1,21 @@ -def insertion_sort(arr): - ''' - Args: Input list - Returns: Sorted list - ''' - for i in range(1, len(arr)): - key = arr[i] - j = i-1 - # finding element greater than current value - while(j>=0 and arr[j]>key): - arr[j+1] = arr[j] - j -= 1 - arr[j+1] = key - - return arr - -# Driver Program -if __name__ == '__main__': - arr = [5,3,9,6,1,8] - arr = insertion_sort(arr) +def insertion_sort(arr): + ''' + Args: Input list + Returns: Sorted list + ''' + for i in range(1, len(arr)): + key = arr[i] + j = i-1 + # finding element greater than current value + while(j>=0 and arr[j]>key): + arr[j+1] = arr[j] + j -= 1 + arr[j+1] = key + + return arr + +# Driver Program +if __name__ == '__main__': + arr = [5,3,9,6,1,8] + arr = insertion_sort(arr) print(arr) \ No newline at end of file diff --git a/Sorting/Merge Sort/C++/Arrays/MergeSortArray.cpp b/Sorting/Merge Sort/C++/Arrays/MergeSortArray.cpp index 47d4abf9..e63d555f 100644 --- a/Sorting/Merge Sort/C++/Arrays/MergeSortArray.cpp +++ b/Sorting/Merge Sort/C++/Arrays/MergeSortArray.cpp @@ -18,7 +18,7 @@ void printArray(T *arr, N left, N right) { } template -MergeSortArray::MergeSortArray() { +MergeSortArray::MergeSortArray() { newArray = nullptr; } @@ -57,15 +57,15 @@ void MergeSortArray::sort(T* arr, N left, N right) { } template -void MergeSortArray::mergeTheArray(T* arr, N left, N mid, N right) { -#ifdef DEBUG - std::cout << "Merging the array: " << std::endl; - std::cout << "Left start: " << left << " Mid: " << mid << " Right end : " << right << std::endl; -#endif - // Left chunk is arr[left....mid] - // Right chunk is arr[mid+1....right] - - N k = left; +void MergeSortArray::mergeTheArray(T* arr, N left, N mid, N right) { +#ifdef DEBUG + std::cout << "Merging the array: " << std::endl; + std::cout << "Left start: " << left << " Mid: " << mid << " Right end : " << right << std::endl; +#endif + // Left chunk is arr[left....mid] + // Right chunk is arr[mid+1....right] + + N k = left; N i = left; N j = mid+1; // Compare left and right chunk @@ -105,6 +105,6 @@ void MergeSortArray::mergeTheArray(T* arr, N left, N mid, N right) { } template -MergeSortArray::~MergeSortArray() { +MergeSortArray::~MergeSortArray() { delete[] newArray; } diff --git a/Sorting/Merge Sort/C++/Linked Lists/MergeSortLinkedList.cpp b/Sorting/Merge Sort/C++/Linked Lists/MergeSortLinkedList.cpp index 25be65fe..2c8d6767 100644 --- a/Sorting/Merge Sort/C++/Linked Lists/MergeSortLinkedList.cpp +++ b/Sorting/Merge Sort/C++/Linked Lists/MergeSortLinkedList.cpp @@ -164,13 +164,13 @@ void MergeSortLinkedList::sort(Node*& Head) { } template -Node* MergeSortLinkedList::mergeSortedLists(Node* leftHead, Node* rightHead) { -#ifdef DEBUG - std::cout << "Merging the array: " << std::endl; -#endif - Node* result = new Node; // dummy node - Node* temp = result; - +Node* MergeSortLinkedList::mergeSortedLists(Node* leftHead, Node* rightHead) { +#ifdef DEBUG + std::cout << "Merging the array: " << std::endl; +#endif + Node* result = new Node; // dummy node + Node* temp = result; + while (leftHead != nullptr && rightHead != nullptr) { if (leftHead->val > rightHead->val) { // move the right side list elememt to end of result @@ -210,6 +210,6 @@ Node* MergeSortLinkedList::mergeSortedLists(Node* leftHead, Node } template -MergeSortLinkedList::~MergeSortLinkedList() { +MergeSortLinkedList::~MergeSortLinkedList() { } diff --git a/Sorting/Selection Sort/C++/SelectionSort b/Sorting/Selection Sort/C++/SelectionSort old mode 100755 new mode 100644 diff --git a/Sorting/Selection Sort/python/selection_sort.py b/Sorting/Selection Sort/Python/selection_sort.py similarity index 100% rename from Sorting/Selection Sort/python/selection_sort.py rename to Sorting/Selection Sort/Python/selection_sort.py From deed3987391a22fd287db67929fb044590d7e40f Mon Sep 17 00:00:00 2001 From: OO7kartik Date: Thu, 17 Oct 2019 21:49:27 +0530 Subject: [PATCH 2/2] Added tree traversal using recursion --- Data Structures/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Data Structures/.gitkeep diff --git a/Data Structures/.gitkeep b/Data Structures/.gitkeep deleted file mode 100644 index e69de29b..00000000