def test_check_complete_falsify(self):
        root = Node(0)
        root.left = Node(1)
        root.right = Node(2)
        root.right.left = Node(3)
        btree = tree.BinaryTree(root)
        self.assertFalse(btree.is_complete)

        # subtrees are complete
        root.left.left = Node(4)
        self.assertFalse(btree.is_complete)
        root.right.right = Node(5)
        self.assertFalse(btree.is_complete)

        root.left.left = None
        root.right.right = None

        # left and right subtrees have the same number of nodes
        root.left.right = Node(4)
        self.assertFalse(btree.is_complete)

        root.right.left = None
        self.assertFalse(btree.is_complete)

        # left and right subtrees are of equal height
        root.left.left = Node(3)
        root.right.right = Node(5)
        self.assertFalse(btree.is_complete)

        # left subtree is taller than right by more than 1
        root.right.right = None
        root.left.left.left = Node(6)
        root.left.left.right = Node(7)
        self.assertFalse(btree.is_complete)
    def test_check_perfect(self):
        root = Node(0)
        root.left = Node(1)
        root.right = Node(2)
        btree = tree.BinaryTree(root)
        self.assertTrue(btree.is_perfect)

        root.left.left = Node(3)
        self.assertFalse(btree.is_perfect)

        root.left.right = Node(4)
        self.assertTrue(btree.is_perfect)

        root.right.left = Node(5)
        self.assertFalse(btree.is_perfect)

        root.right.right = Node(6)
        self.assertTrue(btree.is_perfect)

        temp = root.right
        root.right = None
        self.assertFalse(btree.is_perfect)

        root.right = temp
        temp = root.left
        root.left = None
        self.assertFalse(btree.is_perfect)
    def test_check_full(self):
        root = Node(0)
        btree = tree.BinaryTree(root)
        self.assertTrue(btree.is_full)

        root.left = Node(1)
        self.assertFalse(btree.is_full)

        root.right = Node(2)
        self.assertTrue(btree.is_full)
 def test_postorder_traversal(self):
     ordering = []
     root = Node(0)
     root.left = Node(1)
     root.right = Node(2)
     root.left.left = Node(3)
     root.right.left = Node(4)
     root.right.right = Node(5)
     btree = tree.BinaryTree(root)
     btree.traverse_postorder(store_ordering(ordering))
     self.assertEqual(ordering, [3, 1, 4, 5, 2, 0])
示例#5
0
 def test_add_to_del(self):
     checktree = tree.BinaryTree()
     checktree.add(23)
     checktree.add(65)
     checktree.add(12)
     checktree.add(80)
     checktree.delete(23)
     checktree.delete(65)
     checktree.delete(12)
     checktree.delete(80)
     self.assertTrue(checktree.is_empty())
    def test_check_complete_confirm(self):
        root = Node(0)
        root.left = Node(1)
        root.right = Node(2)
        btree = tree.BinaryTree(root)
        self.assertTrue(btree.is_complete)

        root.left.left = Node(3)
        self.assertTrue(btree.is_complete)

        root.left.right = Node(4)
        self.assertTrue(btree.is_complete)
    def test_calc_height(self):
        root = Node(0)
        root.left = Node(1)
        root.right = Node(2)
        root.left.left = Node(3)
        root.right.left = Node(4)
        root.right.right = Node(5)
        btree = tree.BinaryTree(root)
        self.assertEqual(btree.height, 2)

        root.right.right.right = Node(6)
        self.assertEqual(btree.height, 3)
    def test_check_balanced(self):
        root = Node(0)
        root.left = Node(1)
        root.right = Node(2)
        root.right.left = Node(3)
        root.right.right = Node(4)
        root.right.right.left = Node(5)
        btree = tree.BinaryTree(root)
        self.assertFalse(btree.is_balanced)

        root.left.left = Node(6)
        self.assertTrue(btree.is_balanced)
示例#9
0
 def ExampleTree(self):
   a = bt.Node(314)
   b = bt.Node(6)
   c = bt.Node(271)
   d = bt.Node(28)
   e = bt.Node(0)
   f = bt.Node(561)
   g = bt.Node(3)
   h = bt.Node(17)
   i = bt.Node(6)
   j = bt.Node(2)
   k = bt.Node(1)
   l = bt.Node(401)
   m = bt.Node(641)
   n = bt.Node(257)
   o = bt.Node(271)
   p = bt.Node(28)
   tree = bt.BinaryTree(a)
   a.left = b
   b.parent = a
   b.left = c
   c.parent = b
   c.left = d
   d.parent = c
   c.right = e
   e.parent = c
   b.right = f
   f.parent = b
   f.right = g
   g.parent = f
   g.left = h
   h.parent = g
   a.right = i
   i.parent = a
   i.left = j
   j.parent = i
   j.right = k
   k.parent = j
   k.left = l
   l.parent = k
   l.right = m
   m.parent = l
   k.right = n
   n.parent = k
   i.right = o
   o.parent = i
   o.right = p
   p.parent = o
   return tree
 def __init__(self, root):
     if isinstance(root, tree.BinaryTree):
         if btree.is_min_heap:
             super().__init__(root)
         else:
             raise ValueError('Given tree is not a min heap')
     elif isinstance(root, tree.Node):
         btree = tree.BinaryTree(root)
         if btree.is_min_heap:
             super().__init__(root)
         else:
             raise ValueError('Given root node is not part of a min heap')
     else:
         raise TypeError(
             'MinHeap must be initialized with a Node or BinaryTree')
    def test_count_nodes(self):
        btree = tree.BinaryTree()
        self.assertEqual(btree.nodes, 0)

        root = Node(0)
        btree.root = root
        self.assertEqual(btree.nodes, 1)

        root.left = Node(1)
        self.assertEqual(btree.nodes, 2)

        root.right = Node(2)
        self.assertEqual(btree.nodes, 3)

        root.right.right = Node(3)
        self.assertEqual(btree.nodes, 4)

        root.right.left = Node(4)
        self.assertEqual(btree.nodes, 5)
示例#12
0
 def testIsHeightBalanced(self):
   a = bt.Node('a')
   b = bt.Node('b')
   c = bt.Node('c')
   d = bt.Node('d')
   e = bt.Node('e')
   f = bt.Node('f')
   g = bt.Node('g')
   tree = bt.BinaryTree(a)
   a.left = b
   b.left = c
   b.right = d
   d.right = e
   a.right = f
   f.right = g
   # Tree is originally height-balanced
   self.assertTrue(tree.is_height_balanced())
   # Without node 'g', no longer true
   f.right = None
   self.assertFalse(tree.is_height_balanced())
示例#13
0
 def setUp(self):
     self.T = tree.BinaryTree()
     self.root = self.T.addNode(0)
     for i in range(10):
         self.T.insert(self.root, i)
示例#14
0
res, classnum, classnuml = preprocess(training_file)
# No of attributes
c_high = len(res[0]) - 1
g_vars.init(classnum + 1, classnuml, c_high, option)
attr = []
for i in range(c_high):
    attr.append(i)
# print attr
if option == "forest3":
    trees = []
    Btrees = []
    for i in range(3):
        trees.append(DTL(res, attr, DISTRIBUTION(res)))
    for i in trees:
        Btrees.append(t.BinaryTree(i))
    for i in range(len(Btrees)):
        Btrees[i].preorder_print(i + 1, start=trees[i])
    tree = trees
elif option == "forest15":
    trees = []
    Btrees = []
    for i in range(15):
        trees.append(DTL(res, attr, DISTRIBUTION(res)))
    for i in trees:
        Btrees.append(t.BinaryTree(i))
    tree = trees
else:
    tree = []
    tree.append(DTL(res, attr, DISTRIBUTION(res)))
    Mtree = t.BinaryTree(tree[0])
示例#15
0
 def test_find(self):
     checktree = tree.BinaryTree()
     checktree.add(32)
     self.assertTrue(checktree.find(32))
     self.assertFalse(checktree.find(123))