Esempio n. 1
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
Esempio n. 2
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()
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)
Esempio n. 4
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)
Esempio n. 5
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]
Esempio n. 6
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)
Esempio n. 7
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)
	if root.right:
		leaf(root.right)
Esempio n. 8
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)
Esempio n. 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]
Esempio n. 10
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())
Esempio n. 11
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
Esempio n. 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
				if leaf(temp):
					print temp.key
Esempio n. 13
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)