Пример #1
0
tree = BST()
tree.ins(tree.root,6)
tree.ins(tree.root,2)
tree.ins(tree.root,4)
tree.ins(tree.root,0)
tree.ins(tree.root,3)
tree.ins(tree.root,5)
tree.ins(tree.root,1)
tree.ins(tree.root,-1)

tree.ins(tree.root,8)
tree.ins(tree.root,7)
tree.ins(tree.root,10)
tree.ins(tree.root,9)
tree.ins(tree.root,11)
inorder(tree.root)
print "\n"

##N^2 complexity
def BSTDLL(x,flag):
    if x==None:
        return
    if x.left==None and x.right==None:
        return x
    l=None
    r=None
    if x.left:
        l = BSTDLL(x.left,'r')
    if x.right:
        r = BSTDLL(x.right,'l')
    if l:
Пример #2
0
    if x == None:
        H.h = 0
        return 0
    lh.h += 1
    rh.h += 1
    ld = FindDia(x.left, lh)
    rd = FindDia(x.right, rh)
    H.h = max(lh.h, rh.h) + 1
    return max(lh.h + rh.h + 1, max(ld, rd))


a = BTree()
a.insertL(a.root, 9)
a.insertL(a.root, 8)
a.insertL(a.root, 7)
a.insertL(a.root, 6)
a.insertL(a.root, 5)
a.insertL(a.root, 4)
a.insertL(a.root, 3)
a.insertL(a.root, 2)
a.insertL(a.root, 1)

a.insertR(a.root, 0)
a.insertR(a.root, 15)
a.insertR(a.root, 11)
a.insertR(a.root, 14)
s = Height()
print FindDia(a.root, s)
print "\n inorder"
inorder(a.root)
Пример #3
0
from BTreeAndBST import bTree, inorder
from BaseClass import Queue


def convertMirror(x):
    if x == None:
        return
    temp = x.left
    x.left = x.right
    x.right = temp
    convertMirror(x.left)
    convertMirror(x.right)
    return x


inorder(bTree.root)


def reverLO():
    arr = []
    queue = Queue()
    queue.enq(bTree.root)
    while queue.isEmpty() == False:
        x = queue.deq()
        arr.append(x)
        if x.right:
            queue.enq(x.right)
        if x.left:
            queue.enq(x.left)

    while len(arr) != 0:
Пример #4
0
    rh =Height()
    if x==None:
        H.h =0
        return 0
    lh.h +=1
    rh.h +=1
    ld = FindDia(x.left,lh)
    rd = FindDia(x.right,rh)
    H.h =max(lh.h,rh.h)+1
    return max(lh.h + rh.h + 1, max(ld,rd))

a = BTree()
a.insertL(a.root,9)
a.insertL(a.root,8)
a.insertL(a.root,7)
a.insertL(a.root,6)
a.insertL(a.root,5)
a.insertL(a.root,4)
a.insertL(a.root,3)
a.insertL(a.root,2)
a.insertL(a.root,1)

a.insertR(a.root,0)
a.insertR(a.root,15)
a.insertR(a.root,11)
a.insertR(a.root,14)
s = Height()
print FindDia(a.root,s)
print "\n inorder"
inorder(a.root)