示例#1
0
    a[i][i-1]=0
    a[i][i]=p[i]
    r[i][i]=i
    r[i][i-1]=0
a[n+1][n]=0
r[n+1][n]=0

for diagonal in range(1,n):
    for i in range(1,n-diagonal+1):
        j=i+diagonal
        min=10000
        minpoint=0
        for k in range(i,j+1):
            psum=sum(p[i:j+1])
            if (a[i][k-1]+a[k+1][j]+psum < min):
                min=a[i][k-1]+a[k+1][j]+psum
                minpoint=k
        a[i][j]=min
        r[i][j]=minpoint                                            


utility.printMatrixF(a)
print()
utility.printMatrix(r)

root=tree(key,r,1,n)

utility.print_inOrder(root)
print()
utility.print_preOrder(root)
示例#2
0

def bin_search_tree_insert(root, node):
    if root is None:
        root = node
    else:
        if (root.data > node.data):
            if (root.l_child is None):
                root.l_child = node
            else:
                bin_search_tree_insert(root.l_child, node)
        else:
            if (root.r_child == None):
                root.r_child = node
            else:
                bin_search_tree_insert(root.r_child, node)


#def bin_search_t:
#    tree_delete(root,node):

r = Node(7)
bin_search_tree_insert(r, Node(9))
bin_search_tree_insert(r, Node(1))
bin_search_tree_insert(r, Node(3))
bin_search_tree_insert(r, Node(12))

utility.print_inOrder(r)
print()
utility.print_preOrder(r)