Skip to content

Commit 7f1e7b1

Browse files
authored
Add files via upload
1 parent e1016cc commit 7f1e7b1

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

printree/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from printree.binarytree_code import binarytree

printree/binarytree_code.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#B PRAJEETH
2+
#original final copy 20/5/22
3+
#THIS CODE HAS EVERYTHING IN TERMS OF FUNCTION, YOU CAN USE THE printree FUNCION OF THIS CODE AND PRINT THE BINARY TREE SUCCESSFULLY
4+
5+
6+
def Preorder(root,right,left,val,n,ar,y,x,max_width):
7+
#print("inside")
8+
#for i in range(3):
9+
# print(ar[i])
10+
if root:
11+
ar[2*y][x-1]=getattr(root,val)
12+
if(len(str(getattr(root,val))) > max_width[0]):
13+
max_width[0] = len(str(getattr(root,val)))
14+
15+
if(n > y+1):
16+
for i in range(1, (2**(n-y-2)) +1):
17+
ar[2*y][x-1+i]="_"
18+
ar[2*y][x-1-i]="_"
19+
ar[2*y +1][x-1 - (2**(n-y-2))]="|"
20+
ar[2*y +1][x-1 + (2**(n-y-2))]="|"
21+
y=y+1
22+
if(y <= n-1):
23+
ar[2*y][x-1 - (2**(n-y-1)) ]="?"
24+
ar[2*y][x-1 + (2**(n-y-1)) ]="?"
25+
26+
Preorder(getattr(root,left),right,left,val,n,ar,y,x-(2**(n-y-1)),max_width)
27+
Preorder(getattr(root,right),right,left,val,n,ar,y,x+(2**(n-y-1)),max_width)
28+
y=y-1
29+
30+
def maxDepth(node,left,right): #function to calculte the maximum depth of a tree
31+
if node is None:
32+
return -1 ;
33+
34+
else :
35+
36+
lDepth = maxDepth(getattr(node,left),left,right)
37+
rDepth = maxDepth(getattr(node,right),left,right)
38+
39+
if (lDepth > rDepth):
40+
return lDepth+1
41+
else:
42+
return rDepth+1
43+
44+
def binarytree(r,right,left,val):
45+
ar=[]
46+
n=maxDepth(r,left,right) +1 # height of tree
47+
for i in range(2*n):
48+
ls=[]
49+
for j in range((2**n)-1):
50+
ls.append("")
51+
ar.append(ls)
52+
53+
y=0
54+
x=2**(n-y-1)
55+
#print("printing tree ... \n")
56+
max_width=[0]
57+
Preorder(r,right,left,val,n,ar,y,x,max_width)
58+
59+
for i in range(2*n):
60+
ct=0
61+
ct1=0
62+
for j in range((2**n)-1):
63+
if(ar[i][j]==""):
64+
print(" "*max_width[0],end="")
65+
else:
66+
width=max_width[0] - len(str(ar[i][j]))
67+
if(ar[i][j] != "_" and ar[i][j]!="?" and ar[i][j]!="|" and width>0):
68+
ct1 = ct1 +1
69+
if(i != 2*n - 2):
70+
if(ct1%2 == 0):
71+
print("_"*(width) + str(ar[i][j]),end="")
72+
else:
73+
print(str(ar[i][j]) + "_"*(width) ,end="")
74+
75+
else:
76+
if(ct1%2 == 0):
77+
print(" "*(width) + str(ar[i][j]),end="")
78+
else:
79+
print(str(ar[i][j])+" "*(width) ,end="")
80+
elif(ar[i][j]=="?"):
81+
ct1=ct1+1
82+
if(ct1 %2 == 0):
83+
print(" "*width + str(ar[i][j]),end="")
84+
else:
85+
print(str(ar[i][j]) + " "*width ,end="")
86+
elif(ar[i][j] == "_"):
87+
print(ar[i][j]*max_width[0],end="")
88+
else:
89+
ct=ct+1
90+
if(ct%2 == 0):
91+
print(" "*(width) + str(ar[i][j]),end="")
92+
else:
93+
print(str(ar[i][j]) + " "*(width),end="")
94+
95+
print()
96+
97+
98+

0 commit comments

Comments
 (0)