inorder(tree.right)
    return l


def Symmetry():
    equal = True
    sym = inorder(tree)
    length = len(sym)
    print sym, length

    while length > 1 and equal:
        first = sym.pop()
        last = sym.pop(0)
        length -= 2
        if first == last:
            equal = True
            continue
        else:
            equal = False
    print equal


tree = BinaryTree(3)
tree.insert_left(9)
tree.insert_right(20)
tree.get_left_child().insert_left(4)
tree.get_left_child().insert_right(5)
tree.get_right_child().insert_left(15)
tree.get_right_child().insert_right(7)

Symmetry()
Beispiel #2
0
from binaryTree import BinaryTree

tree = BinaryTree(26)
tree.insert_left(10)
tree.insert_right(3)
tree.get_left_child().insert_left(6)
tree.get_left_child().insert_right(4)
tree.get_right_child().insert_right(3)

def sum_nodes(root):
	if root==None:
		return 0
	return sum_nodes(root.left) + root.key + sum_nodes(root.right)

def sumtree(root):
	if root==None or (root.left==None and root.right==None):
		return 1
	ls = sum_nodes(root.left)
	rs = sum_nodes(root.right)

	if (root.key == ls+rs) and (sumtree(root.left)) and (sumtree(root.right)):
		return 1
	return 0


print sumtree(tree)
Beispiel #3
0
from binaryTree import BinaryTree

tree = BinaryTree(5)
tree.insert_left(4)
tree.insert_right(8)
tree.get_left_child().insert_left(11)
# tree.get_left_child().insert_right(2)
tree.get_right_child().insert_left(13)
tree.get_right_child().insert_right(4)
tree.get_left_child().get_left_child().insert_left(7)
tree.left.left.insert_right(2)
tree.right.right.insert_right(1)
tree.right.right.insert_left(5)

# def path(tree):
# 	paths = []
# 	if not (tree.left or tree.right):
# 		return [[tree.key]]    # this will return all the leaf nodes
# 	if tree.left:
# 		paths.extend([[tree.key] + child for child in path(tree.left)])
# 	if tree.right:
# 		paths.extend([[tree.key] + child for child in path(tree.right)])
# 	return paths

# k=22
# p=[]
# p=path(tree)
# print p
# for i in p:
# 	if sum(i)==k:
# 		print 1
Beispiel #4
0
from binaryTree import BinaryTree

tree = BinaryTree(3)
tree.insert_left(5)
tree.insert_right(1)
tree.get_left_child().insert_left(6)
tree.get_left_child().insert_right(2)
tree.get_right_child().insert_left(0)
tree.get_right_child().insert_right(8)
#tree.get_left_child().get_left_child().insert_left(8)
tree.get_left_child().get_right_child().insert_left(7)
tree.get_left_child().get_right_child().insert_right(4)
# tree.get_right_child().get_left_child().insert_left(16)
# tree.get_right_child().get_right_child().insert_right(27)

l=[]
def inorder(tree):
	global l
	if tree==None:
		return
	else:
		inorder(tree.left)
		l.append(tree.key)
		inorder(tree.right)
	return l

def depth(root,node):
	count=0
	if root==None:
		return 0
	q=[root]
from binaryTree import BinaryTree

tree = BinaryTree(1)
tree.insert_left(2)
tree.insert_right(3)
tree.get_left_child().insert_left(4)
tree.get_left_child().insert_right(5)
tree.get_right_child().insert_left(6)
tree.get_right_child().insert_right(7)
tree.left.left.insert_left(8)
tree.left.left.insert_right(9)
tree.right.left.insert_left(10)
tree.right.left.insert_right(11)
tree.right.right.insert_right(12)

p=[]
def leftmost(root):
	global p
	if root.left:
		p.append(root.key)
		leftmost(root.left)

def leaf(root):
	global p
	if not (root.right or root.left):
		p.append(root.key)
	if root.left:
		leaf(root.left)
	if root.right:
		leaf(root.right)
Beispiel #6
0
from binaryTree import BinaryTree

tree = BinaryTree(3)
tree.insert_left(5)
tree.insert_right(1)
tree.get_left_child().insert_left(6)
tree.get_left_child().insert_right(2)
tree.get_right_child().insert_left(0)
tree.get_right_child().insert_right(8)
#tree.get_left_child().get_left_child().insert_left(8)
tree.get_left_child().get_right_child().insert_left(7)
tree.get_left_child().get_right_child().insert_right(4)
# tree.get_right_child().get_left_child().insert_left(16)
# tree.get_right_child().get_right_child().insert_right(27)

l=[]
def inorder(tree):
	global l
	if tree==None:
		return
	else:
		inorder(tree.left)
		l.append(tree.key)
		inorder(tree.right)
	return l

def depth(root,node):
	count=0
	if root==None:
		return 0
	q=[root]
Beispiel #7
0
from binaryTree import BinaryTree
root = BinaryTree('a')
print(root)
print(root.get_root_val())
print(root.get_left_child())
root.insert_left('b')
print(root.get_left_child().get_root_val())
root.insert_right('c')
print(root.get_right_child().get_root_val())
root.get_right_child().set_root_val('hello')
print(root.get_right_child().get_root_val())
root.insert_left('d')
print(root.get_left_child())
print(root.get_left_child().get_left_child().get_root_val())
Beispiel #8
0
from binaryTree import BinaryTree

tree = BinaryTree(1)
tree.insert_left(2)
tree.insert_right(3)
tree.get_left_child().insert_left(4)
tree.get_left_child().insert_right(5)
tree.get_right_child().insert_left(6)
tree.get_right_child().insert_right(7)
tree.left.left.insert_left(8)
tree.left.left.insert_right(9)
tree.right.left.insert_left(10)
tree.right.left.insert_right(11)
tree.right.right.insert_right(12)

p = []


def leftmost(root):
    global p
    if root.left:
        p.append(root.key)
        leftmost(root.left)


def leaf(root):
    global p
    if not (root.right or root.left):
        p.append(root.key)
    if root.left:
        leaf(root.left)