コード例 #1
0
def tree():
    '''
          10
        0    15
     -2    4    18
    '''
    t = Tree()
    for n in (10, 15, 0, 4, 18, -2):
        t.insert(n)
    return t
コード例 #2
0
 def test_height_of_known_height_tree(self):
   '''
   assert that the height of a tree known to be 4 is actually 4
   '''
   t = Tree()
   t.add(1)
   t.add(2)
   t.add(3)
   t.add(4)
   self.assertEqual(t.height(), 4)
コード例 #3
0
 def create_three_node_tree(self):
   '''
   create a sample tree which looks like:
       23
       /\ 
     15  47
   '''
   t = Tree()
   t.add(23)
   t.add(15)
   t.add(47)
   return t
コード例 #4
0
 def test_4_node_right_tree(self):
   '''
   another sample tree, this one with only right nodes
   13 \ 14 \ 15
   '''
   t = Tree()
   t.add(13)
   t.add(14)
   t.add(15)
   self.assertIsNone(t.root.left)
   self.assertEqual(t.root.value, 13)
   self.assertEqual(t.root.right.value, 14)
   self.assertEqual(t.root.right.right.value, 15)
コード例 #5
0
ファイル: bst.py プロジェクト: jwjohns/binary-tree_python
import sys
from os import path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from binary_search_tree import Tree

#from .binary_search_tree import Tree

bst = Tree()
bst.insert(10)
print(bst.insert(15))
bst.preorder()
bst.postorder()
bst.inorder()
コード例 #6
0
ファイル: bst.py プロジェクト: jwjohns/binary-tree_python
import sys
from os import path
sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )
from binary_search_tree import Tree

#from .binary_search_tree import Tree


bst = Tree()
bst.insert(10)
print(bst.insert(15))
bst.preorder()
bst.postorder()
bst.inorder()
コード例 #7
0
 def create_sample_tree(self):
     '''
 create a sample tree to use in the traversal implementations
 '''
     t = Tree()
     t.add(100)
     t.add(50)
     t.add(25)
     t.add(75)
     t.add(150)
     t.add(125)
     t.add(175)
     t.add(110)
     return t.root
コード例 #8
0
def test_empty_tree_find_fails():
    t = Tree()
    assert t.find(10) is False
コード例 #9
0
 def test_one_node_tree(self):
   t = Tree()
   t.add(23)
   self.assertEqual(t.root.value, 23)
コード例 #10
0
 def test_height_of_zero_node_tree(self):
   '''
   assert that the height of a tree with no nodes should be 0
   '''
   t = Tree()
   self.assertEqual(t.height(), 0)
コード例 #11
0
 def create_sample_tree(self):
   '''
   create a sample tree to use in the traversal implementations
   '''
   t = Tree()
   t.add(100)
   t.add(50)
   t.add(25)
   t.add(75)
   t.add(150)
   t.add(125)
   t.add(175)
   t.add(110)
   return t.root
コード例 #12
0
            if left > root.data:
#                print("Left greater than root")
                raise Exception
        else:
            left = None
        if left and right:
            if right < left:
#                print("Left greater than right")
                raise Exception
    else:
        return True
    is_valid(root.right)
    is_valid(root.left)
    
if __name__ == "__main__":
    tree = Tree()
    tree.insert(1)
    tree.insert(2)
    tree.insert(10)
    tree.insert(6)
    try:
        is_valid(tree.root)
    except:
        print("Tree is invalid")
     
        
    invalid_root = Node(2)
    right = Node(4)
    left = Node(5)
    invalid_root.right = right
    invalid_root.left = left
コード例 #13
0
ファイル: bst_client.py プロジェクト: ankuagar/leetcode
#!/usr/bin/env python3

import unittest
from binary_search_tree import Tree, InorderIterator, InorderIterator1, TreeNode


def insert(tree, lst):
    for elem in lst:
        tree.insert(elem)


t = unittest.TestCase()

# pre_order testcases
lst = [100, 50, 200, 25, 125, 350]
tree = Tree()
insert(tree, lst)
t.assertEqual([100, 50, 25, 200, 125, 350], tree.pre_order())

lst = [100]
tree = Tree()
insert(tree, lst)
t.assertEqual([100], tree.pre_order())

lst = []
tree = Tree()
insert(tree, lst)
t.assertEqual([], tree.pre_order())

lst = [1, 2, 3, 4, 5, 6, 7, 8]
tree = Tree()
コード例 #14
0
 def test_insert_and_search(self):
     bst = Tree()
     bst.insert(5)
     self.assertEqual(bst.search(5), True)
     bst.insert(15)
     self.assertEqual(bst.search(15), True)
     bst.insert(6)
     bst.insert(7)
     self.assertEqual(bst.search(6), True)