def test():
    T = Node(17)
    T.left = Node(33)
    T.left.left = Node(19)
    T.left.right = Node(16)
    T.left.right.left = Node(38)
    T.left.right.right = Node(31)
    T.right = Node(48)
    T.right.left = Node(11)
    T.right.right = Node(14)
    print(Height(T))
Ejemplo n.º 2
0
def test():
    T = Node(17)
    T.left = Node(13)
    T.right = Node(26)
    T.left.left = Node(14)
    T.left.right = Node(5)
    print("Preorder : ")
    PreorderTraverse(T)
    print("-------")
    print("Inorder : ")
    InorderTraverse(T)
    print("-------")
    print("Postorder : ")
    PostorderTraverse(T)
    print("-------")
    print("Levelorder : ")
    LevelorderTraverse(T)
    if root.left==None and root.right==None:
      return 0

    #recursively compute intermediate nodes in left and right subtree 
    left_subtree_non_leaf=non_leaf(root.left)
    right_subtree_non_leaf=non_leaf(root.right)

    #compute the total intermediates node by considering current intermediate node , left_subtree_non_leaf node and right_subtree_non_leaf node
    total_non_leaf=left_subtree_non_leaf+right_subtree_non_leaf+1
    
    return total_non_leaf
 
    
    

a=Node(1)
b=Node(2)
c=Node(3)
d=Node(4)
e=Node(5)
f=Node(6)
g=Node(7)
a.left=b
a.right=c
b.left=d
b.right=e
c.left=f
c.right=g

result=non_leaf(a)
print(result)
Ejemplo n.º 4
0
    if root.left == None and root.right == None:
        return 1

    #recursively compute the levels of left and right subtree
    left_subtree_levels = levels(root.left)
    right_subtree_levels = levels(root.right)

    #compute the overall levels of tree
    total_levels = max(left_subtree_levels, right_subtree_levels) + 1

    return total_levels


a = Node(1)
b = Node(2)
c = Node(3)
d = Node(4)
e = Node(5)
f = Node(6)
g = Node(7)
h = Node(8)
a.left = b
a.right = c
b.left = d
b.right = e
c.left = f
d.left = g
g.left = h

result = diameter(a)
print(result)
        stack.append(current)  #while moving push node onto stack
        current = current.left

    #now perform the backtracking and print the nodes data
    while len(stack) != 0:
        item = stack.pop()
        print(item.data)

        #if node have right children then again move to the left most leaf node
        if item.right != None:
            current = item.right
            while current != None:
                stack.append(current)  #while moving push node onto stack
                current = current.left


a = Node(1)
b = Node(2)
c = Node(3)
d = Node(4)
e = Node(5)
f = Node(6)
g = Node(7)
a.left = b
a.right = c
b.left = d
b.right = e
c.left = f
c.right = g
inorder(a)
            return is_bst(root.left)
        else:
            return False

    #intermidiate node with right children only
    elif root.right != None:
        if root.data <= root.right.data:
            return is_bst(root.right)
        else:
            return False

    # return False


n1 = Node(1)
n2 = Node(2)
n3 = Node(3)
n4 = Node(4)
n5 = Node(5)
n6 = Node(6)
n7 = Node(7)

n4.left = n2
n4.right = n6
n2.left = n1
n2.right = n3
n6.left = n5
n6.right = n7

result = is_bst(n4)
print(result)
Ejemplo n.º 7
0
        return equal(root1.left, root2.left) and equal(root1.right,
                                                       root2.right)

    else:
        return False


a = Node(1)
b = Node(2)
c = Node(3)
d = Node(4)
e = Node(5)
f = Node(6)
g = Node(7)
a.left = b
a.right = c
b.left = d
b.right = e
c.left = f
c.right = g

a1 = Node(1)
b1 = Node(2)
c1 = Node(3)
d1 = Node(4)
e1 = Node(5)
f1 = Node(6)
g1 = Node(7)
a1.left = b1
a1.right = c1
b1.left = d1
Ejemplo n.º 8
0
    if root.left == None and root.right == None:
        return 0

    #recursively compute the height of left and right subtree
    left_subtree_height = height(root.left)
    right_subtree_height = height(root.right)

    #compute the overall height of tree
    total_height = max(left_subtree_height, right_subtree_height) + 1

    return total_height


a = Node(1)
b = Node(2)
c = Node(3)
d = Node(4)
e = Node(5)
f = Node(6)
g = Node(7)
h = Node(8)
a.left = b
a.right = c
b.left = d
b.right = e
c.left = f
d.left = g
g.left = h

result = height(a)
print(result)
    if not root:
        return True

    #if leaf node
    if root.left == None and root.right == None:
        return True

    #recursively check for left and right subtree
    if root.left != None and root.right != None:
        return strict_binary_tree(root.left) and strict_binary_tree(root.right)

    else:
        return False


a = Node(1)
b = Node(2)
c = Node(3)
d = Node(4)
e = Node(5)
f = Node(6)
g = Node(7)
a.left = b
a.right = c
b.left = d
b.right = e
c.left = f
c.right = g

result = strict_binary_tree(a)
print(result)