Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added binary tree views and string matching z algorithm #906

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 68 additions & 0 deletions 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)
19 changes: 19 additions & 0 deletions 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)
16 changes: 16 additions & 0 deletions 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=' ')
68 changes: 68 additions & 0 deletions 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
74 changes: 74 additions & 0 deletions 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