Skip to content

Commit 5e1e3f6

Browse files
authored
Merge pull request #26 from AbhiSaphire/pc-upload
Tree Algo
2 parents 1678be3 + 80652f5 commit 5e1e3f6

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

HackerEarth/Tree/BottomViewTree.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Python3 Bottom View Binary Tree
2+
3+
class Node:
4+
def __init__(self, hd, data):
5+
self.left = None
6+
self.right = None
7+
self.hd = hd
8+
self.data = data
9+
10+
def BottomViewTree(root):
11+
if not root:
12+
return
13+
q = []
14+
m = {}
15+
hd = 0
16+
root.hd = hd
17+
q.append(root)
18+
while q:
19+
hd = root.hd
20+
m[hd] = root.data
21+
if root.left:
22+
root.left = hd - 1
23+
q.append(root.left)
24+
if root.right:
25+
root.right = hd + 1
26+
q.append(root.right)
27+
q.pop(0)
28+
root = q[0]
29+
30+
print(*m.values())

HackerEarth/Tree/TopViewTree.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Python3 Top View of Binary Tree
2+
3+
class Node:
4+
def __init__(self, data, hd):
5+
self.left = None
6+
self.right = None
7+
self.hd = hd
8+
self.data = data
9+
10+
def TopViewTree(root):
11+
if root == None:
12+
return
13+
q = []
14+
m = {}
15+
hd = 0
16+
root.hd = hd
17+
q.append(root)
18+
while q:
19+
hd = root.hd
20+
if hd not in m:
21+
m[hd] = root.data
22+
if root.left:
23+
root.left = hd - 1
24+
q.append(root.left)
25+
if root.right:
26+
root,right = hd + 1
27+
q.append(root.right)
28+
q.pop(0)
29+
root = q[0]
30+
print(*m.values())

0 commit comments

Comments
 (0)