Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53f9506

Browse files
hanif-aliOmkarPathak
authored andcommitted
Added Pretty Printing feature to Binary Tree Programs (#17)
* Added some more functions to BinarySearchTree. -> Defining intial elements of newly created Trees -> Prettyprinting trees with left-right order and * representing null nodes. * Added pretty print to BinaryTree.py * Fixed flake8 Linting
1 parent 1cf984a commit 53f9506

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

‎Programs/P43_BinarySearchTree.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ def postorder(self):
7272
print(str(self.data), end = ' ')
7373

7474
class Tree(object):
75-
def __init__(self):
75+
def __init__(self, initial_data= []):
7676
self.root = None
7777

78+
# If provided, add initial data
79+
for data in initial_data:
80+
self.insert(data)
81+
7882
def insert(self, data):
7983
if self.root:
8084
return self.root.insert(data)
@@ -106,6 +110,22 @@ def postorder(self):
106110
print('Postorder: ')
107111
self.root.postorder()
108112

113+
114+
def pprint(self, head_node=0, _pre="", _last=True, term=False):
115+
116+
head_node = self.root if head_node == 0 else head_node
117+
118+
data = "*" if head_node is None else head_node.data
119+
120+
print(_pre, "`- " if _last else "|- ", data, sep="")
121+
_pre += " " if _last else "| "
122+
123+
if term: return
124+
125+
for i, child in enumerate([head_node.leftChild, head_node.rightChild]):
126+
self.pprint(child, _pre, bool(i) ,term=not(bool(child)))
127+
128+
109129
if __name__ == '__main__':
110130
tree = Tree()
111131
tree.insert(10)
@@ -117,6 +137,7 @@ def postorder(self):
117137
tree.insert(7)
118138
tree.insert(15)
119139
tree.insert(13)
140+
tree.pprint()
120141
print(tree.find(1))
121142
print(tree.find(12))
122143
tree.preorder()

‎Programs/P62_BinaryTree.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,39 @@ def insertLeft(self,newnodeData):
3838
tree.left = self.left
3939

4040

41+
42+
4143
def printTree(tree):
4244
if tree != None:
4345
printTree(tree.getLeftChild())
4446
print(tree.getnodeDataValue())
4547
printTree(tree.getRightChild())
4648

49+
50+
def pprint(head_node, _pre="", _last=True, term=False):
51+
data = "*" if head_node is None else head_node.nodeData
52+
53+
print(_pre, "`- " if _last else "|- ", data, sep="")
54+
_pre += " " if _last else "| "
55+
56+
if term: return
57+
58+
left = head_node.getLeftChild()
59+
right = head_node.getRightChild()
60+
61+
for i, child in enumerate([left, right]):
62+
pprint(child, _pre, bool(i) ,term=not(bool(child)))
63+
64+
65+
66+
4767
def testTree():
4868
myTree = BinaryTree("1")
4969
myTree.insertLeft("2")
5070
myTree.insertRight("3")
5171
myTree.insertRight("4")
5272
printTree(myTree)
73+
pprint(myTree)
5374

5475
if __name__ == '__main__':
5576
testTree()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /