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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added binary tree views
YashodeepDandegaonkar committed Sep 25, 2023
commit 9e17f9696465ad30524321b53c8f186110956747
19 changes: 19 additions & 0 deletions algorithms/tree/Bottomview.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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