def Decode_by_Tree(T, codeString):
    space = ' '
    temp = BinaryTree(space)
    temp = T
    phraseString = ""
    check = True
    Decode_phrase_list = []
    Decode_phrase_list.append(phraseString)
    Decode_phrase_list.append(check)
    codeStringlist = []
    for n in codeString:
        codeStringlist.append(n)
    foundchar = False
    while (check and (len(codeStringlist) > 0)):
        digit = codeStringlist.pop(0)
        if (digit == '0'):
            if (temp.getLeftChild() != None):
                if (temp.getLeftChild().getRootVal() == space):
                    temp = temp.getLeftChild()
                    if ((len(codeStringlist) == 0)):
                        check = False

                else:
                    phraseString = phraseString + temp.getLeftChild(
                    ).getRootVal()
                    temp = T
            else:
                check = False
        else:
            if (digit == '1'):
                if (temp.getRightChild() != None):
                    if (temp.getRightChild().getRootVal() == space):
                        temp = temp.getRightChild()
                        if ((len(codeStringlist) == 0)):
                            check = False
                    else:
                        phraseString = phraseString + temp.getRightChild(
                        ).getRootVal()
                        temp = T
                else:
                    check = False

    Decode_phrase_list[0] = phraseString
    Decode_phrase_list[1] = check
    return Decode_phrase_list
def main():
	bookTree = BinaryTree("Book")
	bookTree.insertLeft("Chapter1")
	bookTree.insertRight("Chapter2")

	chap1 = bookTree.getLeftChild()
	chap2 = bookTree.getRightChild()

	chap1.insertLeft("Sec 1.1")
	chap1.insertRight("Sec 1.2")

	chap2.insertLeft("Sec 2.1")
	chap2.insertRight("Sec 2.2")

	pre = preorder(bookTree)
	post = postorder(bookTree)
	inO = inorder(bookTree)
	print("In pre order: ", pre)
	print("In post order: ", post)
	print("In order: ", inO )
Esempio n. 3
0
from BinaryTree import BinaryTree

r = BinaryTree('a')
print(r.getRootVal())

print(r.getLeftChild())

r.insertLeft('b')
print(r.getLeftChild())
print(r.getRootVal())

r.insertRight('c')
print(r.getRightChild())
print(r.getRightChild().getRootVal())

r.getRightChild().setRootVal('not today')
print(r.getRightChild().getRootVal())
from BinaryTree import BinaryTree

#       a
#    /    \
#   b      c
#    \    / \
#     d  e   f

bt = BinaryTree('a')

bt.insertRight('f')
bt.insertRight('c')
bt.getRightChild().insertLeft('e')

bt.insertLeft('b')
bt.getLeftChild().insertRight('d')


def preorder(bt):
    if bt:
        print(bt.getRootValue())
        preorder(bt.getLeftChild())
        preorder(bt.getRightChild())


print("Preorder : ")
preorder(bt)
print("---------------------------")


def inorder(bt):
Esempio n. 5
0
def Encode_by_Tree(charcodelist):
    space = ' '
    myTree_check_list = []
    mytree = BinaryTree(space)
    check = True
    myTree_check_list.append(mytree)
    myTree_check_list.append(check)
    print(myTree_check_list)
    for i in range(0, len(charcodelist) - 1):
        print("#####################################")
        if (not check):
            break
        line = charcodelist[i]
        mychar = line[0]
        mycode = line[1]
        mycode = mycode + mychar
        temp = BinaryTree(space)
        temp = mytree
        for digit in range(0, len(mycode) - 1):
            if (digit == (len(mycode) - 2)):
                digit = digit + 1

            print("-----------------------------------")
            if (mycode[digit] == '0'):
                print("Go left")
                if (temp.getLeftChild() == None):
                    print("Add Left")
                    temp.insertLeft(space)
                    temp = temp.getLeftChild()
                else:
                    if (temp.getLeftChild().getRootVal() == space):
                        print("just go left ")
                        temp = temp.getLeftChild()
                    else:
                        check = False
                        print(
                            " Erorr ______on going left________________________________________",
                            mycode[len(mycode) - 1])
                        break

            else:
                if (mycode[digit] == '1'):
                    print("Go Right")
                    if (temp.getRightChild() == None):
                        print("Add Right")
                        temp.insertRight(space)
                        temp = temp.getRightChild()
                    else:
                        if (temp.getRightChild().getRootVal() == space):
                            print("just go Right")
                            temp = temp.getRightChild()
                        else:
                            check = False
                            print(
                                " Erorr ______on going Right________________________________________",
                                mycode[len(mycode) - 1])
                            break

                else:
                    if (mycode[len(mycode) - 2] == '0'):
                        try:
                            if (temp.getLeftChild().getRootVal() != None):
                                check = False
                                print(
                                    " Erorr ______on Adding Left ________________________________________",
                                    mycode[len(mycode) - 1])
                                break
                        except AttributeError:
                            temp.insertLeft(mycode[digit])
                            print("Adding to the left of the tree ",
                                  mycode[digit])
                    if (mycode[len(mycode) - 2] == '1'):
                        try:
                            if (temp.getRightChild().getRootVal() != None):
                                check = False
                                print(
                                    " Erorr ______on Adding Right ________________________________________",
                                    mycode[len(mycode) - 1])
                                break
                        except AttributeError:
                            temp.insertRight(mycode[digit])
                            print("Adding to the Right of the tree ",
                                  mycode[digit])

        print(charcodelist[i])
    myTree_check_list[0] = mytree
    myTree_check_list[1] = check
    return myTree_check_list
#Create a node and reference representation of the
#tree shown in Figure 6.6(a) on p. 238 of Ranum and Miller.
#The nodes are added level by level working downwards.
print('Creating tree')
#Level 0
t = BinaryTree('a')

#Level 1
t.insertLeft('b')
t.insertRight('c')

#Level 2
t.getLeftChild().insertLeft('d')
t.getLeftChild().insertRight('e')
t.getRightChild().insertLeft('f')


#Function to convert from node and reference
#to list of lists representation.
def makeLists(tr):
    #If tree is empty the list is empty
    if tr == None:
        return []
    #Otherwise call the function recursively
    else:
        return [
            tr.getRootVal(),
            makeLists(tr.getLeftChild()),
            makeLists(tr.getRightChild())
        ]
    #If tree is empty the list is empty
    if tr == None:
        return []
    #Otherwise call the function recursively
    else:
        return [
            tr.getRootVal(),
            makeLists(tr.getLeftChild()),
            makeLists(tr.getRightChild())
        ]


#Level 0
t1 = BinaryTree(9)

#Level 1
t1.insertLeft(26)
t1.insertRight(18)

#Level 2
t1.getLeftChild().insertLeft(44)
t1.getLeftChild().insertRight(77)
t1.getRightChild().insertLeft(31)
t1.getRightChild().insertRight(93)

#Level 3
t1.getLeftChild().getLeftChild().insertLeft(55)

#Display list of lists representation
print(makeLists(t1))
def Encode_by_Tree(charcodelist):
    space = ' '
    myTree_check_list = []
    mytree = BinaryTree(space)
    check = True
    myTree_check_list.append(mytree)
    myTree_check_list.append(check)
    for i in range(0, len(charcodelist) - 1):
        if (not check):
            break
        line = charcodelist[i]
        mychar = line[0]
        mycode = line[1]
        mycode = mycode + mychar
        temp = BinaryTree(space)
        temp = mytree
        for digit in range(0, len(mycode) - 1):
            if (digit == (len(mycode) - 2)):
                digit = digit + 1

            if (mycode[digit] == '0'):
                if (temp.getLeftChild() == None):
                    temp.insertLeft(space)
                    temp = temp.getLeftChild()
                else:
                    if (temp.getLeftChild().getRootVal() == space):
                        temp = temp.getLeftChild()
                    else:
                        check = False
                        break

            else:
                if (mycode[digit] == '1'):
                    if (temp.getRightChild() == None):
                        temp.insertRight(space)
                        temp = temp.getRightChild()
                    else:
                        if (temp.getRightChild().getRootVal() == space):
                            temp = temp.getRightChild()
                        else:
                            check = False
                            break

                else:
                    if (mycode[len(mycode) - 2] == '0'):
                        try:
                            if (temp.getLeftChild().getRootVal() != None):
                                check = False
                                break
                        except AttributeError:
                            temp.insertLeft(mycode[digit])
                    if (mycode[len(mycode) - 2] == '1'):
                        try:
                            if (temp.getRightChild().getRootVal() != None):
                                check = False
                                break
                        except AttributeError:
                            temp.insertRight(mycode[digit])

    myTree_check_list[0] = mytree
    myTree_check_list[1] = check
    return myTree_check_list
Esempio n. 9
0
#!/usr/bin/python

from BinaryTree import BinaryTree


r = BinaryTree(10)
r.insertLeft(5)
r.getLeftChild().insertRight(6)
r.insertRight(15)
r.getRightChild().insertLeft(11)
r.getRightChild().insertRight(100)

print r.getRootVal()
print r.getLeftChild().getRootVal()
print r.getLeftChild().getRightChild().getRootVal()
print r.getRightChild().getRootVal()
print r.getRightChild().getLeftChild().getRootVal()
print r.getRightChild().getRightChild().getRootVal()

print

def preorder(tree):
    if tree:
        print(tree.getRootVal())
        preorder(tree.getLeftChild())
        preorder(tree.getRightChild())

preorder(r)
print
r.preorder()
print
Esempio n. 10
0
from BinaryTree import BinaryTree
import time

if __name__ == "__main__":
    root = BinaryTree(5)

    root.insertLeft(30)

    root.getLeftChild().insertLeft(40)

    root.getLeftChild().insertRight(25)

    root.insertRight(2)
    root.getRightChild().insertLeft(4)

    print("----- postorder traversal ----")
    t1 = time.time()
    res = root.postorder()
    t2 = time.time()
    print(res)
    print("postorder traversal took", t2 - t1, "seconds")

    print()
    print("----- preorder traversal -----")
    t1 = time.time()
    res = root.preorder()
    print(res)
    t2 = time.time()
    print("preorder traversal took", t2 - t1, "seconds")