-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path006binaryTree.py
More file actions
102 lines (90 loc) · 3.33 KB
/
006binaryTree.py
File metadata and controls
102 lines (90 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
class BinaryTree():
def __init__(self):
self.prettyPrintString = []
self.binaryTree = [0,[0],[0]]
self.printString = ''
self.depth = 0
def addToBinaryTree(self,binaryString):
tempBinaryTreePosition = self.binaryTree
for binary in binaryString:
if binary != '\n':
tempBinaryTreePosition = tempBinaryTreePosition[int(binary)+1]
tempBinaryTreePosition[0] += 1
if len(tempBinaryTreePosition) == 1:
tempBinaryTreePosition.append([0])
tempBinaryTreePosition.append([0])
def readFileInput(self,file):
with open(file,'r') as infile:
for line in infile:
self.addToBinaryTree(line)
def depthBinaryTreeHelper(self,localBinaryTree,depth):
if len(localBinaryTree) == 1:
return
else:
depth +=1
self.updateDepth(depth)
self.depthBinaryTreeHelper(localBinaryTree[1],depth)
self.depthBinaryTreeHelper(localBinaryTree[2],depth)
def depthBinaryTree(self):
self.depthBinaryTreeHelper(self.binaryTree,0)
def updateDepth(self,newDepth):
self.depth = newDepth if newDepth>self.depth else self.depth
def stringifyBinaryTree(self):
currentDepth = 1
while currentDepth < self.depth:
self.recursiveStringifyLevel(self.binaryTree,currentDepth,0)
self.prettyPrintString.append(self.printString)
self.printString = ''
currentDepth += 1
def recursiveStringifyLevel(self,binaryTreePrint,printDepth,currentDepth):
if len(binaryTreePrint) == 1 or currentDepth > printDepth:
return
elif currentDepth == printDepth:
self.printString += f'{binaryTreePrint[0]} '
else:
self.recursiveStringifyLevel(binaryTreePrint[1],printDepth,currentDepth+1)
self.recursiveStringifyLevel(binaryTreePrint[2],printDepth,currentDepth+1)
def rightSidePrintBinaryTree(self):
prettyPrintString = self.prettyPrintString
maxLength = len(prettyPrintString[-1].strip().split(' '))
for line in prettyPrintString:
nrInLine = line.strip().split(' ')
currentLength = len(nrInLine)
emptyness = int(maxLength/currentLength)-1
for nr in nrInLine:
for i in range(emptyness):
print(' '*4,end='')
print(nr+(' '*(4-len(nr))),end='')
print()
def leftSidePrintBinaryTree(self):
prettyPrintString = self.prettyPrintString
maxLength = len(prettyPrintString[-1].strip().split(' '))
for line in prettyPrintString:
nrInLine = line.strip().split(' ')
currentLength = len(nrInLine)
emptyness = int(maxLength/currentLength)-1
for nr in nrInLine:
print(nr+(' '*(4-len(nr))),end='')
for i in range(emptyness):
print(' '*4,end='')
print()
def centralPrintBinaryTree(self):
prettyPrintString = self.prettyPrintString
maxLength = len(prettyPrintString[-1].strip().split(' '))*4
for line in prettyPrintString:
nrInLine = line.strip().split(' ')
partLength = int(maxLength/(len(nrInLine)*2))
print(' '*partLength,end='')
for i in nrInLine:
print(i,end='')
print(' '*((partLength*2)-len(i)),end='')
print(' '*partLength,end='')
print()
if '__main__' == __name__:
bt = BinaryTree()
bt.readFileInput('006outfile.txt')
bt.depthBinaryTree()
bt.stringifyBinaryTree()
bt.rightSidePrintBinaryTree()
bt.leftSidePrintBinaryTree()
bt.centralPrintBinaryTree()