class Tree: def __init__(self): self.root = None def setRoot(self, node): self.root = node def size(self): if self.root == None: return 0 def subtreeSize(node): return 1 + sum(subtreeSize(c) for c in node.getChildren()) return subtreeSize(self.root) class BinaryNode: def __init__(self, v): self.val = v self.leftChild = None self.rightChild = None def get(self): return self.val def set(self, v): self.val = v def getChildren(self): children = [] if self.leftChild != None: children.append(self.leftChild) if self.rightChild != None: children.append(self.rightChild) return children class BinarySearchTree(Tree): def insert(self, val): if self.root == None: self.setRoot(BinaryNode(val)) else: currentRoot = self.root while(True): if val <= currentRoot.get(): if currentRoot.leftChild != None: currentRoot = currentRoot.leftChild else: currentRoot.leftChild = BinaryNode(val) break else: if currentRoot.rightChild != None: currentRoot = currentRoot.rightChild else: currentRoot.rightChild = BinaryNode(val) break def find(self, val): currentRoot = self.root while (currentRoot != None and currentRoot.get() != val): if val < currentRoot.get(): currentRoot = currentRoot.leftChild else: currentRoot = currentRoot.rightChild if currentRoot == None: return False else: return True if __name__ == "__main__": btree = BinarySearchTree() vals = [5, 3, 9, 4, 1, 7, 30] for val in vals: btree.insert(val) print "Tree size is %i" % btree.size() tests = [8, 7, 1, 5] for t in tests: print "find(%i) = %s" % (t, ("True" if btree.find(t) else "False"))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question