From f8f9f98dffa675a23071887613d6fcd0fb16ee3e Mon Sep 17 00:00:00 2001 From: Vir Bhojani <141424554+Veer567@users.noreply.github.com> Date: Mon, 21 Oct 2024 07:36:30 +0530 Subject: [PATCH 1/2] Create Fibonacci_series_with_classes.py This Python program calculates the Fibonacci series using a class with memoization. The Fibonacci class has a calculate method that recursively computes Fibonacci numbers, storing previously calculated results in a dictionary to improve efficiency. The print_series method outputs the series up to a specified count. This approach is optimized for performance by avoiding redundant calculations through memoization. --- Python/Fibonacci_series_with_classes.py | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Python/Fibonacci_series_with_classes.py diff --git a/Python/Fibonacci_series_with_classes.py b/Python/Fibonacci_series_with_classes.py new file mode 100644 index 0000000..bc69246 --- /dev/null +++ b/Python/Fibonacci_series_with_classes.py @@ -0,0 +1,26 @@ +class Fibonacci: + def __init__(self): + self.memo = {} # Dictionary to store calculated Fibonacci numbers for optimization + + def calculate(self, n): + # Base cases + if n <= 0: + return 0 + elif n == 1: + return 1 + + # Check if Fibonacci number is already calculated + if n in self.memo: + return self.memo[n] + + # Calculate the Fibonacci number and store it in the dictionary (memoization) + self.memo[n] = self.calculate(n-1) + self.calculate(n-2) + return self.memo[n] + + def print_series(self, count): + for i in range(count): + print(self.calculate(i), end=" ") + +# Example usage +fib = Fibonacci() +fib.print_series(10) # Output: 0 1 1 2 3 5 8 13 21 34 From 3c5f411189735b7f645cf28cc3fa84e9b75a08cf Mon Sep 17 00:00:00 2001 From: Vir Bhojani <141424554+Veer567@users.noreply.github.com> Date: Mon, 21 Oct 2024 10:02:50 +0530 Subject: [PATCH 2/2] Create Binary_Tree.py A Binary Tree is a hierarchical data structure where each node has at most two children, referred to as the left and right child. In-Order Traversal is a tree traversal method where nodes are visited in the following order: Visit the left subtree. Visit the root node. Visit the right subtree. In in-order traversal, the elements of a binary tree are processed in a sorted sequence (for a binary search tree). This is useful for retrieving data in a specific order. --- Python/Binary_Tree.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Python/Binary_Tree.py diff --git a/Python/Binary_Tree.py b/Python/Binary_Tree.py new file mode 100644 index 0000000..34b23c3 --- /dev/null +++ b/Python/Binary_Tree.py @@ -0,0 +1,26 @@ +class TreeNode: + def __init__(self, data): + self.data = data + self.left = None + self.right = None + +class BinaryTree: + def __init__(self): + self.root = None + + def inorder_traversal(self, node): + if node: + self.inorder_traversal(node.left) + print(node.data, end=" ") + self.inorder_traversal(node.right) + +# Example usage +tree = BinaryTree() +tree.root = TreeNode(1) +tree.root.left = TreeNode(2) +tree.root.right = TreeNode(3) +tree.root.left.left = TreeNode(4) +tree.root.left.right = TreeNode(5) + +print("In-order Traversal:") +tree.inorder_traversal(tree.root) # Output: 4 2 5 1 3