bintree.py you are to write a
class Bintree: with three method calls, as
indicated in the following excerpt from the main program
lab3.py.
from bintree import Bintree # All else in the file is hidden
svenska=Bintree()
svenska.put("gurka")
- - -
if svenska.exists("gurka"):
- - -
svenska.write() # Prints all words in alphabetic order
When the tree object receives the call put("gurka"), the object
sends its root pointer and the word gurka to a recursive function
putta, responsible for creating a new tree node in the right
position. The other method calls work in analogy.
class Bintree:
root=None
def put(self,newvalue):
self.root=putta(self.root,newvalue)
def exists(self,value):
return finns(self.root,value)
def write(self):
skriv(self.root)
print
Here the class definition ends, but the code continues with definitions of
putta, finns and skriv . If you try to add a doublet,
putta just prints it on the screen.
Of course there must also be a class Node: in the bintree file,
containing a value and two pointers.
In lab3.py you start by reading the file
/info/grudat06/word3.txt into your tree, for
example like this.
svenskfil=open("word3.txt") # Opens the file for reading
for rad in svenskfil.readlines():
ord=rad[0:3] # One three letter word per line
svenska.put(ord) # into the search tree
If you are successful, the doublets that are spit out will convey an
important message.
if engelska.exists(...). If the word was already there, you do
nothing, but if it is new you should also check if it happens to exist as
a Swedish word. If that is the case, it gets printed on the screen.
When you have done this correctly, the printed words will convey another secret message. Having received this message, you may try to get a teacher signature below.
Alpin pinal: Investigate which five letter words become other words when the first two letters are moved to the end. You may use the file /info/grudat06/word5.txt.