mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
last fixes: tree common ancestor
This commit is contained in:
parent
cc8f1099d6
commit
faa442ccb5
@ -54,10 +54,22 @@ class Node(object):
|
|||||||
print self.item
|
print self.item
|
||||||
|
|
||||||
if self.left:
|
if self.left:
|
||||||
return self.left._printPreorder()
|
self.left._printPreorder()
|
||||||
|
|
||||||
if self.right:
|
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:
|
if self.root:
|
||||||
return self.root._search(value)
|
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__':
|
if __name__ == '__main__':
|
||||||
|
@ -47,6 +47,14 @@ class Node(object):
|
|||||||
return not self.right and not self.left
|
return not self.right and not self.left
|
||||||
|
|
||||||
|
|
||||||
|
def _preorder(self):
|
||||||
|
print self.item
|
||||||
|
if self.left:
|
||||||
|
self.left._preorder()
|
||||||
|
if self.right:
|
||||||
|
self.right._preorder()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class BT(object):
|
class BT(object):
|
||||||
|
|
||||||
@ -61,40 +69,14 @@ class BT(object):
|
|||||||
self.root._add(value)
|
self.root._add(value)
|
||||||
|
|
||||||
|
|
||||||
def printPreorder(self):
|
|
||||||
current = self.root
|
|
||||||
nodes, stack = [], []
|
|
||||||
while stack or current:
|
|
||||||
if current:
|
|
||||||
nodes.append(current.item) # this is what change
|
|
||||||
stack.append(current)
|
|
||||||
current = current.left
|
|
||||||
else:
|
|
||||||
current = stack.pop()
|
|
||||||
current = current.right
|
|
||||||
print nodes
|
|
||||||
|
|
||||||
|
|
||||||
def printInorder(self):
|
|
||||||
current = self.root
|
|
||||||
nodes, stack = [], []
|
|
||||||
while stack or current:
|
|
||||||
|
|
||||||
if current:
|
|
||||||
stack.append(current)
|
|
||||||
current = current.left
|
|
||||||
else:
|
|
||||||
current = stack.pop()
|
|
||||||
nodes.append(current.item) # this is what change
|
|
||||||
current = current.right
|
|
||||||
print nodes
|
|
||||||
|
|
||||||
|
|
||||||
def search(self, value):
|
def search(self, value):
|
||||||
if self.root:
|
if self.root:
|
||||||
return self.root._search(value)
|
return self.root._search(value)
|
||||||
|
|
||||||
|
|
||||||
|
def preorder(self):
|
||||||
|
if self.root:
|
||||||
|
return self.root._preorder()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
@ -111,7 +93,4 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
print
|
print
|
||||||
print "Printing preorder..."
|
print "Printing preorder..."
|
||||||
bt.printPreorder()
|
bt.preorder()
|
||||||
|
|
||||||
print "Printing Inorder..."
|
|
||||||
bt.printInorder()
|
|
||||||
|
@ -19,9 +19,17 @@ class Node(object):
|
|||||||
self.right = new_node
|
self.right = new_node
|
||||||
else:
|
else:
|
||||||
self.left = self.left.add(value)
|
self.left = self.left.add(value)
|
||||||
|
|
||||||
return self # without this, it doesn't add!
|
return self # without this, it doesn't add!
|
||||||
|
|
||||||
|
def search(self, item):
|
||||||
|
if self.value == item:
|
||||||
|
return True
|
||||||
|
found = False
|
||||||
|
if (self.left and self.left.search(item)) or \
|
||||||
|
(self.right and self.right.search(item)):
|
||||||
|
found = True
|
||||||
|
return found
|
||||||
|
|
||||||
def preorder(self):
|
def preorder(self):
|
||||||
yield self.value
|
yield self.value
|
||||||
if self.left:
|
if self.left:
|
||||||
@ -40,7 +48,6 @@ class Node(object):
|
|||||||
for node in self.right.postorder():
|
for node in self.right.postorder():
|
||||||
yield node
|
yield node
|
||||||
|
|
||||||
|
|
||||||
def inorder(self):
|
def inorder(self):
|
||||||
yield self.value
|
yield self.value
|
||||||
if self.left:
|
if self.left:
|
||||||
@ -50,6 +57,14 @@ class Node(object):
|
|||||||
for node in self.right.inorder():
|
for node in self.right.inorder():
|
||||||
yield node
|
yield node
|
||||||
|
|
||||||
|
# this is the most basic way to write this function
|
||||||
|
def preorder_simple(self):
|
||||||
|
print self.value
|
||||||
|
if self.left:
|
||||||
|
self.left.preorder_simple()
|
||||||
|
if self.right:
|
||||||
|
self.right.preorder_simple()
|
||||||
|
|
||||||
|
|
||||||
# Another possibility: use an array (a little bit more expensive):
|
# Another possibility: use an array (a little bit more expensive):
|
||||||
def preorder_array(self):
|
def preorder_array(self):
|
||||||
@ -74,6 +89,12 @@ class BT(object):
|
|||||||
else:
|
else:
|
||||||
self.root.add(value)
|
self.root.add(value)
|
||||||
|
|
||||||
|
def search(self, item):
|
||||||
|
if self.root:
|
||||||
|
return self.root.search(item)
|
||||||
|
else:
|
||||||
|
return 'Tree is empty.'
|
||||||
|
|
||||||
def preorder(self):
|
def preorder(self):
|
||||||
if self.root:
|
if self.root:
|
||||||
return self.root.preorder()
|
return self.root.preorder()
|
||||||
@ -98,6 +119,11 @@ class BT(object):
|
|||||||
else:
|
else:
|
||||||
return 'Tree is empty.'
|
return 'Tree is empty.'
|
||||||
|
|
||||||
|
def preorder_simple(self):
|
||||||
|
if self.root:
|
||||||
|
return self.root.preorder_simple()
|
||||||
|
else:
|
||||||
|
return 'Tree is empty.'
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
@ -106,21 +132,38 @@ if __name__ == '__main__':
|
|||||||
for i in range(1, 11):
|
for i in range(1, 11):
|
||||||
tree.add(i)
|
tree.add(i)
|
||||||
|
|
||||||
|
print 'Searching for node 4'
|
||||||
|
print tree.search(4)
|
||||||
|
|
||||||
|
print 'Searching for node 1'
|
||||||
|
print tree.search(1)
|
||||||
|
|
||||||
|
print 'Searching for node 12'
|
||||||
|
print tree.search(12)
|
||||||
|
|
||||||
|
print 'Pre-order generator...'
|
||||||
getree = tree.preorder()
|
getree = tree.preorder()
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
print next(getree)
|
print next(getree)
|
||||||
|
|
||||||
print
|
print
|
||||||
|
print 'Pre-order array...'
|
||||||
|
|
||||||
print tree.preorder_array()
|
print tree.preorder_array()
|
||||||
|
|
||||||
print
|
print
|
||||||
|
print 'Pre-order simple...'
|
||||||
|
|
||||||
|
tree.preorder_simple()
|
||||||
|
|
||||||
|
print
|
||||||
|
print 'Inorder...'
|
||||||
|
|
||||||
getree = tree.inorder()
|
getree = tree.inorder()
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
print next(getree)
|
print next(getree)
|
||||||
|
|
||||||
print
|
print
|
||||||
|
print 'Postorder...'
|
||||||
|
|
||||||
getree = tree.postorder()
|
getree = tree.postorder()
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
|
@ -4,37 +4,54 @@ __author__ = "bt3"
|
|||||||
|
|
||||||
|
|
||||||
from binary_search_tree import BST, Node
|
from binary_search_tree import BST, Node
|
||||||
from transversal import inorder
|
|
||||||
|
|
||||||
def find_ancestor(path, low_value, high_value):
|
|
||||||
|
|
||||||
|
def find_ancestor(path, low_item, high_item):
|
||||||
while path:
|
while path:
|
||||||
current_value = path[0]
|
current_item = path[0]
|
||||||
|
|
||||||
if current_value < low_value:
|
if current_item < low_item:
|
||||||
try:
|
try:
|
||||||
path = path[2:]
|
path = path[2:]
|
||||||
except:
|
except:
|
||||||
return current_value
|
return current_item
|
||||||
|
|
||||||
elif current_value > high_value:
|
elif current_item > high_item:
|
||||||
try:
|
try:
|
||||||
path = path[1:]
|
path = path[1:]
|
||||||
except:
|
except:
|
||||||
return current_value
|
return current_item
|
||||||
|
|
||||||
|
elif low_item <= current_item <= high_item:
|
||||||
|
return current_item
|
||||||
|
|
||||||
|
|
||||||
|
def find_ancestor2(tree, n1, n2):
|
||||||
|
if not tree:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if n1 <= tree.item and n2 >= tree.item or (not tree.left and not tree.right) :
|
||||||
|
return tree.item
|
||||||
|
|
||||||
|
if tree.left and (n1 < tree.item and n2 < tree.item):
|
||||||
|
return find_ancestor(tree.left, n1, n2) or tree.item
|
||||||
|
|
||||||
|
if tree.right and (n1 > tree.item and n2 > tree.item):
|
||||||
|
return find_ancestor(tree.right, n1, n2) or tree.item
|
||||||
|
|
||||||
elif low_value <= current_value <= high_value:
|
|
||||||
return current_value
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
|
||||||
bst = BST()
|
bst = BST()
|
||||||
l = [10, 5, 6, 3, 8, 2, 1, 11, 9, 4]
|
l = [10, 5, 6, 3, 8, 2, 1, 11, 9, 4]
|
||||||
for i in l:
|
for i in l:
|
||||||
bst.add(i)
|
bst.add(i)
|
||||||
nodes = inorder(bst.root)
|
nodes = bst.preorder_array()
|
||||||
|
|
||||||
print 'Original: ', l
|
print 'Original: ', l
|
||||||
print 'Inorder: ', nodes
|
print 'Preorder: ', nodes
|
||||||
|
|
||||||
|
print 'Method 1: '
|
||||||
print 'Ancestor for 3, 11:', find_ancestor(nodes, 3, 11)
|
print 'Ancestor for 3, 11:', find_ancestor(nodes, 3, 11)
|
||||||
|
|
||||||
|
print 'Method 2: '
|
||||||
|
print 'Ancestor for 3, 11: ', find_ancestor2(bst.root, 3, 11)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user