当前位置: 首页>>代码示例>>Python>>正文


Python Tree.inorder方法代码示例

本文整理汇总了Python中tree.Tree.inorder方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.inorder方法的具体用法?Python Tree.inorder怎么用?Python Tree.inorder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tree.Tree的用法示例。


在下文中一共展示了Tree.inorder方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TreeOrderTestCase

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import inorder [as 别名]
class TreeOrderTestCase(unittest.TestCase):

    def setUp(self):
        self.tree = Tree(1)
        self.tree.left = Tree(2)
        self.tree.right = Tree(3)
        self.tree.right.left = Tree(4)
        self.tree.right.right = Tree(5)

    def test_preorder_tree(self):
        self.assertEqual(list(self.tree.preorder()), [1, 2, 3, 4, 5])

    def test_postorder_tree(self):
        self.assertEqual(list(self.tree.postorder()), [2, 4, 5, 3, 1])

    def test_inorder_tree(self):
        self.assertEqual(list(self.tree.inorder()), [2, 1, 4, 3, 5])
开发者ID:klugjohannes,项目名称:arithmetic-parser,代码行数:19,代码来源:test.py

示例2: TreeTestCase

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import inorder [as 别名]
class TreeTestCase(unittest.TestCase):

    def setUp(self):
        self.tree = Tree()

    def test001(self):
        # creation
        self.assertIsNotNone(self.tree)
        self.assertIsNone(self.tree.root)

    def test002(self):
        # insert
        self.tree.insert(42)
        self.assertEqual(self.tree.root.data, 42)
        
        self.tree.insert(25)
        self.assertEqual(self.tree.root.left.data, 25)
        
        self.tree.insert(777)
        self.assertEqual(self.tree.root.right.data, 777)
        
    def test003(self):
        # length
        for i in range(1, 10):
            self.tree.insert(i)
            self.assertEqual(i, len(self.tree))
        
    def test004(self):
        # iterators
        self.tree.insert([4, 2, 1, 3, 6, 5, 8, 7, 9])
            
        self.assertEqual([4, 2, 1, 3, 6, 5, 8, 7, 9], [n.data for n in self.tree.preorder()])    
        self.assertEqual([1, 3, 2, 5, 7, 9, 8, 6, 4], [n.data for n in self.tree.posorder()])
        self.assertEqual([1, 2, 3, 4, 5, 6, 7, 8, 9], [n.data for n in self.tree.inorder()])
            
    def test005(self):
        # simple remove
        self.tree.insert([42, 25, 777])
        
        self.tree.remove(0)
        self.assertEqual(len(self.tree), 3)
        
        self.tree.remove(777)
        self.assertEqual(len(self.tree), 2)
        
        self.assertEqual([42, 25], [n.data for n in self.tree.preorder()])
            
        self.tree.insert(12)
        self.assertEqual(len(self.tree), 3)

        self.tree.remove(25)
        self.assertEqual(len(self.tree), 2)
        
        self.assertEqual([42, 12], [n.data for n in self.tree.preorder()])
        
    def test006(self):
        # complex remove
        self.tree.insert([42, 25, 777, 12, 36, 555, 999, 600])
            
        self.tree.remove(42)
        self.assertEqual(self.tree.root.data, 555)
        
        self.tree.remove(555)
        self.assertEqual(self.tree.root.data, 600)
            
    def test007(self):
        # stress
        data = range(10, 100)
        random.shuffle(data)
        
        tree = Tree(data)
        self.assertEqual(len(tree), len(data))
        
        random.shuffle(data)
        
        for d in data:
            tree.remove(d)
            
        self.assertEqual(len(tree), 0)
开发者ID:moisesguimaraes,项目名称:tree,代码行数:81,代码来源:test.py

示例3: getpath

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import inorder [as 别名]
    return None
def getpath(root,node):
    l =[]
    r = []
    if root == node:
        return [root]
    if root.left:
        l = getpath(root.left,node)
    if root.right:
        r = getpath(root.right,node)
    if l != [] or r != []:
        return [root] + l + r
    return []

def left_most(root):
    temp = root
    while temp.left:
        temp = temp.left
    return temp

t = Tree()
for i in range(15):
    t.insert(i)
print t.inorder(t.root)
for i in range(15):
    n = t.getNodeinstance(i)
    if inorder_next(t.root,n) != None:
        print n.val,"->",inorder_next(t.root,n).val
    else:
        print n.val,"->",None
开发者ID:aadiuppal,项目名称:programming,代码行数:32,代码来源:inorder_next.py

示例4: TestTree

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import inorder [as 别名]
class TestTree(unittest.TestCase):
    def setUp(self):
        self.tree = Tree()

    def testInsertSingleValue(self):
        self.tree.insert(1)

    def testInsertTwoValues(self):
        self.tree.insert(1)
        self.tree.insert(2)
        self.assertEqual(self.tree.root.data, 1)
        self.assertEqual(self.tree.root.right.data, 2)

    def testInsertTreeValues(self):
        self.tree.insert(1)
        self.tree.insert(2)
        self.tree.insert(3)
        self.assertEqual(self.tree.root.data, 1)
        self.assertEqual(self.tree.root.right.data, 2)
        self.assertEqual(self.tree.root.right.right.data, 3)

    def testMakeSearchTree(self):
        self.tree.insert(2)
        self.tree.insert(1)
        self.tree.insert(3)
        self.assertEqual(self.tree.root.data, 2)
        self.assertEqual(self.tree.root.left.data, 1)
        self.assertEqual(self.tree.root.right.data, 3)

    def decrement(value):
        return value - 1

    def increment(value):
        return value + 1

    def testPreorderTraversalManipulations(self):
        self.tree.insert(2)
        self.tree.insert(1)
        self.tree.insert(3)
        self.tree.insert(4)

        self.tree.preorder(TestTree.increment)
        self.assertEqual(self.tree.root.data, 3)
        self.assertEqual(self.tree.root.left.data, 2)
        self.assertEqual(self.tree.root.right.data, 4)
        self.assertEqual(self.tree.root.right.right.data, 5)

    def testInOrderTraversalManupulations(self):
        self.tree.insert(2)
        self.tree.insert(1)
        self.tree.insert(3)
        self.tree.insert(4)

        self.tree.inorder(TestTree.decrement)
        self.assertEqual(self.tree.root.data, 1)
        self.assertEqual(self.tree.root.left.data, 0)
        self.assertEqual(self.tree.root.right.data, 2)
        self.assertEqual(self.tree.root.right.right.data, 3)

    def testPostOrderTraversalManipulations(self):
        self.tree.insert(2)
        self.tree.insert(1)
        self.tree.insert(3)
        self.tree.insert(4)

        self.tree.postorder(TestTree.decrement)
        self.tree.postorder(TestTree.decrement)
        self.assertEqual(self.tree.root.data, 0)
        self.assertEqual(self.tree.root.left.data, -1)
        self.assertEqual(self.tree.root.right.data, 1)
        self.assertEqual(self.tree.root.right.right.data, 2)

    def testPreOrderTraversalPure(self):
        self.tree.insert(2)
        self.tree.insert(1)
        self.tree.insert(3)
        self.tree.insert(4)

        self.tree.preorder(TestTree.decrement, False)
        self.assertEqual(self.tree.root.data, 2)
        self.assertEqual(self.tree.root.left.data, 1)
        self.assertEqual(self.tree.root.right.data, 3)
        self.assertEqual(self.tree.root.right.right.data, 4)

    def testInOrderTraversalPure(self):
        self.tree.insert(2)
        self.tree.insert(1)
        self.tree.insert(3)
        self.tree.insert(4)

        self.tree.inorder(TestTree.increment, False)
        self.assertEqual(self.tree.root.data, 2)
        self.assertEqual(self.tree.root.left.data, 1)
        self.assertEqual(self.tree.root.right.data, 3)
        self.assertEqual(self.tree.root.right.right.data, 4)

    def testPostOrderTraversalPure(self):
        self.tree.insert(2)
        self.tree.insert(1)
        self.tree.insert(3)
#.........这里部分代码省略.........
开发者ID:itsankoff,项目名称:AI,代码行数:103,代码来源:unittests.py


注:本文中的tree.Tree.inorder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。