Example #1
0
        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()
Example #2
0
		ans=[]
		line=0
		while queue:
			level=[]
			size=len(queue)
			for i in range(size):
				node=queue.pop(0)
				if node:
					if line%2==0:
						level.append(node.key)
					else:
						level.insert(0,node.key)
				
				if node.left:
					queue.append(node.left)
				if node.right:
					queue.append(node.right)

			ans.append(level)
			line+=1
		return ans

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)

print zigzag(tree)
Example #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
Example #4
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)
from binaryTree import BinaryTree

tree = BinaryTree(20)
tree.insert_left(8)
tree.insert_right(22)
tree.get_left_child().insert_left(4)
tree.get_left_child().insert_right(12)
tree.get_left_child().get_right_child().insert_left(10)
tree.get_left_child().get_right_child().insert_right(14)


def lca_without_parent(root, value1, value2):
    while root != None:
        if root.key > value1 and root.key > value2:
            root = root.left
        elif root.key < value1 and root.key < value2:
            root = root.right
        else:
            return root.key


print lca_without_parent(tree, 4, 14)
Example #6
0
from binaryTree import BinaryTree

tree = BinaryTree(5)
tree.insert_left(4)
tree.insert_right(5)
# tree.get_left_child().insert_left(3)
# tree.get_right_child().insert_right(7)

l = []


def check(tree):
    if tree != None:
        if tree.left:
            if tree.left.key < tree.key:
                l.append("bST")
                check(tree.left)
            else:
                l.append("not BST")

        if tree.right:
            if tree.right.key > tree.key:
                l.append("BSt")
                check(tree.right)
            else:
                l.append("Not BSt")

    return l


print check(tree)
Example #7
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]
Example #8
0
from binaryTree import BinaryTree

tree = BinaryTree(10)
tree.insert_left(2)
tree.insert_right(7)
tree.get_left_child().insert_left(8)
tree.get_left_child().insert_right(4)


def preorder(root):
    if root != None:
        print root.key,
        preorder(root.left)
        preorder(root.right)


def inorder(root, l):
    if root != None:
        inorder(root.left, l)
        l.append(root.key)
        inorder(root.right, l)


def replace_inorder(root, l):
    if root != None:
        replace_inorder(root.left, l)
        root.key = l[replace_inorder.index]
        replace_inorder.index += 1
        replace_inorder(root.right, l)

Example #9
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]
Example #10
0
from binaryTree import BinaryTree

tree = BinaryTree(5)
tree.insert_left(4)
tree.insert_right(5)
# tree.get_left_child().insert_left(3)
# tree.get_right_child().insert_right(7)


l=[]
def check(tree):
	if tree!=None:
		if tree.left:
			if tree.left.key<tree.key:
				l.append("bST")
				check(tree.left)
			else:
				l.append("not BST")

		if tree.right:
			if tree.right.key>tree.key:
				l.append("BSt")
				check(tree.right)
			else:
				l.append("Not BSt")

	return l

print check(tree)

# def check(tree):
Example #11
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())
Example #12
0
from binaryTree import BinaryTree

tree = BinaryTree(1)
tree.insert_left(1)
tree.insert_right('A')
tree.left.insert_left('B')
tree.left.insert_right('C')


def leaf(node):
    if not (node.right or node.left):
        return True


def huffman(root, code):
    temp = root
    if temp == None:
        return
    for i in code:
        if i == '1':
            if temp.right:
                temp = temp.right
                if leaf(temp):
                    print temp.key
                    temp = root
                else:
                    continue

        elif i == '0':
            if temp.left:
                temp = temp.left
Example #13
0
from binaryTree import BinaryTree

tree=BinaryTree(1)
tree.insert_left(1)
tree.insert_right('A')
tree.left.insert_left('B')
tree.left.insert_right('C')

def leaf(node):
	if not (node.right or node.left):
		return True

def huffman(root,code):
	temp=root
	if temp==None:
		return
	for i in code:
		if i=='1':
			if temp.right:
				temp=temp.right
				if leaf(temp):
					print temp.key
					temp=root
				else:
					continue

		elif i=='0':
			if temp.left:
				temp=temp.left
				if leaf(temp):
					print temp.key
Example #14
0
from binaryTree import BinaryTree

tree=BinaryTree(1)
tree.insert_left(2)
tree.insert_right(3)

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

def recover(root):
	y=[]
	if root==None:
		return
	p=inorder(root)
	for i in range(len(p)-1):
		if p[i]>p[i+1]:
			y.extend([p[i],p[i+1]])

	return [min(y),max(y)]

print recover(tree)