-
Notifications
You must be signed in to change notification settings - Fork 586
/
P62_BinaryTree.py
82 lines (58 loc) · 1.88 KB
/
P62_BinaryTree.py
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
# Author: OMKAR PATHAK
# A data structure in which a record is linked to two successor records, usually referred to as
# the left branch when greater and the right when less than the previous record.
class BinaryTree(object):
def __init__(self,nodeData):
self.left = None
self.right = None
self.nodeData = nodeData
def getLeftChild(self):
return self.left
def getRightChild(self):
return self.right
def setnodeDataValue(self,value):
self.nodeData = value
def getnodeDataValue(self):
return self.nodeData
def insertRight(self,newnodeData):
if self.right == None:
self.right = BinaryTree(newnodeData)
else:
tree = BinaryTree(newnodeData)
tree.right = self.right
self.right = tree
def insertLeft(self,newnodeData):
if self.left == None:
self.left = BinaryTree(newnodeData)
else:
tree = BinaryTree(newnodeData)
self.left = tree
tree.left = self.left
def printTree(tree):
if tree != None:
printTree(tree.getLeftChild())
print(tree.getnodeDataValue())
printTree(tree.getRightChild())
def pprint(head_node, _pre="", _last=True, term=False):
data = "*" if head_node is None else head_node.nodeData
print(_pre, "`- " if _last else "|- ", data, sep="")
_pre += " " if _last else "| "
if term: return
left = head_node.getLeftChild()
right = head_node.getRightChild()
for i, child in enumerate([left, right]):
pprint(child, _pre, bool(i) ,term=not(bool(child)))
def testTree():
myTree = BinaryTree("1")
myTree.insertLeft("2")
myTree.insertRight("3")
myTree.insertRight("4")
printTree(myTree)
pprint(myTree)
if __name__ == '__main__':
testTree()
# OUTPUT
# 2
# 1
# 4
# 3