Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. class TreeNode:
  2.     def __init__(self, nucloet, index):
  3.         self.value = nucloet
  4.         self.children = []
  5.         self.index = index
  6.    
  7.     def addChild(self, childNode):
  8.         self.children.append(childNode)
  9.  
  10.     def print(self, parentIndex):
  11.         if self.value is not None:
  12.             print(parentIndex, self.index, self.value)
  13.         for child in self.children:
  14.             child.print(self.index)
  15.  
  16.  
  17. class Trie:
  18.     def __init__(self):
  19.         self.index = 1
  20.         self.root = TreeNode(None,self.index)
  21.  
  22.     def addWord(self, word):
  23.         current_node = self.root
  24.         for value in word:
  25.             found = False
  26.             for child in current_node.children:
  27.                 if child.value== value:
  28.                     current_node= child
  29.                     found = True
  30.                     break
  31.             if not found:
  32.                 self.index +=1
  33.                 newNode = TreeNode(value, self.index)
  34.                 current_node.addChild(newNode)
  35.                 current_node = newNode
  36.  
  37.     def print(self):
  38.         self.root.print(0)
  39.  
  40.  
  41. def read() :
  42.    dnas =[]
  43.    f =open('rosalind_trie.txt')
  44.    for line in f:
  45.       line = line.replace('\n', '')
  46.       dnas.append(line)
  47.    return dnas
  48. dnas = read()
  49.  
  50. trie = Trie()
  51. for dna in dnas:
  52.     trie.addWord(dna)
  53.  
  54. trie.print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement