MCPcopy
hub / github.com/Jack-Lee-Hiter/AlgorithmsByPython / BinaryTree

Class BinaryTree

BinaryTree.py:1–42  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class BinaryTree:
2 def __init__(self, rootObj):
3 self.key = rootObj
4 self.leftChild = None
5 self.rightChild = None
6
7 def insertLeft(self, newNode):
8 if self.leftChild == None:
9 self.leftChild = BinaryTree(newNode)
10 else:
11 t = BinaryTree(newNode)
12 t.leftChild = self.leftChild
13 self.leftChild = t
14
15 def insertRight(self, newNode):
16 if self.rightChild == None:
17 self.rightChild = BinaryTree(newNode)
18 else:
19 t = BinaryTree(newNode)
20 t.rightChild = self.rightChild
21 self.rightChild = t
22
23 def getRightChild(self):
24 return self.rightChild
25
26 def getLeftChild(self):
27 return self.leftChild
28
29 def setRootVal(self, obj):
30 self.key = obj
31
32 def getRootVal(self):
33 return self.key
34
35 # 树的前序遍历
36 # 树的后序遍历以及中序遍历见ParseTree.py
37 def preorder(self):
38 print(self.key)
39 if self.leftChild:
40 self.leftChild.preorder()
41 if self.rightChild:
42 self.rightChild.preorder()
43
44'''
45以下为测试数据, 去掉 # 即可

Callers 3

buildParseTreeFunction · 0.90
insertLeftMethod · 0.85
insertRightMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected