mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-23 08:51:31 -04:00
last fixes: tree common ancestor
This commit is contained in:
parent
cc8f1099d6
commit
faa442ccb5
4 changed files with 109 additions and 53 deletions
|
@ -54,10 +54,22 @@ class Node(object):
|
|||
print self.item
|
||||
|
||||
if self.left:
|
||||
return self.left._printPreorder()
|
||||
self.left._printPreorder()
|
||||
|
||||
if self.right:
|
||||
return self.right._printPreorder()
|
||||
self.right._printPreorder()
|
||||
|
||||
# Another possibility: use an array (a little bit more expensive):
|
||||
def _preorder_array(self):
|
||||
nodes = []
|
||||
if self.item:
|
||||
nodes.append(self.item)
|
||||
if self.left:
|
||||
nodes.extend(self.left._preorder_array())
|
||||
if self.right:
|
||||
nodes.extend(self.right._preorder_array())
|
||||
return nodes
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -80,6 +92,11 @@ class BST(object):
|
|||
if self.root:
|
||||
return self.root._search(value)
|
||||
|
||||
def preorder_array(self):
|
||||
if self.root:
|
||||
return self.root._preorder_array()
|
||||
else:
|
||||
return 'Tree is empty.'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue