-
Notifications
You must be signed in to change notification settings - Fork 0
/
bintree.py
84 lines (60 loc) · 1.91 KB
/
bintree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# ECS 36A - Joey Hua - Matt Bishop
# Homework 5
class Node():
def __init__(self, data=0, count=1):
self.data = data
self.count = count
self.lnode = None
self.rnode = None
def __str__(self):
return str(self.data) + ' (' + str(self.count) + ')'
##### ~~~~~~~~ END OF NODE CLASS ~~~~~~~~ #####
### main execution
import re
def getwords():
# ask user for input of file
filename = input('Enter a text file that you want to turn into a binary tree: ')
# open the file
try:
with open(filename) as f:
fcontents = f.read()
# split file based on everything except for alphanumerics
wordlist = re.split('[\W]', fcontents)
# delete blanks strings
for word in wordlist:
if word == '':
wordlist.remove(word)
# start with the root and create the tree
rootnode = Node(wordlist[0])
for word in wordlist:
if word is not rootnode.data:
newnode = Node(word)
createtree(rootnode, newnode)
# print the tree
printtree(rootnode)
except FileNotFoundError:
print('Either the file does not exist or you entered the wrong pathname.')
# add words to tree
def createtree(root=Node(), node=Node()):
# go down the tree and compare words
if node.data == root.data:
root.count += 1
elif node.data < root.data:
if root.lnode is None:
root.lnode = node
else:
createtree(root.lnode, node)
elif node.data > root.data:
if root.rnode is None:
root.rnode = node
else:
createtree(root.rnode, node)
# print the tree
def printtree(node=Node(), level=0):
if node is not None:
printtree(node.lnode, level+1)
print(' ' * (level) + str(node))
printtree(node.rnode, level+1)
def main():
getwords()
main()