mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
clean up
This commit is contained in:
parent
1ef64d6e17
commit
e0eb554dfd
279 changed files with 24267 additions and 0 deletions
89
First_edition_2014/ebook_src/trees/transversal.py
Executable file
89
First_edition_2014/ebook_src/trees/transversal.py
Executable file
|
@ -0,0 +1,89 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
from collections import deque
|
||||
from binary_search_tree import BST, Node
|
||||
from binary_tree import BT, Node
|
||||
|
||||
|
||||
def BFT(tree):
|
||||
current = tree.root
|
||||
nodes = []
|
||||
queue = deque()
|
||||
queue.append(current)
|
||||
|
||||
while queue:
|
||||
current = queue.popleft()
|
||||
nodes.append(current.item)
|
||||
if current.left:
|
||||
queue.append(current.left)
|
||||
if current.right:
|
||||
queue.append(current.right)
|
||||
|
||||
return nodes
|
||||
|
||||
|
||||
def preorder(tree, nodes=None):
|
||||
nodes = nodes or []
|
||||
if tree:
|
||||
nodes.append(tree.item)
|
||||
if tree.left:
|
||||
preorder(tree.left, nodes)
|
||||
if tree.right:
|
||||
preorder(tree.right, nodes)
|
||||
return nodes
|
||||
|
||||
|
||||
def postorder(tree, nodes=None):
|
||||
nodes = nodes or []
|
||||
if tree:
|
||||
if tree.left:
|
||||
nodes = postorder(tree.left, nodes)
|
||||
if tree.right:
|
||||
nodes = postorder(tree.right, nodes)
|
||||
nodes.append(tree.item)
|
||||
|
||||
return nodes
|
||||
|
||||
|
||||
def inorder(tree, nodes=None):
|
||||
nodes = nodes or []
|
||||
if tree:
|
||||
if tree.left:
|
||||
nodes = inorder(tree.left, nodes)
|
||||
nodes.append(tree.item)
|
||||
if tree.right:
|
||||
nodes = inorder(tree.right, nodes)
|
||||
return nodes
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
|
||||
# bt
|
||||
bt = BT()
|
||||
l = [10, 5, 6, 3, 8, 2, 1, 11, 9, 4]
|
||||
for i in l:
|
||||
bt.add(i)
|
||||
print 'BT...'
|
||||
print 'Original: ', l
|
||||
print 'Preorder: ', preorder(bt.root)
|
||||
print 'Postorder: ', postorder(bt.root)
|
||||
print 'Inorder: ', inorder(bt.root)
|
||||
print 'Breath: ', BFT(bt)
|
||||
|
||||
# bst
|
||||
bst = BST()
|
||||
l = [10, 5, 6, 3, 8, 2, 1, 11, 9, 4]
|
||||
for i in l:
|
||||
bst.add(i)
|
||||
|
||||
print
|
||||
print 'BST ...'
|
||||
print 'Original: ', l
|
||||
print 'Preorder: ', preorder(bst.root)
|
||||
print 'Postorder: ', postorder(bst.root)
|
||||
print 'Inorder: ', inorder(bst.root)
|
||||
print 'Breath: ', BFT(bst)
|
Loading…
Add table
Add a link
Reference in a new issue