From 9e17f9696465ad30524321b53c8f186110956747 Mon Sep 17 00:00:00 2001 From: Yashodeep <2020.yashodeep.dandegaonkar@ves.ac.in> Date: Mon, 25 Sep 2023 13:15:31 +0530 Subject: [PATCH 1/2] added binary tree views --- algorithms/tree/Bottomview.py | 19 +++++++++ algorithms/tree/Topview.py | 16 ++++++++ algorithms/tree/leftview.py | 68 ++++++++++++++++++++++++++++++++ algorithms/tree/rightview.py | 74 +++++++++++++++++++++++++++++++++++ 4 files changed, 177 insertions(+) create mode 100644 algorithms/tree/Bottomview.py create mode 100644 algorithms/tree/Topview.py create mode 100644 algorithms/tree/leftview.py create mode 100644 algorithms/tree/rightview.py diff --git a/algorithms/tree/Bottomview.py b/algorithms/tree/Bottomview.py new file mode 100644 index 000000000..6cde999b2 --- /dev/null +++ b/algorithms/tree/Bottomview.py @@ -0,0 +1,19 @@ +def print_bottom_of_binary_tree(root): + d = dict() + + printBottomViewUtil(root, d, 0, 0) + for i in sorted(d.keys()): + print(d[i][0], end = " ") + +def bottomview(root, d, hd, level): + if root is None: + return + +if hd in d: + if level >= d[hd][1]: + d[hd] = [root.data, level] +else: + d[hd] = [root.data, level] + +bottomview(root.left, d, hd - 1,level + 1) +bottomview(root.right, d, hd + 1,level + 1) \ No newline at end of file diff --git a/algorithms/tree/Topview.py b/algorithms/tree/Topview.py new file mode 100644 index 000000000..94e1c7761 --- /dev/null +++ b/algorithms/tree/Topview.py @@ -0,0 +1,16 @@ +def topView(head, dis, level, dict): + + if head is None: + return + + if dis not in dict or level < dict[dis][1]: + dict[dis] = (head.key, level) + topView(head.left, dis - 1, level + 1, dict) + topView(head.right, dis + 1, level + 1, dict) + +def printTopView(head): + dict = {} + + topView(head, 0, 0, dict) + for key in sorted(dict.keys()): + print(dict.get(key)[0], end=' ') \ No newline at end of file diff --git a/algorithms/tree/leftview.py b/algorithms/tree/leftview.py new file mode 100644 index 000000000..1a970d0f3 --- /dev/null +++ b/algorithms/tree/leftview.py @@ -0,0 +1,68 @@ +# Python3 program to print left view of +# Binary Tree + +# Binary Tree Node +""" utility that allocates a newNode +with the given key """ + + +class newNode: + + # Construct to create a newNode + def __init__(self, key): + self.data = key + self.left = None + self.right = None + self.hd = 0 + +# function to print left view of +# binary tree + + +def printLeftView(root): + + if (not root): + return + + q = [] + q.append(root) + + while (len(q)): + + # number of nodes at current level + n = len(q) + + # Traverse all nodes of current level + for i in range(1, n + 1): + temp = q[0] + q.pop(0) + + # Print the left most element + # at the level + if (i == 1): + print(temp.data, end=" ") + + # Add left node to queue + if (temp.left != None): + q.append(temp.left) + + # Add right node to queue + if (temp.right != None): + q.append(temp.right) + + +# Driver Code +if __name__ == '__main__': + + root = newNode(10) + root.left = newNode(2) + root.right = newNode(3) + root.left.left = newNode(7) + root.left.right = newNode(8) + root.right.right = newNode(15) + root.right.left = newNode(12) + root.right.right.left = newNode(14) + printLeftView(root) + +# This code is contributed by +# Manne SreeCharan diff --git a/algorithms/tree/rightview.py b/algorithms/tree/rightview.py new file mode 100644 index 000000000..23e318c90 --- /dev/null +++ b/algorithms/tree/rightview.py @@ -0,0 +1,74 @@ + +# Python3 program to print right +# view of Binary Tree +from collections import deque + +# A binary tree node + + +class Node: + + # A constructor to create a new + # Binary tree Node + def __init__(self, val): + self.data = val + self.left = None + self.right = None + +# Function to print Right view of +# binary tree + + +def rightView(root): + + if root is None: + return + + q = deque() + q.append(root) + + while q: + + # Get number of nodes for each level + n = len(q) + + # Traverse all the nodes of the + # current level + + while n > 0: + n -= 1 + + # Get the front node in the queue + node = q.popleft() + + # Print the last node of each level + if n == 0: + print(node.data, end=" ") + + # If left child is not null push it + # into the queue + if node.left: + q.append(node.left) + + # If right child is not null push + # it into the queue + if node.right: + q.append(node.right) + +# Driver code + + +# Let's construct the tree as +# shown in example +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.left = Node(4) +root.left.right = Node(5) +root.right.left = Node(6) +root.right.right = Node(7) +root.right.left.right = Node(8) + +rightView(root) + +# This code is contributed by Pulkit Pansari From 60427bf7e8e7798d60fd8f9f9f0ecee99d58493a Mon Sep 17 00:00:00 2001 From: Yashodeep <2020.yashodeep.dandegaonkar@ves.ac.in> Date: Mon, 25 Sep 2023 13:50:28 +0530 Subject: [PATCH 2/2] added string matching z algorithm --- algorithms/strings/Zalgorithm.py | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 algorithms/strings/Zalgorithm.py diff --git a/algorithms/strings/Zalgorithm.py b/algorithms/strings/Zalgorithm.py new file mode 100644 index 000000000..47e52bad8 --- /dev/null +++ b/algorithms/strings/Zalgorithm.py @@ -0,0 +1,68 @@ +# Fills Z array for given string str[] +def create_Zarr(string, z): + n = len(string) + + # [L,R] make a window which matches + # with prefix of s + left, right, k = 0, 0, 0 + for i in range(1, n): + + # if i>R nothing matches so we will calculate. + # Z[i] using naive way. + if i > right: + left, right = i, i + + # R-L = 0 in starting, so it will start + # checking from 0'th index. + while right < n and string[right - left] == string[right]: + right += 1 + z[i] = right - left + right -= 1 + else: + + # k = i-L so k corresponds to number which + # matches in [L,R] interval. + k = i - left + + # if Z[k] is less than remaining interval + # then Z[i] will be equal to Z[k]. + + if z[k] < right - i + 1: + z[i] = z[k] + + + else: + + # else start from R and check manually + left = i + while right < n and string[right - left] == string[right]: + right += 1 + z[i] = right - left + right -= 1 + +# prints all occurrences of pattern +# in text using Z algo +def find(text, pattern): + + # Create concatenated string "P$T" + concat = pattern + "$" + text + left = len(concat) + + # Construct Z array + z = [0] * left + create_Zarr(concat, z) + + # now looping through Z array for matching condition + for i in range(left): + + # if Z[i] (matched region) is equal to pattern + # length we got the pattern + if z[i] == len(pattern): + print("Pattern found at index", + i - len(pattern) - 1) + +# Driver Code +if __name__ == "__main__": + text = "faabbcdeffghiaaabbcdfgaabf" + pattern = "aabb" + find(text, pattern)