Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BinaryTreeNode:
- def __init__(self, value, parentNode='self', leftChild='none', rightChild='none'):
- self.Value = value
- self.leftChild = leftChild
- self.rightChild = rightChild
- if parentNode == 'self':
- self.parent = self
- else:
- self.parent = parentNode
- def addChild(self, child):
- #add a child node. working
- if child.Value < self.Value:
- if self.leftChild == 'none':
- self.leftChild = child
- child.parent = self
- else:
- self.leftChild.addChild(child)
- elif child.Value > self.Value:
- if self.rightChild == 'none':
- self.rightChild = child
- child.parent = self
- else:
- self.rightChild.addChild(child)
- def findValue(self, value):
- #Performs a binary search for a specified value
- if value == self.Value:
- print("found!")
- return self
- elif value < self.Value:
- if self.leftChild == 'none':
- print('value not found')
- else:
- self.leftChild.findValue(value)
- elif value > self.Value:
- if self.rightChild == 'none':
- print('value not found')
- else:
- self.rightChild.findValue(value)
- def genList(self):
- #recursively generates a sorted list of child node values
- numList = []
- if self.leftChild != 'none':
- numList.extend(self.leftChild.genList()) #error
- numList.extend(list((self.Value,)))
- if self.rightChild != 'none':
- numList.extend(self.rightChild.genList()) #error
- return numList
- def getAncestor(self):
- #find the top node of the tree. working
- if self.parent == self:
- return self
- else:
- return self.parent.getAncestor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement