Skip to content

Commit 4a44a37

Browse files
committed
Simple Binary Tree implementation
1 parent e3470a7 commit 4a44a37

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Programs/P62_BinaryTree.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Author: OMKAR PATHAK
2+
# A data structure in which a record is linked to two successor records, usually referred to as
3+
# the left branch when greater and the right when less than the previous record.
4+
5+
class BinaryTree(object):
6+
7+
def __init__(self,nodeData):
8+
self.left = None
9+
self.right = None
10+
self.nodeData = nodeData
11+
12+
def getLeftChild(self):
13+
return self.left
14+
15+
def getRightChild(self):
16+
return self.right
17+
18+
def setnodeDataValue(self,value):
19+
self.nodeData = value
20+
21+
def getnodeDataValue(self):
22+
return self.nodeData
23+
24+
def insertRight(self,newnodeData):
25+
if self.right == None:
26+
self.right = BinaryTree(newnodeData)
27+
else:
28+
tree = BinaryTree(newnodeData)
29+
tree.right = self.right
30+
self.right = tree
31+
32+
def insertLeft(self,newnodeData):
33+
if self.left == None:
34+
self.left = BinaryTree(newnodeData)
35+
else:
36+
tree = BinaryTree(newnodeData)
37+
self.left = tree
38+
tree.left = self.left
39+
40+
41+
def printTree(tree):
42+
if tree != None:
43+
printTree(tree.getLeftChild())
44+
print(tree.getnodeDataValue())
45+
printTree(tree.getRightChild())
46+
47+
def testTree():
48+
myTree = BinaryTree("1")
49+
myTree.insertLeft("2")
50+
myTree.insertRight("3")
51+
myTree.insertRight("4")
52+
printTree(myTree)
53+
54+
if __name__ == '__main__':
55+
testTree()

0 commit comments

Comments
 (0)