Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __author__ = 'DMG'
- from Red import Queue
- class TreeNode(object):
- def __init__(self, data):
- self._data = data
- self._parent = None
- self._children = []
- self._indexes = []
- def isLeaf(self):
- return self._children is None
- def isRoot(self):
- return self._parent is None
- def addChild(self, n):
- n._parent = self
- self._children.append(n)
- class Trie(object):
- def __init__(self):
- self._root = None
- def isEmpty(self):
- return self._root is None
- def preorder(self, node):
- if not self.isEmpty():
- print node._data,
- for c in node._children:
- self.preorder(c)
- def postorder(self, node):
- if not self.isEmpty():
- for c in node._children:
- self.postorder(c)
- print node._data
- def breadthFirst(self):
- if not self.isEmpty():
- q = Queue()
- q.enQueue(self._root)
- while not q.isEmpty():
- e = q.deQueue()
- print e._data,
- for c in e._children:
- q.enQueue(c)
- # Feeling proud and stupid
- def addWord(self, node, string):
- if len(string) > 0:
- for c in node._children:
- if c._data == string[0]:
- self.addWord(c, string[1:])
- return
- while len(string) > 0:
- currentNode = TreeNode(string[0])
- node.addChild(currentNode)
- node = currentNode
- string = string[1:]
- trie = Trie()
- trie._root = TreeNode(" ")
- listaRijeci = ["bear", "sell", "stop", "stock", "bid", "bell", "buy"]
- for rijec in listaRijeci:
- trie.addWord(trie._root, rijec)
- trie.breadthFirst()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement