-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathbinary_tree_traversal.py
49 lines (37 loc) · 1.15 KB
/
binary_tree_traversal.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
class BinaryTree:
def __init__(self, obj):
self.key = obj
self.leftChild = None
self.rightChild = None
def insertLeft(self, newNode):
if self.leftChild == None:
self.leftChild = BinaryTree(newNode)
else:
aux = BinaryTree(newNode)
aux.leftChild = self.leftChild
self.leftChild = aux
def insertRight(self, newNode):
if self.rightChild == None:
self.rightChild = BinaryTree(newNode)
else:
aux = BinaryTree(newNode)
aux.rightChild = self.rightChild
self.rightChild = aux
def getRightChild(self):
return self.rightChild
def getLeftChild(self):
return self.leftChild
def setRootVal(self,obj):
self.key = obj
def getRootVal(self):
return self.key
def inorder(tree):
if tree != None:
inorder(tree.getLeftChild())
print(tree.getRootVal())
inorder(tree.getRightChild())
if __name__ == "__main__":
r = BinaryTree('a')
r.insertLeft('b')
r.insertRight('c')
inorder(r)